-
Notifications
You must be signed in to change notification settings - Fork 5
/
LAnn2Triple.py
266 lines (260 loc) · 11.7 KB
/
LAnn2Triple.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
#!/usr/bin/python
#coding=utf8
"""
# Author: hu-tom
# Created Time : 2020-02-24 18:46:36
# File Name: LAn2Triple.py
# Description:
"""
import os
import sys
from pylab import *
result='result/'
if not os.path.isdir(result):
os.mkdir(result)
fs=os.listdir()
txts=[i for i in fs if i[-5:]=='.lann']
datas=[]
labels=[]
count=0
triple_all_count = 0
datas=[]
relation_labels=[]
entity_type=[]
relation2delete = [
'list中添加临时跳过的关系',
]
skiped_count = 0
relation2delete = ['关系_'+l for l in relation2delete]
def find_end_and_type(start,labels):
assert start+1 < len(labels),f'{start+1},{len(labels)}'
for i,l in enumerate(labels[start+1:]):
# print(tokens[start+i],labels[start],l)
if l[0] in ['B','O']:
return start+i,labels[start][2:]
def find_token_start(start,tokens):
for i,t in enumerate(tokens[start::-1]):
if t in ['。','_换行符_']:
return start-i+1
return 0
def find_token_end(start,tokens):
for i,t in enumerate(tokens[start:]):
if t in ['。','_换行符_']:
return start+i+1
for txt in txts:
# labels.append(txt[:-4])
with open(txt,'r',encoding='utf8') as f:
print(txt)
datas_tmp={}
tokens=[]
labels=[]
spos_all=[]
spos_with_range=[]
content=f.read().splitlines()
count_tmp=0
for i,sample in enumerate(content):
if sample.strip()=="":
continue
c,l,spo=sample.split('\t')
tokens.append(c)
labels.append(l)
if spo=='X':
continue
if ';' in spo:
sposs=spo.split(';')
triple_all_count += len(sposs)
for spos in sposs:
if spos.split('>')[1] in relation2delete:
skiped_count +=1
continue
spos_all.append(spos)
count_tmp+=1
else:
triple_all_count += 1
if spo.split('>')[1] in relation2delete:
skiped_count +=1
continue
spos_all.append(spo)
count_tmp+=1
for spo in spos_all:
# print(f'============ {spo} ===============')
s,p,o=spo.split('>')
es,ts=find_end_and_type(int(s),labels)
if tokens[es] in [' ','_换行符_','。','.']:
print('?',txt,es,tokens[es])
print('该处实体结束位置可能不正确!')
eo,to=find_end_and_type(int(o),labels)
if tokens[eo] in [' ','_换行符_','。','.']:
print('?',txt,eo,tokens[eo])
print('该处实体结束位置可能不正确!')
if p[0]!='关':
try:
p=labels[int(p)][2:]
except:
print(txt,spo)
import sys;sys.exit(0)
# print(p)
if p not in relation_labels:
relation_labels.append(p)
if ts not in entity_type:
entity_type.append(ts)
if to not in entity_type:
entity_type.append(to)
if '' in entity_type:
print('Error !')
print(txt,spo,'序列标注存在问题:类型为空白!')
sys.exit(0)
lb=min(int(s),int(o))
ub=max(es,eo)
tokens_start=find_token_start(lb,tokens)
tokens_end=find_token_end(ub,tokens)
flag=False
# print(spo,lb,ub,tokens_start,tokens_end)
if len(list(datas_tmp.keys()))>0:
# print('list(datas_tmp.keys())={}'.format((list(datas_tmp.keys()))))
for k in datas_tmp.keys():
assert 'location' in list(datas_tmp[k].keys()),f'No location {k},{txt},{list(datas_tmp.keys())}'
# print(datas_tmp[k])
if tokens_start>=k[0] and tokens_end<=k[1]:#新token范围更小
# print('='*40)
# print('新token范围更小')
# print(tokens_start,tokens_end)
# print(k)
datas_tmp[k]['spo_list'].append((''.join(tokens[int(s):es+1]),p,''.join(tokens[int(o):eo+1])))
datas_tmp[k]['spo_details'].append([int(s),es,ts,p,int(o),eo,to])
flag=True
break
elif tokens_start<=k[0] and tokens_end>=k[1]:#新token范围更大
# print('='*40)
# print('新token范围更大')
# print(tokens_start,tokens_end)
# print(k)
datas_tmp[(tokens_start,tokens_end)]={}
datas_tmp[(tokens_start,tokens_end)]['tokens']=tokens[tokens_start:tokens_end]
datas_tmp[(tokens_start,tokens_end)]['spo_list']=datas_tmp[k]['spo_list']+[(''.join(tokens[int(s):es+1]),p,''.join(tokens[int(o):eo+1]))]
datas_tmp[(tokens_start,tokens_end)]['spo_details']=datas_tmp[k]['spo_details']+[[int(s),es,ts,p,int(o),eo,to]]
datas_tmp.pop(k)
datas_tmp[(tokens_start,tokens_end)]['location']= f'{txt}:{tokens_start}'
flag=True
break
elif tokens_start<=k[0] and tokens_end>k[0] and tokens_end<=k[1]:#新token左边范围更大,重复部分右范围
# print('='*40)
# print(tokens_start,tokens_end)
# print(k)
# print('新token左边范围更大,重复部分右范围')
datas_tmp[(tokens_start,k[1])]['tokens']={}
datas_tmp[(tokens_start,k[1])]['tokens']=tokens[tokens_start:k[1]]
datas_tmp[(tokens_start,k[1])]['spo_list']=datas_tmp[k]['spo_list']+[(''.join(tokens[int(s):es+1]),p,''.join(tokens[int(o):eo+1]))]
datas_tmp[(tokens_start,k[1])]['spo_details']=datas_tmp[k]['spo_details']+[[int(s),es,ts,p,int(o),eo,to]]
datas_tmp.pop(k)
datas_tmp[(tokens_start,k[1])]['location']= f'{txt}:{tokens_start}'
flag=True
break
elif tokens_start>=k[0] and tokens_start<=k[1] and tokens_end>=k[1]:#新token右边范围更大,重复部分左范围
# print('='*40)
# print('新token右边范围更大,重复部分左范围')
# print(tokens_start,tokens_end)
# print(k)
datas_tmp[(k[0],tokens_end)]={}
datas_tmp[(k[0],tokens_end)]['tokens']=tokens[k[0]:tokens_end]
datas_tmp[(k[0],tokens_end)]['spo_list']=datas_tmp[k]['spo_list']+[(''.join(tokens[int(s):es+1]),p,''.join(tokens[int(o):eo+1]))]
datas_tmp[(k[0],tokens_end)]['spo_details']=datas_tmp[k]['spo_details']+[[int(s),es,ts,p,int(o),eo,to]]
datas_tmp.pop(k)
datas_tmp[(k[0],tokens_end)]['location']= f'{txt}:{k[0]}'
flag=True
break
elif tokens_start>k[1] and k==list(datas_tmp.keys())[-1]:
# print('='*40)
# print('新范围的token')
# print(tokens_start,tokens_end)
# print(k)
datas_tmp[(tokens_start,tokens_end)]={'tokens':tokens[tokens_start:tokens_end],'spo_list':[(''.join(tokens[int(s):es+1]),p,''.join(tokens[int(o):eo+1]))],'spo_details':[[int(s),es,ts,p,int(o),eo,to]]}
flag=True
datas_tmp[(tokens_start,tokens_end)]['location']= f'{txt}:{tokens_start}'
break
# print('xxx'*30)
# print('查找完毕')
if not flag:#没找到之前合适的区间,新生成区间
# print('n'*30)
# print('没找到之前合适的区间,新生成区间')
datas_tmp[(tokens_start,tokens_end)]={'tokens':tokens[tokens_start:tokens_end],'spo_list':[(''.join(tokens[int(s):es+1]),p,''.join(tokens[int(o):eo+1]))],'spo_details':[[int(s),es,ts,p,int(o),eo,to]]}
datas_tmp[(tokens_start,tokens_end)]['location']= f'{txt}:{tokens_start}'
if(len(datas_tmp.keys())==0):
continue
keys=list(datas_tmp.keys())
for k in keys:
for i,spos in enumerate(datas_tmp[k]['spo_details']):
for j in [0,1,4,5]:
# print('-'*30)
# print(k[0])
# print(datas_tmp[k]['spo_details'][i][j])
datas_tmp[k]['spo_details'][i][j]-=k[0]
# print(datas_tmp[k]['spo_details'][i][j])
vv = list(datas_tmp.values())#该语料文件所有起始区间对应的三元组数据
for v in vv:#数据完整性检查
# print(v)
tokens_check = v['tokens']
spo_list_check = v['spo_list']
spo_detail_check = v['spo_details']
for li,de in zip(spo_list_check,spo_detail_check):
# print(li,de)
e1 = li[0]
e2 = li[2]
se1 = de[0]
ee1 = de[1]
se2 = de[4]
ee2 = de[5]
# print(se1,ee1,se2,ee2,len(tokens_check))
# print(tokens_check[se1],tokens_check[ee1],tokens_check[se2],tokens_check[ee2])
c1 = tokens_check[se1]==e1[0] and \
tokens_check[ee1]==e1[-1] and \
tokens_check[se2]==e2[0] and \
tokens_check[ee2]==e2[-1]
assert c1==True,f"Error!!!!{v}{v['location']}"
datas+=list(datas_tmp.values())
count+=count_tmp
shuffle(datas)
def statistic(datas):
print('='*40)
statistic_result = {}
spo_count = 0
seq_len = 0
for d in datas:
spo_list = d['spo_list']
seq_len += len(d['tokens'])
spo_count += len(spo_list)
for spo in spo_list:
relation = spo[1][3:]
if relation not in statistic_result:
statistic_result[relation] = 0
statistic_result[relation] += 1
for k,v in statistic_result.items():
print('\t',k,v)
print(f'\t共有{spo_count}个三元组')
print(f'\t平均长度:{seq_len/spo_count}')
cut_index = int(0.75*len(datas))
train_datas = datas[0:cut_index]
test_datas = datas[cut_index:]
statistic(train_datas)
statistic(test_datas)
print('='*40)
print('数据集整体信息:')
print('\t共{}个三元组'.format(triple_all_count))
print('\t剩下{}个三元组'.format(count))
print('\t去掉{}个三元组'.format(skiped_count))
print('\t共{}个样本'.format(len(datas)))
print(f'\t共{len(train_datas)}个训练样本,共{len(test_datas)}个测试样本')
print(f'\t共{len(entity_type)}个实体类型:{entity_type}')
print(f'\t共{len(relation_labels)}个关系类型:{relation_labels}')
relation_labels = sorted(relation_labels)
entity_type = sorted(entity_type)
with open('result/datasets.txt','w',encoding='utf8') as f:
f.write(str(datas))
with open(result+'train_datas.txt','w',encoding='utf8') as f:
f.write(str(train_datas))
with open(result+'test_datas.txt','w',encoding='utf8') as f:
f.write(str(test_datas))
with open(result+'relation_labels.txt','w',encoding='utf8') as f:
f.write(str(relation_labels))
with open(result+'entity_type.txt','w',encoding='utf8') as f:
f.write(str(entity_type))