-
Notifications
You must be signed in to change notification settings - Fork 30
/
CalculateGenericRate.py
273 lines (251 loc) · 11 KB
/
CalculateGenericRate.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
import json
import pdb
from BuildKG import Map2doc
from relation_metrics import span_metric
def ReadJson(senfn):
docs = {}
with open(senfn) as f:
docs_sent = [json.loads(jsonline) for jsonline in f.readlines()]
for i in range(len(docs_sent)):
docs[docs_sent[i]['doc_key']] = docs_sent[i]
if 'relations' in docs[docs_sent[i]['doc_key']]:
docs[docs_sent[i]['doc_key']]['relation'] = flat(docs[docs_sent[i]['doc_key']]['relations'])
docs[docs_sent[i]['doc_key']]['ner'] = GetNER(docs[docs_sent[i]['doc_key']]['ner'])
# Map2doc(docs[docs_sent[i]['doc_key']])
return docs
def GetNER(lst):
NERdir = {}
for sent in lst:
for ent in sent:
NERdir[tuple(ent[:2])] = ent[-1]
return NERdir
def flat(lst):
new_lst = []
for ele in lst:
new_lst += ele
return new_lst
def ReadJsonPred(senfn, truedocs):
docs = {}
with open(senfn) as f:
docs_sent = [json.loads(jsonline) for jsonline in f.readlines()]
for i in range(len(docs_sent)):
docs[docs_sent[i]['doc_key']] = docs_sent[i]
if 'relations' in docs[docs_sent[i]['doc_key']]:
docs[docs_sent[i]['doc_key']]['relation'] = docs[docs_sent[i]['doc_key']]['relations']
docs[docs_sent[i]['doc_key']]['sentences'] = truedocs[docs_sent[i]['doc_key']]['sentences']
docs[docs_sent[i]['doc_key']]['ner'] = [[] for shit in range(len(docs[docs_sent[i]['doc_key']]['sentences']))]
Map2doc(docs[docs_sent[i]['doc_key']])
return docs
# def Aspect
def GetRelCoref(true_docs, gold_docs,aspect):
true_rels = []
for doc_key in true_docs:
nerdir = gold_docs[doc_key]['ner']
sentences = true_docs[doc_key]['sentences']
corefs = gold_docs[doc_key]['clusters']
coref_set = set()
for cluster in corefs:
for span in cluster:
coref_set.add(tuple(span))
for relation in true_docs[doc_key]['relation']:
span0 = tuple(relation[:2])
span1 = tuple(relation[2:4])
rel = relation[-1]
key = False
phrase1 = ' '.join(sentences[span0[0]:(span0[1]+1)])
phrase2 = ' '.join(sentences[span1[0]:(span1[1]+1)])
if span0 in coref_set or span1 in coref_set:
key = True
# if span0 in nerdir and nerdir[span0] == aspect:
# key = True
# print phrase1
# if span1 in nerdir and nerdir[span1] == aspect:
# key = True
# print phrase2
if key:
# pdb.set_trace()
# print phrase1 + '\t' + rel + '\t' + phrase2
# print phrase1
# print phrase2
relation_token = [[doc_key + str(span0[0]) + '_' + str(span0[1]) , doc_key + str(span1[0]) + '_' + str(span1[1])], rel]
true_rels.append(relation_token)
return true_rels
def GetRel(true_docs, gold_docs,aspect):
true_rels = []
for doc_key in true_docs:
nerdir = gold_docs[doc_key]['ner']
sentences = true_docs[doc_key]['sentences']
for relation in true_docs[doc_key]['relation']:
span0 = tuple(relation[:2])
span1 = tuple(relation[2:4])
rel = relation[-1]
key = False
phrase1 = ' '.join(sentences[span0[0]:(span0[1]+1)])
phrase2 = ' '.join(sentences[span1[0]:(span1[1]+1)])
if span0 in nerdir and nerdir[span0] == aspect:
key = True
print phrase1
if span1 in nerdir and nerdir[span1] == aspect:
key = True
print phrase2
if key:
# pdb.set_trace()
# print phrase1 + '\t' + rel + '\t' + phrase2
# print phrase1
# print phrase2
relation_token = [[doc_key + str(span0[0]) + '_' + str(span0[1]) , doc_key + str(span1[0]) + '_' + str(span1[1])], rel]
true_rels.append(relation_token)
return true_rels
def GetRelSpan(true_docs, gold_docs,aspect):
true_rels = []
for doc_key in true_docs:
nerdir = gold_docs[doc_key]['ner']
sentences = true_docs[doc_key]['sentences']
for relation in true_docs[doc_key]['relation']:
span0 = tuple(relation[:2])
span1 = tuple(relation[2:4])
rel = relation[-1]
key = True
phrase1 = ' '.join(sentences[span0[0]:(span0[1]+1)])
phrase2 = ' '.join(sentences[span1[0]:(span1[1]+1)])
if span0 in nerdir and nerdir[span0] == aspect:
key = True
print phrase1
if span1 in nerdir and nerdir[span1] == aspect:
key = True
print phrase2
if doc_key == 'IJCAI_2016_413_abs':
# pdb.set_trace()
print phrase1 + '\t' + rel + '\t' + phrase2
if span0[0] > span1[0]:
span0, span1 = span1, span0
relation_token = [[doc_key + str(span0[0]) + '_' + str(span0[1]) , doc_key + str(span1[0]) + '_' + str(span1[1])], 'REL']
true_rels.append(relation_token)
return true_rels
def GetRelPhrase(true_docs, gold_docs,aspect, phraseset):
true_rels = []
for doc_key in true_docs:
nerdir = gold_docs[doc_key]['ner']
sentences = true_docs[doc_key]['sentences']
for relation in true_docs[doc_key]['relation']:
span0 = tuple(relation[:2])
span1 = tuple(relation[2:4])
rel = relation[-1]
key = False
# key = True
phrase1 = ' '.join(sentences[span0[0]:(span0[1]+1)])
phrase2 = ' '.join(sentences[span1[0]:(span1[1]+1)])
# if phrase1 in phraseset or phrase2 in phraseset:
# key = True
if phrase1.isupper():
# print phrase1
key = True
if phrase2.isupper():
# print phrase2
key = True
# if span0 in nerdir and nerdir[span0] == aspect:
# key = True
# print phrase1
# if span1 in nerdir and nerdir[span1] == aspect:
# key = True
# print phrase2
if key:
# pdb.set_trace()
# print phrase1 + '\t' + rel + '\t' + phrase2
# print phrase1
# print phrase2
# print phrase1, phrase2
relation_token = [[doc_key + str(span0[0]) + '_' + str(span0[1]) , doc_key + str(span1[0]) + '_' + str(span1[1])], rel]
true_rels.append(relation_token)
return true_rels
def TrueSet(true_docs, gold_docs,aspect):
true_rels = []
for doc_key in true_docs:
nerdir = gold_docs[doc_key]['ner']
sentences = true_docs[doc_key]['sentences']
for relation in true_docs[doc_key]['relation']:
span0 = tuple(relation[:2])
span1 = tuple(relation[2:4])
rel = relation[-1]
key = True
# key = True
phrase1 = ' '.join(sentences[span0[0]:(span0[1]+1)])
phrase2 = ' '.join(sentences[span1[0]:(span1[1]+1)])
# if phrase1 in phraseset or phrase2 in phraseset:
# key = True
if phrase1.isupper():
# print phrase1
key = True
if phrase2.isupper():
# print phrase2
key = True
if key:
relation_token = doc_key + str(span0[0]) + '_' + str(span0[1]) +'_'+ doc_key + str(span1[0]) + '_' + str(span1[1]) +'_' + rel
true_rels.append(relation_token)
return true_rels
def PrintError(true_docs, gold_docs,aspect, trueset):
true_rels = []
pronoun = ['this', 'it', 'former', 'latter', 'they','those','It','alternative']
for doc_key in true_docs:
nerdir = gold_docs[doc_key]['ner']
sentences = true_docs[doc_key]['sentences']
for relation in true_docs[doc_key]['relation']:
span0 = tuple(relation[:2])
span1 = tuple(relation[2:4])
rel = relation[-1]
key = True
# key = True
phrase1 = ' '.join(sentences[span0[0]:(span0[1]+1)])
phrase2 = ' '.join(sentences[span1[0]:(span1[1]+1)])
# if phrase1 in phraseset or phrase2 in phraseset:
# key = True
if phrase1.isupper():
# print phrase1
key = True
if phrase2.isupper():
# print phrase2
key = True
# if phrase1 in set(pronoun) or phrase2 in set(pronoun):
# key = True
if key:
relation_token = doc_key + str(span0[0]) + '_' + str(span0[1]) +'_'+ doc_key + str(span1[0]) + '_' + str(span1[1]) +'_' + rel
if relation_token not in trueset:
print doc_key
print phrase1 +'\t'+ phrase2 +'\t'+ rel
return true_rels
predfn = '/home/yiluan/lsgn_cleanup/dev.output_nocoref.json'
# predfn = '/home/yiluan/lsgn_cleanup/dev.output.json'
truefn = './ScienceKG_dev.noreverse.json'
true_docs = ReadJson(truefn)
pred_docs = ReadJsonPred(predfn, true_docs)
for key in true_docs:
true_docs[key]['sentences'] = pred_docs[key]['sentences']
# pdb.set_trace()
# aspects = ["Task", "Generic", "Metric", "Material", "OtherScientificTerm", "Method"]
aspects = ['Generic']
pronoun = ['this', 'it', 'former', 'latter', 'they','those','It','alternative']
# for aspect in aspects:
# true_rel = GetRel(true_docs, true_docs,aspect)
# pred_rel = GetRel(pred_docs, true_docs,aspect)
# print aspect
# print len(true_rel)
# print span_metric(true_rel, pred_rel)
# for aspect in aspects:
# true_rel = GetRelPhrase(true_docs, true_docs,aspect, set(pronoun))
# pred_rel = GetRelPhrase(pred_docs, true_docs,aspect, set(pronoun))
# print aspect
# print len(true_rel)
# print span_metric(true_rel, pred_rel)
# for aspect in aspects:
# true_rel = TrueSet(pred_docs, true_docs,aspect)
# pred_rel = PrintError(true_docs, true_docs,aspect, true_rel)
# for aspect in aspects:
# true_rel = GetRelCoref(true_docs, true_docs,aspect)
# pred_rel = GetRelCoref(pred_docs, true_docs,aspect)
# print aspect
# print len(true_rel)
# print span_metric(true_rel, pred_rel)
aspect = 'True'
# true_rel = GetRelSpan(true_docs, true_docs,aspect)
pred_rel = GetRelSpan(pred_docs, true_docs,aspect)
# print span_metric(true_rel, pred_rel)