-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
153 lines (132 loc) · 4.98 KB
/
demo.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
import json
def make_key(holder_inds, target_inds):
key = ""
key += str(holder_inds) + "; "
key += str(target_inds)
return key
def convert_to_str(list):
str = ""
i = 0
for token in list:
if token is not "'" and token is not "." and token is not "\"\"" and token is not "," and token is not "''":
str += " " + token
else:
str += token
i += 1
return str
def convert_to_list(str):
str = str[1:len(str) - 1]
list = []
element = ""
for char in str:
if char == ",":
list.append(element)
element = ""
elif char != " ":
element += char
list.append(element)
return list
docs_to_data = {}
docs_to_num_right_wrong = {}
with open("./error_analysis/final/right_docs_dev.json", "r", encoding="latin1") as rf:
for line in rf:
annot = json.loads(line)
if annot["docid"] not in docs_to_data:
docs_to_data[annot["docid"]] = []
docs_to_num_right_wrong[annot["docid"]] = {"right": 0, "wrong": 0}
docs_to_data[annot["docid"]].append(annot)
if annot["actual"] != 1:
docs_to_num_right_wrong[annot["docid"]]["right"] += 1
with open("./error_analysis/final/wrong_docs_dev.json", "r", encoding="latin1") as rf:
for line in rf:
annot = json.loads(line)
if annot["docid"] not in docs_to_data:
docs_to_data[annot["docid"]] = []
docs_to_num_right_wrong[annot["docid"]] = {"right": 0, "wrong": 0}
docs_to_data[annot["docid"]].append(annot)
if annot["actual"] != 1:
docs_to_num_right_wrong[annot["docid"]]["wrong"] += 1
doc_to_ht_to_preds = {}
doc_to_ht_to_acts = {}
for doc in docs_to_data:
annots = docs_to_data[doc]
for annot in annots:
ht_key = make_key(annot["holders"], annot["targets"])
probs = str(annot["probabilities"])
doc_to_ht_to_preds[ht_key] = probs
doc_to_ht_to_acts[ht_key] = annot["actual"]
docs_to_use = []
for doc in sorted(docs_to_num_right_wrong.items(), key=lambda x: x[1]["wrong"], reverse=False):
docs_to_use.append(doc)
if len(docs_to_use) > 30:
break
doc_to_ht_to_annot = {}
doc_to_entity = {}
with open("./data/new_annot/none/acl_dev_eval_new.json", "r", encoding="latin1") as rf:
for line in rf:
annot = json.loads(line)
docid = annot["docid"]
if docid not in doc_to_ht_to_annot:
doc_to_ht_to_annot[docid] = {}
doc_to_entity[docid] = {}
ht_key = make_key(annot["holder_index"], annot["target_index"])
if ht_key not in doc_to_ht_to_annot[docid]:
doc_to_ht_to_annot[docid][ht_key] = annot
holder = str(annot["holder"])
if holder not in doc_to_entity[docid]:
doc_to_entity[docid][holder] = annot["holder_index"]
target = str(annot["target"])
if target not in doc_to_entity[docid]:
doc_to_entity[docid][target] = annot["target_index"]
text_to_docid = {}
text_to_ht_to_pred = {}
text_to_ht_to_acts = {}
text_to_entity = {}
texts_ht_preds = []
for doc in docs_to_use:
docid = doc[0]
ht_to_annot = doc_to_ht_to_annot[docid]
for ht in ht_to_annot:
pred = doc_to_ht_to_preds[ht]
act = doc_to_ht_to_acts[ht]
token = doc_to_ht_to_annot[docid][ht]["token"]
if docid == "AFP_ENG_20101123.0339":
for entity in doc_to_entity[docid]:
for indices in doc_to_entity[docid][entity]:
token[indices[0]] = "{" + token[indices[0]]
token[indices[1]] = token[indices[0]] + "}_" + str(entity)
holder = doc_to_ht_to_annot[docid][ht]["holder"]
target = doc_to_ht_to_annot[docid][ht]["target"]
text = convert_to_str(token)
if text not in text_to_ht_to_pred:
text_to_ht_to_pred[text] = {}
text_to_ht_to_acts[text] = {}
text_to_entity[text] = doc_to_entity[docid]
text_to_docid[text] = docid
text_to_ht_to_pred[text]["(\"" + str(holder) + "\", \"" + str(target) + "\")"] = pred
text_to_ht_to_acts[text]["(\"" + str(holder) + "\", \"" + str(target) + "\")"] = act
# AFP_ENG_20101123.0339
# AFP_ENG_20100322.0387
# APW_ENG_20090729.0699
# XIN_ENG_20100125.0144
# XIN_ENG_20090902.0333
def main():
print(docs_to_use)
print()
for text in text_to_ht_to_pred:
print(text_to_docid[text])
print(text)
print(text_to_entity[text])
ht_to_pred = text_to_ht_to_pred[text]
for ht in ht_to_pred:
pred_label = 0
prob_list = convert_to_list(ht_to_pred[ht])
for i in range(3):
if float(prob_list[i]) > float(prob_list[pred_label]):
pred_label = i
act_label = text_to_ht_to_acts[text][ht]
if pred_label != 1:
print(" " + str(ht) + ", ") # + ": " + str(prob_list) + " " + str(pred_label) + " " + str(act_label))
print()
if __name__ == "__main__":
main()