-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathutils.py
111 lines (89 loc) · 3.48 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
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
import logging
import pickle
import time
from collections import defaultdict, deque
def get_logger(dataset):
pathname = "./log/{}_{}.txt".format(dataset, time.strftime("%m-%d_%H-%M-%S"))
logger = logging.getLogger()
logger.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s - %(levelname)s: %(message)s",
datefmt='%Y-%m-%d %H:%M:%S')
file_handler = logging.FileHandler(pathname)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
stream_handler.setFormatter(formatter)
logger.addHandler(file_handler)
logger.addHandler(stream_handler)
return logger
def save_file(path, data):
with open(path, "wb") as f:
pickle.dump(data, f)
def load_file(path):
with open(path, "rb") as f:
data = pickle.load(f)
return data
def convert_index_to_text(index, type):
text = "-".join([str(i) for i in index])
text = text + "-#-{}".format(type)
return text
def convert_text_to_index(text):
index, type = text.split("-#-")
index = [int(x) for x in index.split("-")]
return index, int(type)
def decode(outputs, entities, length):
class Node:
def __init__(self):
self.THW = [] # [(tail, type)]
self.NNW = defaultdict(set) # {(head,tail): {next_index}}
ent_r, ent_p, ent_c = 0, 0, 0
decode_entities = []
q = deque()
for instance, ent_set, l in zip(outputs, entities, length):
predicts = []
nodes = [Node() for _ in range(l)]
for cur in reversed(range(l)):
heads = []
for pre in range(cur+1):
# THW
if instance[cur, pre] > 1:
nodes[pre].THW.append((cur, instance[cur, pre]))
heads.append(pre)
# NNW
if pre < cur and instance[pre, cur] == 1:
# cur node
for head in heads:
nodes[pre].NNW[(head,cur)].add(cur)
# post nodes
for head,tail in nodes[cur].NNW.keys():
if tail >= cur and head <= pre:
nodes[pre].NNW[(head,tail)].add(cur)
# entity
for tail,type_id in nodes[cur].THW:
if cur == tail:
predicts.append(([cur], type_id))
continue
q.clear()
q.append([cur])
while len(q) > 0:
chains = q.pop()
for idx in nodes[chains[-1]].NNW[(cur,tail)]:
if idx == tail:
predicts.append((chains + [idx], type_id))
else:
q.append(chains + [idx])
predicts = set([convert_index_to_text(x[0], x[1]) for x in predicts])
decode_entities.append([convert_text_to_index(x) for x in predicts])
ent_r += len(ent_set)
ent_p += len(predicts)
ent_c += len(predicts.intersection(ent_set))
return ent_c, ent_p, ent_r, decode_entities
def cal_f1(c, p, r):
if r == 0 or p == 0:
return 0, 0, 0
r = c / r if r else 0
p = c / p if p else 0
if r and p:
return 2 * p * r / (p + r), p, r
return 0, p, r