-
Notifications
You must be signed in to change notification settings - Fork 0
/
p040.py
executable file
·62 lines (44 loc) · 1.86 KB
/
p040.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
40. 係り受け解析結果の読み込み(形態素)
形態素を表すクラスMorphを実装せよ.このクラスは表層形(surface),
基本形(base),品詞(pos),品詞細分類1(pos1)をメンバ変数に
持つこととする.さらに,CaboChaの解析結果(neko.txt.cabocha)を読み込み,
各文をMorphオブジェクトのリストとして表現し,3文目の形態素列を表示せよ.
"""
import sys
from itertools import groupby
class Morph(object):
def __init__(self, line):
self.surface, remain = line.strip().split('\t')
self.pos, self.pos1, _, _, _, _, self.base = remain.split(',')[:7]
def __str__(self):
return '{surface}\t{base}\t{pos}\t{pos1}'.format(**self.__dict__)
def __getitem__(self, item):
# attr = 'surface'; morph[attr] で読み出せるようになる.
# これをしないと getattr() とか使う必要があってちょっと面倒.
return self.__dict__[item]
class Line(object):
EOS, CHUNK, MORPH = range(3)
def classify_cabocha_line(line):
if line.startswith('EOS'):
return Line.EOS
elif line.startswith('* '):
return Line.CHUNK
else:
return Line.MORPH
def parse_cabocha(cabocha_seq):
for startswith_eos, sentence in groupby(cabocha_seq,
key=lambda l: l.startswith('EOS')):
if not startswith_eos:
for key, group in groupby(sorted(sentence, key=classify_cabocha_line),
key=classify_cabocha_line):
if key == Line.MORPH:
yield [Morph(line) for line in group]
def main():
for morphs in parse_cabocha(sys.stdin):
for morph in morphs:
print morph
if __name__ == '__main__':
main()