-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
50 lines (43 loc) · 1.87 KB
/
utils.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
from torch.autograd import Variable
import torch
from const import Config
import json
config=Config()
def convert_long_tensor(var,use_cuda):
var=torch.LongTensor(var)
if use_cuda:
var=var.cuda(async=True)
return var
def convert_long_variable(var,use_cuda):
return Variable(convert_long_tensor(var,use_cuda))
def calculate(x,y,id2word,id2tag):
'''
{"sentence": ["陈", "明", "亮", "又", "哭", "又", "闹", ",", "但", "仍", "无", "济", "于", "事", "。"],
"tags": ["B-PER", "I-PER", "I-PER", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O", "O"]}
'''
entity=[]
res=[]
for j in range(len(x)):
if x[j]==0 or y[j]==0:
continue
if j+1<len(x) and id2tag[y[j]][0]=='B' and id2tag[y[j+1]][0]=='I':
entity=[id2word[x[j]]+'/'+id2tag[y[j]]]
elif j+1<len(x) and id2tag[y[j]][0]=='I' and len(entity)!=0 and id2tag[y[j+1]][0]=='I' and entity[-1].split('/')[1][1:]==id2tag[y[j]][1:]:
entity.append(id2word[x[j]]+'/'+id2tag[y[j]])
elif j+1<len(x) and id2tag[y[j]][0]=='I' and len(entity)!=0 and id2tag[y[j+1]][0]=='O' and entity[-1].split('/')[1][1:]==id2tag[y[j]][1:]:
entity.append(id2word[x[j]]+'/'+id2tag[y[j]])
res.append(entity)
entity=[]
elif j+1==len(x) and id2tag[y[j]][0]=='I' and len(entity)!=0 and entity[-1].split('/')[1][1:]==id2tag[y[j]][1:]:
entity.append(id2word[x[j]] + '/' + id2tag[y[j]])
res.append(entity)
entity = []
elif j+1<len(x) and id2tag[y[j]][0]=='B' and (id2tag[y[j+1]][0]=='O' or id2tag[y[j+1]][0]=='B'):
res.append([id2word[x[j]]+'/'+id2tag[y[j]]])
entity=[]
elif j+1==len(x) and id2tag[y[j]][0]=='B':
res.append([id2word[x[j]] + '/' + id2tag[y[j]]])
entity = []
else:
entity=[]
return res