###やりたいこと
テキストファイルからトピックモデルを作成するプログラムを実装中なのですが、コーパスを生成するのに用いる辞書に指示語(あれ、ここ、そちら)や、それ自体に重要な意味を持たない動詞(いる、ある)を含んでしまっているので精度があまりよくありません。
そこで決め打ちで指定した単語を辞書に含ませないようにする方法を教えていただきたいです。
以下に実装中のコードを記します。
python
1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3 4#トピックモデル(入力:指定したディレクトリ内の.txtファイル) 5 6import time 7import os 8import glob 9import MeCab 10from gensim import corpora, models 11 12#ディレクトリ指定 13input_dir = input('select directory:') 14 15t0 = time.time() 16 17txt_list = glob.glob(input_dir + os.sep + '*.txt') 18 19texts = '' 20for file in txt_list: 21 with open(file, 'r', encoding='utf-8') as f: 22 texts += f.read() 23 24f.close() 25 26#文字列を改行位置で区切って分割する(形態素解析) 27m = MeCab.Tagger ("-Ochasen") 28chunks = m.parse(texts).splitlines() 29 30#絞り結果を格納 31sels = [] 32 33#絞り込み 34for chunk in chunks: 35 #chunk:形態素解析結果(1行の文字列) 36 #タブ(\t)で区切り、文字列リストを作成 37 cols = chunk.split('\t') 38 if len(cols) >= 4: 39 #parts[0]:品詞の種類 40 parts = cols[3].split('-') 41 #指定した品詞で始まっている場合 → true 42 if parts[0].startswith('名詞') or parts[0].startswith('動詞'): 43 #sels:形態素(原形)のみの行列 44 sels.append(cols[2]) 45 46#辞書作成 47dictionary = corpora.Dictionary([sels]) 48 49""" 50no_below:出現回数no_below回以下の単語を排除 51no_above:全体のno_above以上に出現した単語を排除(no_above = 0.3 ➝ 30%) 52""" 53#辞書加工 54#dictionary.filter_extremes(no_below = 1,no_above = 0.3) 55 56 57#[辞書の単語数]次元のベクトルに変換(コーパス作成) 58corpus = [dictionary.doc2bow(sels) for sel in sels] 59 60t1 = time.time() 61 62#LDAモデルに投入 63lda = models.LdaModel(corpus = corpus, 64 id2word = dictionary, 65 #トピック数 66 num_topics = 20) 67 68t2 = time.time() 69 70print(lda) 71 72#各トピックにおける各トークンの出現確率を係数で表示 73for topic in lda.show_topics(-1): 74 print(topic) 75 76""" 77#文書の推定トピック番号 , 推定の確信度を表示 78for topics_per_document in lda[corpus]: 79 print(topics_per_document) 80""" 81 82corpus_time = t1 - t0 83print('コーパス生成時間:%f秒'%corpus_time) 84 85lda_time = t2 -t1 86print('LDAモデル生成時間:%f秒'%lda_time) 87 88total_time = t2 - t0 89print('合計時間:%f秒'%total_time)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/09 04:50