<指定した条件のみ、共起の結果を出力して欲しい>
私は、nltkで数値と共起している語を抽出したいと考えているのですが、
品詞などを指定すれば出来ると思っていたのですがうまくいきません。
一応、助詞・助動詞・読点・句点は除くことが出来ました。
python
1import nltk 2from nltk import ngrams 3from pyknp import KNP 4import jctconv 5import re 6 7 8# knpで形態素解析を行ったあとの結果をリストで返す 9def separate(text,option = '-tab'): 10 number_word = [] 11 tokens = [] 12 knp = KNP(option = option) 13 result = knp.parse(jctconv.h2z(text,digit = True)) 14 15 for mrph in result.mrph_list(): 16 if not mrph.hinsi == '助詞' and not mrph.hinsi == '助動詞' and not mrph.bunrui == '読点' and not mrph.bunrui == '句点': 17 print(mrph.bunrui) 18 tokens.append(mrph.midasi) 19 return tokens 20 21 22# 形態素解析を行った結果から、共起の結果を得る 23def word_cooccurrence(list): 24 bigrams = nltk.bigrams(list) # バイグラムを作成 25 fd = nltk.FreqDist(bigrams) # バイグラムの出現頻度分布 26 return fd.items() 27 28 29if __name__ == '__main__': 30 with open('text.txt',encoding = 'utf8') as f: 31 text = f.read() 32 result = word_cooccurrence(separate(text)) 33 print(result) 34 35<text.txtの中身> 36ポット内の温度が100℃より低い場合、水が沸騰しない。 37 38<実行結果> 39dict_items([(('ポット', '内'), 1), (('内', '温度'), 1), (('温度', '100'), 1), (('100', '℃'), 1), (('℃', '低い'), 1), (('低い', '場合'), 1), (('場合', '水'), 1), (('水', '沸騰'), 1), (('沸騰', 'し'), 1), (('し', 'ない'), 1)])
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/06/26 05:44