u++の備忘録

言語処理100本ノック 2020「35. 単語の出現頻度」

問題文

nlp100.github.io

問題の概要

30. 形態素解析結果の読み込み」で作成したデータ構造から、全ての単語を取り出し、出現頻度を計算します。

全ての単語を前から確認していき(単語, 出現回数)のペアの辞書を作り上げました。普通のdictではなく「defaultdict」*1が便利です。

from collections import defaultdict


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)


def extract_words(block):
    return [b['base'] + '_' + b['pos'] + '_' + b['pos1'] for b in block]


filename = 'ch04/neko.txt.mecab'
with open(filename, mode='rt', encoding='utf-8') as f:
    blocks = f.read().split('EOS\n')
blocks = list(filter(lambda x: x != '', blocks))
blocks = [parse_mecab(block) for block in blocks]
words = [extract_words(block) for block in blocks]
d = defaultdict(int)
for word in words:
    for w in word:
        d[w] += 1
ans = sorted(d.items(), key=lambda x: x[1], reverse=True)
print(ans)