質問編集履歴
1
情報の追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,115 +1 @@
|
|
1
1
|
Cabocha ( https://taku910.github.io/cabocha/ ) を使っています。
|
2
|
-
|
3
|
-
キーになる名詞の抽出を行いたいです。
|
4
|
-
|
5
|
-
```ここに言語を入力
|
6
|
-
|
7
|
-
import CaboCha
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
def get_word(tree, chunk):
|
12
|
-
|
13
|
-
surface = ''
|
14
|
-
|
15
|
-
for i in range(chunk.token_pos, chunk.token_pos + chunk.token_size):
|
16
|
-
|
17
|
-
token = tree.token(i)
|
18
|
-
|
19
|
-
features = token.feature.split(',')
|
20
|
-
|
21
|
-
if features[0] == '名詞':
|
22
|
-
|
23
|
-
surface += token.surface
|
24
|
-
|
25
|
-
elif features[0] == '形容詞':
|
26
|
-
|
27
|
-
surface += features[6]
|
28
|
-
|
29
|
-
break
|
30
|
-
|
31
|
-
elif features[0] == '動詞':
|
32
|
-
|
33
|
-
surface += features[6]
|
34
|
-
|
35
|
-
break
|
36
|
-
|
37
|
-
return surface
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
def get_2_words(line):
|
42
|
-
|
43
|
-
cp = CaboCha.Parser('-f1')
|
44
|
-
|
45
|
-
tree = cp.parse(line)
|
46
|
-
|
47
|
-
chunk_dic = {}
|
48
|
-
|
49
|
-
chunk_id = 0
|
50
|
-
|
51
|
-
for i in range(0, tree.size()):
|
52
|
-
|
53
|
-
token = tree.token(i)
|
54
|
-
|
55
|
-
if token.chunk:
|
56
|
-
|
57
|
-
chunk_dic[chunk_id] = token.chunk
|
58
|
-
|
59
|
-
chunk_id += 1
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
tuples = []
|
64
|
-
|
65
|
-
for chunk_id, chunk in chunk_dic.items():
|
66
|
-
|
67
|
-
if chunk.link > 0:
|
68
|
-
|
69
|
-
from_surface = get_word(tree, chunk)
|
70
|
-
|
71
|
-
to_chunk = chunk_dic[chunk.link]
|
72
|
-
|
73
|
-
to_surface = get_word(tree, to_chunk)
|
74
|
-
|
75
|
-
tuples.append((from_surface, to_surface))
|
76
|
-
|
77
|
-
return tuples
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
if __name__ == '__main__' :
|
82
|
-
|
83
|
-
line = '太郎は花子が読んでいる本を次郎に渡した'
|
84
|
-
|
85
|
-
tuples = get_2_words(line)
|
86
|
-
|
87
|
-
for t in tuples:
|
88
|
-
|
89
|
-
print(t[0] + ' => ' + t[1])
|
90
|
-
|
91
|
-
```
|
92
|
-
|
93
|
-
とコードを書いて実行すると、
|
94
|
-
|
95
|
-
```ここに言語を入力
|
96
|
-
|
97
|
-
太郎 => 渡す
|
98
|
-
|
99
|
-
花子 => 読む
|
100
|
-
|
101
|
-
読む => 本
|
102
|
-
|
103
|
-
本 => 渡す
|
104
|
-
|
105
|
-
次郎 => 渡す
|
106
|
-
|
107
|
-
```
|
108
|
-
|
109
|
-
と出力されました。
|
110
|
-
|
111
|
-
太郎が渡したものを抽出したいのですが、
|
112
|
-
|
113
|
-
渡すにかかっているのは太郎以外に本と次郎があって、今回のケースではどちらを渡したのか判別がつきません。
|
114
|
-
|
115
|
-
太郎が渡したもの=本を抽出するにはどうしたらいいですか?
|