問題文
問題の概要
「30. 形態素解析結果の読み込み」で作成したデータ構造から「x['pos'] == '動詞'」の「base」部分を取り出します。
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_base(block): res = list(filter(lambda x: x['pos'] == '動詞', block)) res = [r['base'] for r in res] return res 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] ans = [extract_base(block) for block in blocks] print(ans[5])