-
Notifications
You must be signed in to change notification settings - Fork 59
/
document.py
165 lines (142 loc) · 5.24 KB
/
document.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
import utils
import directories
import timer
from collections import defaultdict
import numpy as np
import evaluation
from collections import Counter
class Document:
def __init__(self, did, mentions, gold, mention_to_gold):
self.did = did
self.mentions = mentions
self.gold = gold
self.mention_to_gold = {m: tuple(g) for m, g in mention_to_gold.items()}
self.reset()
def reset(self):
self.clusters = []
self.mention_to_cluster = {}
self.rs = {}
self.ps = {}
self.ana_to_ant = {}
self.ant_to_anas = {}
for m in self.mentions:
c = (m,)
self.mention_to_cluster[m] = c
self.clusters.append(c)
self.rs[m] = 0
self.ps[m] = 0
self.ana_to_ant[m] = -1
self.ant_to_anas[m] = []
self.p_num = self.r_num = self.p_den = 0
self.r_den = sum(len(g) for g in self.gold)
def get_f1(self, beta=1):
return evaluation.f1(self.p_num, self.p_den, self.r_num, self.r_den, beta=beta)
def update_b3(self, c, hypothetical=False):
timer.start("update b3")
if len(c) == 1:
self.p_den -= 1
self.p_num -= self.ps[c[0]]
self.r_num -= self.rs[c[0]]
self.ps[c[0]] = 0
self.rs[c[0]] = 0
else:
intersect_counts = Counter()
for m in c:
if m in self.mention_to_gold:
intersect_counts[self.mention_to_gold[m]] += 1
for m in c:
if m in self.mention_to_gold:
self.p_num -= self.ps[m]
self.r_num -= self.rs[m]
g = self.mention_to_gold[m]
ic = intersect_counts[g]
self.p_num += ic / float(len(c))
self.r_num += ic / float(len(g))
if not hypothetical:
self.ps[m] = ic / float(len(c))
self.rs[m] = ic / float(len(g))
timer.stop("update b3")
def link(self, m1, m2, hypothetical=False, beta=1):
timer.start("link")
if m1 == -1:
return self.get_f1(beta=beta) if hypothetical else None
c1, c2 = self.mention_to_cluster[m1], self.mention_to_cluster[m2]
assert c1 != c2
new_c = c1 + c2
p_num, r_num, p_den, r_den = self.p_num, self.r_num, self.p_den, self.r_den
if len(c1) == 1:
self.p_den += 1
if len(c2) == 1:
self.p_den += 1
self.update_b3(new_c, hypothetical=hypothetical)
if hypothetical:
f1 = evaluation.f1(self.p_num, self.p_den, self.r_num, self.r_den, beta=beta)
self.p_num, self.r_num, self.p_den, self.r_den = p_num, r_num, p_den, r_den
timer.stop("link")
return f1
else:
self.ana_to_ant[m2] = m1
self.ant_to_anas[m1].append(m2)
self.clusters.remove(c1)
self.clusters.remove(c2)
self.clusters.append(new_c)
for m in new_c:
self.mention_to_cluster[m] = new_c
timer.stop("link")
def unlink(self, m):
timer.start("unlink")
old_ant = self.ana_to_ant[m]
if old_ant != -1:
self.ana_to_ant[m] = -1
self.ant_to_anas[old_ant].remove(m)
old_c = self.mention_to_cluster[m]
c1 = [m]
frontier = self.ant_to_anas[m][:]
while len(frontier) > 0:
m = frontier.pop()
c1.append(m)
frontier += self.ant_to_anas[m]
c1 = tuple(c1)
c2 = tuple(m for m in old_c if m not in c1)
self.update_b3(c1)
self.update_b3(c2)
self.clusters.remove(old_c)
self.clusters.append(c1)
self.clusters.append(c2)
for m in c1:
self.mention_to_cluster[m] = c1
for m in c2:
self.mention_to_cluster[m] = c2
timer.stop("unlink")
def load_gold(dataset_name):
gold = {}
mention_to_gold = {}
for doc_gold in utils.load_json_lines(directories.GOLD + dataset_name):
did = int(list(doc_gold.keys())[0])
gold[did] = doc_gold[str(did)]
mention_to_gold[did] = {}
for gold_cluster in doc_gold[str(did)]:
for m in gold_cluster:
mention_to_gold[did][m] = tuple(gold_cluster)
return gold, mention_to_gold
def load_mentions(dataset_name):
mentions = defaultdict(list)
mention_ids = np.load(directories.MENTION_DATA + dataset_name + '/mid.npy')
doc_ids = np.load(directories.MENTION_DATA + dataset_name + '/mdid.npy')
for did, mid in zip(doc_ids[:, 0], mention_ids[:, 0]):
mentions[did].append(mid)
return mentions
def write_docs(dataset_name):
gold, mention_to_gold = load_gold(dataset_name)
mentions = load_mentions(dataset_name)
docs = []
for did in gold:
docs.append(Document(did, mentions[did],
gold[did], mention_to_gold[did]))
utils.write_pickle(docs, directories.DOCUMENTS + dataset_name + '_docs.pkl')
def main():
write_docs("train")
write_docs("dev")
write_docs("test")
if __name__ == '__main__':
main()