-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathtest.py
178 lines (152 loc) · 6.92 KB
/
test.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
# Set your own model path
MODELDIR = "D:\\ltp_data_v3.4.0"
import sys
import os
from pyltp import Segmentor, Postagger, Parser, NamedEntityRecognizer
print("正在加载LTP模型... ...")
segmentor = Segmentor()
segmentor.load(os.path.join(MODELDIR, "cws.model"))
# segmentor.load_with_lexicon(os.path.join(MODELDIR, "cws.model"),'D:\\ltp_data_v3.4.0\\personal_seg.txt')
postagger = Postagger()
postagger.load(os.path.join(MODELDIR, "pos.model"))
parser = Parser()
parser.load(os.path.join(MODELDIR, "parser.model"))
recognizer = NamedEntityRecognizer()
recognizer.load(os.path.join(MODELDIR, "ner.model"))
print("加载模型完毕。")
in_file_name = "input.txt"
out_file_name = "output.txt"
begin_line = 1
end_line = 0
def extraction_start(in_file_name, out_file_name, begin_line, end_line):
"""
事实三元组抽取的总控程序
Args:
in_file_name: 输入文件的名称
#out_file_name: 输出文件的名称
begin_line: 读文件的起始行
end_line: 读文件的结束行
"""
in_file = open(in_file_name, 'r', encoding='utf-8')
out_file = open(out_file_name, 'w', encoding='utf-8')
for line in in_file:
fact_triple_extract(line.strip(), out_file)
in_file.close()
out_file.close()
def fact_triple_extract(sentence, out_file):
"""
对于给定的句子进行事实三元组抽取
Args:
sentence: 要处理的语句
"""
# print sentence
words = segmentor.segment(sentence)
print("\t".join(words))
postags = postagger.postag(words)
print("\t".join(postags))
netags = recognizer.recognize(words, postags)
arcs = parser.parse(words, postags)
print("\t".join("%d:%s" % (arc.head, arc.relation) for arc in arcs))
child_dict_list = build_parse_child_dict(words, postags, arcs)
for index in range(len(postags)):
# 抽取以谓词为中心的事实三元组
if postags[index] == 'v':
child_dict = child_dict_list[index]
# 主谓宾
if 'SBV' in child_dict and 'VOB' in child_dict:
e1 = complete_e(words, postags, child_dict_list, child_dict['SBV'][0])
r = words[index]
e2 = complete_e(words, postags, child_dict_list, child_dict['VOB'][0])
out_file.write("主语谓语宾语关系\t(%s, %s, %s)\n" % (e1, r, e2))
out_file.flush()
# 定语后置,动宾关系
if arcs[index].relation == 'ATT':
if 'VOB' in child_dict:
e1 = complete_e(words, postags, child_dict_list, arcs[index].head - 1)
r = words[index]
e2 = complete_e(words, postags, child_dict_list, child_dict['VOB'][0])
temp_string = r + e2
if temp_string == e1[:len(temp_string)]:
e1 = e1[len(temp_string):]
if temp_string not in e1:
out_file.write("定语后置动宾关系\t(%s, %s, %s)\n" % (e1, r, e2))
out_file.flush()
# 含有介宾关系的主谓动补关系
if 'SBV' in child_dict and 'CMP' in child_dict:
# e1 = words[child_dict['SBV'][0]]
e1 = complete_e(words, postags, child_dict_list, child_dict['SBV'][0])
cmp_index = child_dict['CMP'][0]
r = words[index] + words[cmp_index]
if 'POB' in child_dict_list[cmp_index]:
e2 = complete_e(words, postags, child_dict_list, child_dict_list[cmp_index]['POB'][0])
out_file.write("介宾关系主谓动补\t(%s, %s, %s)\n" % (e1, r, e2))
out_file.flush()
# 尝试抽取命名实体有关的三元组
if netags[index][0] == 'S' or netags[index][0] == 'B':
ni = index
if netags[ni][0] == 'B':
while netags[ni][0] != 'E':
ni += 1
e1 = ''.join(words[index:ni + 1])
else:
e1 = words[ni]
if arcs[ni].relation == 'ATT' and postags[arcs[ni].head - 1] == 'n' and netags[arcs[ni].head - 1] == 'O':
r = complete_e(words, postags, child_dict_list, arcs[ni].head - 1)
if e1 in r:
r = r[(r.index(e1) + len(e1)):]
if arcs[arcs[ni].head - 1].relation == 'ATT' and netags[arcs[arcs[ni].head - 1].head - 1] != 'O':
e2 = complete_e(words, postags, child_dict_list, arcs[arcs[ni].head - 1].head - 1)
mi = arcs[arcs[ni].head - 1].head - 1
li = mi
if netags[mi][0] == 'B':
while netags[mi][0] != 'E':
mi += 1
e = ''.join(words[li + 1:mi + 1])
e2 += e
if r in e2:
e2 = e2[(e2.index(r) + len(r)):]
if r + e2 in sentence:
out_file.write("人名//地名//机构\t(%s, %s, %s)\n" % (e1, r, e2))
out_file.flush()
def build_parse_child_dict(words, postags, arcs):
"""
为句子中的每个词语维护一个保存句法依存儿子节点的字典
Args:
words: 分词列表
postags: 词性列表
arcs: 句法依存列表
"""
child_dict_list = []
for index in range(len(words)):
child_dict = dict()
for arc_index in range(len(arcs)):
if arcs[arc_index].head == index + 1:
keys = child_dict.keys()
if arcs[arc_index].relation in keys:
child_dict[arcs[arc_index].relation].append(arc_index)
else:
child_dict[arcs[arc_index].relation] = []
child_dict[arcs[arc_index].relation].append(arc_index)
# if child_dict.has_key('SBV'):
# print words[index],child_dict['SBV']
child_dict_list.append(child_dict)
return child_dict_list
def complete_e(words, postags, child_dict_list, word_index):
"""
完善识别的部分实体
"""
child_dict = child_dict_list[word_index]
prefix = ''
if 'ATT' in child_dict:
for i in range(len(child_dict['ATT'])):
prefix += complete_e(words, postags, child_dict_list, child_dict['ATT'][i])
postfix = ''
if postags[word_index] == 'v':
if 'VOB' in child_dict:
postfix += complete_e(words, postags, child_dict_list, child_dict['VOB'][0])
if 'SBV' in child_dict:
prefix = complete_e(words, postags, child_dict_list, child_dict['SBV'][0]) + prefix
return prefix + words[word_index] + postfix
if __name__ == "__main__":
extraction_start(in_file_name, out_file_name, begin_line, end_line)