👩💻
言語処理100本ノック 2020 (Rev 2) 第4章: 形態素解析 37. 「猫」と共起頻度の高い上位10語
問題
37. 「猫」と共起頻度の高い上位10語
「猫」とよく共起する(共起頻度が高い)10語とその出現頻度をグラフ(例えば棒グラフなど)で表示せよ.
solution37.py
import itertools
from collections import Counter
from collections import defaultdict
import matplotlib.pyplot as plt
import japanize_matplotlib
def parse_mecab(block):
res = []
for line in block.split('\n'):
if line == '':
return res
(surface, attr) = line.split('\t')
attr = attr.split(',')
lineDict = {
'surface': surface,
'base': attr[6],
'pos': attr[0],
'pos1': attr[1]
}
res.append(lineDict)
filename = 'chapter04/neko.txt.mecab'
with open(filename, mode='rt', encoding='utf-8') as f:
blocks = f.read().split('EOS\n')
filtered_blocks = list(filter(lambda x: x != '', blocks))
parse_blocks = [parse_mecab(block) for block in filtered_blocks]
neko_cooc = Counter([])
for sentence in parse_blocks:
words = [word["base"] for word in sentence]
if "猫" in words:
neko_cooc += Counter(words)
neko_cooc = dict(neko_cooc.most_common()[:11])
neko_cooc.pop("猫")
print(neko_cooc)
plt.figure()
plt.bar(neko_cooc.keys(), neko_cooc.values())
plt.show()
output
この問題では、各ブロックの形態素解析の結果から、「猫」と共起する単語の出現頻度を集計し、その結果を棒グラフで可視化します。
Discussion