###やりたいこと
タイトルにもある通りなのですが、python3で作成したプログラムの入力に
作業ディレクトリ内にある、テキストファイルを複数保存したフォルダを使いたいと
思っています。
以下に記すプログラムでは入力として、作業ディレクトリ内の1つのテキストファイルを指定するようになっています。
現在実装中のプログラムをどのように修正すれば良いのかを教えていただきたいです。
###実装中のプログラム
python3
1# -*- coding: utf-8 -*- 2 3import time 4import MeCab 5from gensim import corpora, models, similarities 6from xlwt import Workbook 7 8#ファイル指定 9doc = input('select file(ex, ○○.txt):') 10t0 = time.time() 11f = open(doc, "r",encoding = "utf-8") 12text = f.read() 13f.close() 14 15#文字列を改行位置で区切って分割する(形態素解析) 16m = MeCab.Tagger ("-Ochasen") 17chunks = m.parse(text).splitlines() 18 19#絞り結果を格納 20sels = [] 21 22#絞り込み 23for chunk in chunks: 24 #chunk:形態素解析結果(1行の文字列) 25 #タブ(\t)で区切り、文字列リストを作成 26 cols = chunk.split('\t') 27 if len(cols) >= 4: 28 #parts[0]:品詞の種類 29 parts = cols[3].split('-') 30 #指定した品詞で始まっている場合 → 真 31 if parts[0].startswith('名詞') or parts[0].startswith('動詞'): 32 #要素(形態素)を追加 33 #sels:要素(形態素)のみの行列 34 sels.append(chunk[0]) 35 36#辞書作成 37dictionary = corpora.Dictionary([sels]) 38""" 39#単語の出現回数が1以下を除く 40dictionary.filter_extremes(no_below = 1) 41""" 42 43#[辞書の単語数]次元のベクトルに変換(コーパス作成) 44corpus = [dictionary.doc2bow(sels) for sel in sels] 45 46t1 = time.time() 47#LDAモデルに投入 48lda = models.LdaModel(corpus = corpus, id2word = dictionary, 49 #トピック数:20 50 num_topics = 20) 51t2 = time.time() 52print(lda) 53 54 55#各トピックにおける各トークンの出現確率を係数で表示 56for topic in lda.show_topics(-1): 57 print(topic) 58 59""" 60#文書の推定トピック番号 , 推定の確信度を表示 61for topics_per_document in lda[corpus]: 62 print(topics_per_document) 63""" 64 65lda_time = t2 -t1 66print('LDAモデル生成時間:%f秒'%lda_time) 67 68total_time = t2 - t0 69print('合計時間:%f秒'%total_time)
汚いコードで申し訳ありません。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/27 06:26
2017/10/27 06:33
2017/10/27 07:12
2017/10/27 07:26
2017/10/27 07:28