###質問
LDAモデルに詳しい方に質問です。
また、以下に記すプログラムで
複数の小説のテキストファイルからLDAモデルを作成したとき、その中に含まれる
1つの文書のトピックを参照できるライブラリはあるでしょうか?
コード内のnovel_set(ディレクトリ)には複数のテキストファイルが含まれていて、
files[file_no]でfile_no番目のテキストファイル名は
参照できるようになっています。
###実装中のプログラム
python
1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4""" 5トピックモデル(入力:novel_setの全ての.txtファイル) 6各ファイルの管理可能 7形態素解析対象:名詞(代名詞,非自立名詞,数,固有名詞を除く) 8""" 9 10import time 11import glob 12import MeCab 13from gensim import corpora, models 14 15t0 = time.time() 16 17""" 18files:novel_set内のファイルリスト 19files[file_no]:ファイル番号file_noのファイル名 20""" 21files = glob.glob('novel_set/*.txt') 22 23#形態素解析(文字列を改行位置で区切って分割) 24mecab = MeCab.Tagger ("-Ochasen") 25 26texts = [] 27for file in files: 28 f = open(file, "r",encoding = "utf-8") 29 text = f.read() 30 f.close() 31 chunks = mecab.parse(text).splitlines() 32 33 #絞り込み 34 sels = [] 35 for chunk in chunks: 36 cols = chunk.split('\t') 37 if len(cols) >= 4: 38 parts = cols[3].split('-') 39 #指定した品詞で始まっている場合 → true 40 if parts[0].startswith('名詞'): 41 #代名詞,非自立名詞,固有名詞,数を含めない 42 if parts[1] in ['代名詞','非自立','固有名詞','数']: 43 continue 44 #形態素を追加 45 #sels:形態素(原形)のみの行列 46 sels.append(cols[2]) 47 texts.append(sels) 48 49dictionary = corpora.Dictionary(texts) 50corpus = [dictionary.doc2bow(text) for text in texts] 51 52t1 = time.time() 53 54#トピック数 55num_topics = 10 56 57#LDAモデルに投入 58lda = models.LdaModel(corpus = corpus, id2word = dictionary,num_topics = num_topics) 59 60t2 = time.time() 61 62print(lda) 63 64#各トピックにおける各トークンの出現確率を係数で表示 65for topic in lda.show_topics(-1): 66 print(topic) 67 68corpus_time = t1 - t0 69print('コーパス生成時間:%f秒'%corpus_time) 70 71lda_time = t2 -t1 72print('LDAモデル生成時間:%f秒'%lda_time) 73 74total_time = t2 - t0 75print('合計時間:%f秒'%total_time)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/21 16:55
2017/11/21 17:31
2017/11/21 18:40
2017/11/21 18:44
2017/11/22 03:39
2017/11/30 10:09
2017/11/30 10:38
2017/12/01 02:07
2017/12/01 07:19
2017/12/04 17:02
2017/12/05 03:59
2017/12/05 04:43