forked from Learning0411/Knowledge-data-evaluation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_Triplet.py
276 lines (246 loc) · 9.97 KB
/
write_Triplet.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import re
import copy
import os
zhongwen = re.compile(u'[\u4e00-\u9fa5]') # 检查非中文
t = '{'
don = '、'
comma = ','
colon = ':'
find_all = lambda data, s: [r for r in range(len(data)) if data[r] == s]
# 句子结构转换
def index2tag(sentence):
names = []
attrs = []
tag_list = find_all(sentence, t)
don_list = find_all(sentence, don)
comma_list = find_all(sentence, comma)
colon_list = find_all(sentence, colon)
# print(pun1_list)
pun_list = sorted(don_list + comma_list + colon_list)
all_list = sorted(pun_list + tag_list)
# 将标点索引替换成标点符号
for i in range(len(all_list)):
# 替换出除大括号以外其他的标点符号
if sentence[all_list[i]] != '{':
all_list[i] = sentence[all_list[i]]
entity_list = copy.deepcopy(all_list)
entity_type_list = copy.deepcopy(all_list)
# endfor
# 据姚觐元{-3 PER}、钱保塘{-3 PER}《涪州石鱼文字所见录》{-11 BOOK},耆后可补入“□□□□□□□瑾公琰。
# ['姚觐元', '、', '钱保塘', '《涪州石鱼文字所见录》', ',']
# ['PER', '、', 'PER', ' BO', ',']
for i in range(len(all_list)):
for j in range(len(tag_list)):
if tag_list[j] == all_list[i]: # all_list[i] = '{'
brace_index = int(tag_list[j]) # 括号索引
abb_index = int(tag_list[j]) + 4 # 实体类型标注索引
length = int(sentence[brace_index + 2: brace_index + 4]) # 标注长度
entity_content = sentence[brace_index - length: brace_index] # 实体
entity_type = sentence[brace_index + 4: brace_index + 7] # 类型
changdu = zhongwen.findall(str(entity_content)) # 获取中文字符个数
L1 = length - len(changdu) # 非中文个数
ss = 1
entity_content = list(entity_content)
for ii in range(L1):
while 1:
ch = sentence[brace_index - length - ss]
ss += 1
if '\u4e00' <= ch <= '\u9fff':
entity_content.insert(0, ch)
break
count = 0
# 去除括号等
for iii in range(len(entity_content)):
if '\u4e00' > entity_content[iii] or entity_content[iii] > '\u9fff':
count += 1
entity_content[iii] = 0
for jj in range(count):
entity_content.remove(0)
entity_content = ''.join(entity_content) # 列表转换字符串
entity_type_list[i] = entity_type
entity_list[i] = entity_content
# endfor
return entity_list, entity_type_list
def find_index(path, Triplepath):
Triple_Set = []
pun_distance = {':': 1, '、': 2, ',': 5}
# relation=[('GZ','PE'),{'PE':'GZ'},{'PE':'GM'},{'PE':'ZH'},{'PE':'TI'},{'PE':'LO'},{'PE':'BO'},{'PE':'PE'}{'BO':'TI'},{'TK':'TI'},{'TK','Content'},{'TK':'BO'},{'TK':'PE'}]
# relation=[('GZ}','PER'),('PER','GZ}'),('PER',' BO'),('PER','GM}'),('PER','ZH}'),('PER','TIM'),('PER','LOC'),('PER','BOO'),('BOO','TIM')]
relation = [('GZ}', 'PER'), ('TIM', 'PER'), ('BOO', 'PER'), ('GZ}', 'PER'), ('GM}', 'PER'), ('PER', 'GZ}'),
('PER', ' BO'), ('PER', 'GM}'), ('PER', 'ZZ}'), ('PER', 'TIM'),
('PER', 'LOC'), ('PER', 'BOO'), ('BOO', 'TIM'), ('PER', 'HH}')]
with open(path, 'r', encoding='utf-8') as f:
for txt in f:
txt = txt.replace('\n', '')
txt = txt.split('。') # 按句分割
# entity_list ['姚觐元', '、', '钱保塘', ':', '《涪州石鱼文字所见录》', ',', ',', '广陵书社', ',', ',']
# entity_type_list ['PER', '、', 'PER', ':', ' BO', ',', ',', 'ORG', ',', ',']
for sentence in txt:
entity_list, entity_type_list = index2tag(sentence)
for i in range(len(entity_type_list)):
distance = 0
sdcount = 0
gzcount = 0
zcount = 0
hcount = 0
dmcount = 0
gmcount = 0
flag_start_type = entity_type_list[i]
flag_start_content = entity_list[i]
# print('flag_start_type:', flag_start_type)
# print('flag_start_content:', flag_start_content)
for j in range(i + 1, len(entity_type_list)):
flag_end_type = entity_type_list[j]
flag_end_content = entity_list[j]
# print(flag_end_content)
if flag_end_type == ':':
distance = distance + 1
elif flag_end_type == ',':
distance = distance + 5
elif flag_end_type == '、':
distance = distance + 2
else:
distance = distance + 1
# print('距离',distance)
if distance > 100:
break
else:
triple = (flag_start_type, flag_end_type)
if triple in relation:
if flag_start_type == 'GZ}' and flag_end_type == 'PER':
if gzcount == 0:
triple_content = (flag_end_content, '人物官职', distance, flag_start_content)
Triple_Set.append(triple_content)
gzcount += 1
else:
continue
elif flag_start_type == 'PER' and flag_end_type == 'GZ}':
if gzcount == 0:
triple_content = (flag_start_content, '人物官职', distance, flag_end_content)
Triple_Set.append(triple_content)
gzcount += 1
else:
continue
elif flag_start_type == 'PER' and flag_end_type == ' BO':
if '题' in flag_end_content:
triple_content = (flag_start_content, '人物题刻', distance, flag_end_content[2:])
else:
triple_content = (flag_start_content, '人物书名', distance, flag_end_content[2:])
Triple_Set.append(triple_content)
elif flag_start_type == 'PER' and flag_end_type == 'TIM':
if sdcount == 0:
triple_content = (flag_start_content, '人物时代', distance, flag_end_content)
Triple_Set.append(triple_content)
sdcount += 1
else:
continue
elif flag_start_type == 'TIM' and flag_end_type == 'PER':
if sdcount == 0:
triple_content = (flag_end_content, '人物时代', distance, flag_start_content)
Triple_Set.append(triple_content)
sdcount += 1
else:
continue
elif flag_start_type == 'ZZ}' and flag_end_type == 'PER':
if zcount == 0:
triple_content = (flag_end_content, '人物字号', distance, flag_start_content)
Triple_Set.append(triple_content)
zcount += 1
else:
continue
elif flag_start_type == 'BOO' and flag_end_type == 'PER':
if '题' in flag_start_type:
triple_content = (flag_end_content, '人物题刻', distance, flag_start_content)
Triple_Set.append(triple_content)
else:
triple_content = (flag_end_content, '人物书名', distance, flag_start_content)
Triple_Set.append(triple_content)
elif flag_start_type == 'PER' and flag_end_type == 'GM}':
if gmcount == 0:
triple_content = (flag_start_content, '人物功名', distance, flag_end_content)
Triple_Set.append(triple_content)
gmcount += 1
else:
continue
elif flag_start_type == 'PER' and flag_end_type == 'ZZ}':
if zcount == 0:
triple_content = (flag_start_content, '人物字号', distance, flag_end_content)
Triple_Set.append(triple_content)
zcount += 1
else:
continue
elif flag_start_type == 'PER' and flag_end_type == 'HH}':
if hcount == 0:
triple_content = (flag_start_content, '人物字号', distance, flag_end_content)
Triple_Set.append(triple_content)
hcount += 1
else:
continue
elif flag_start_type == 'PER' and flag_end_type == 'LOC':
if dmcount == 0:
triple_content = (flag_start_content, '人物地名', distance, flag_end_content)
Triple_Set.append(triple_content)
dmcount += 1
else:
continue
elif flag_start_type == 'PER' and flag_end_type == 'BOO':
if '题' in flag_end_content:
triple_content = (flag_start_content, '人物题刻', distance, flag_end_content[2:])
else:
triple_content = (flag_start_content, '人物书名', distance, flag_end_content[2:])
Triple_Set.append(triple_content)
elif flag_start_type == 'BOO' and flag_end_type == 'TIM':
if '题' in flag_start_content:
triple_content = (flag_start_content[2:], '题刻时代', distance, flag_end_content)
else:
triple_content = (flag_start_content[2:], '书名时代', distance, flag_end_content)
Triple_Set.append(triple_content)
with open(Triplepath + '\\' + 'Triple_Set副本.txt', 'a', encoding='utf-8') as ff:
for item in Triple_Set:
if len(item[0]) > 1 and len(item[3]) > 1:
ff.write(str(item[0]))
ff.write('\t')
ff.write(str(item[1]))
ff.write('\t')
# f.write(str(item[2]))
# f.write('\t')
ff.write(str(item[3]))
ff.write('\n')
Triple_Set = []
tfile = open(Triplepath + '\\' + 'Triple_Set副本.txt', 'r', encoding='utf-8')
templist = tfile.readlines()
templist = set(templist)
filetemp = open(Triplepath + '\\' + 'Triple_Set.txt', 'w', encoding='utf-8')
for ite in templist:
filetemp.write(str(ite))
filetemp.close()
# def judge_cate()
def relation_match(relation, flag):
dic = []
for item in relation:
if item[0] == flag:
dic.append(item)
# endfor
return dic
def quchong1(filepath):
f = open(filepath, 'r', encoding='utf-8')
guanzhi_list = []
buff = f.readlines()
f.close()
fp = open(filepath, 'w+', encoding='utf-8')
for guanzhi in buff:
guanzhi_temp = guanzhi.strip()
if guanzhi_temp in guanzhi_list:
continue
fp.write(guanzhi)
guanzhi_list.append(guanzhi_temp)
fp.close()
def main(path):
files = os.listdir(path)
for file in files:
Triplepath = path + '\\' + file
biaozhu_path = path + '\\' + file + '\\' + '原文标注.txt'
find_index(biaozhu_path, Triplepath)
os.remove(Triplepath + '\\' + 'Triple_Set副本.txt')
# print('end')
# quchong1(r'E:\项目文件\Triple_Set1.txt')