Skip to content

Commit b318d63

Browse files
authored
Add files via upload
1 parent fb757a7 commit b318d63

File tree

3 files changed

+461
-0
lines changed

3 files changed

+461
-0
lines changed

Code/ED/const.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
EVENT_TYPE = [
2+
"Movement.Transport",
3+
"Personnel.Elect",
4+
"Personnel.Start-Position",
5+
"Personnel.Nominate",
6+
"Personnel.End-Position",
7+
"Conflict.Attack",
8+
"Conflict.Demonstrate",
9+
"Contact.Phone-Write",
10+
"Contact.Meet",
11+
"Transaction.Transfer-Money",
12+
"Transaction.Transfer-Ownership",
13+
"Business.Start-Org",
14+
"Business.Merge-Org",
15+
"Business.Declare-Bankruptcy",
16+
"Business.End-Org",
17+
"Life.Be-Born",
18+
"Life.Injure",
19+
"Life.Die",
20+
"Life.Marry",
21+
"Life.Divorce",
22+
"Justice.Sue",
23+
"Justice.Arrest-Jail",
24+
"Justice.Execute",
25+
"Justice.Charge-Indict",
26+
"Justice.Convict",
27+
"Justice.Trial-Hearing",
28+
"Justice.Sentence",
29+
"Justice.Release-Parole",
30+
"Justice.Fine",
31+
"Justice.Pardon",
32+
"Justice.Appeal",
33+
"Justice.Extradite",
34+
"Justice.Acquit"
35+
]
36+
37+
ENTITY = ['PER', 'ORG', 'GPE', 'LOC', 'FAC', 'VEH', 'WEA']
38+
39+
ROLE = ['Org', 'Place', 'Instrument', 'Vehicle', 'Attacker', 'Prosecutor', 'Agent', 'Victim', 'Origin', 'Target', 'Giver', 'Seller', 'Defendant', 'Recipient', 'Entity', 'Plaintiff', 'Person', 'Artifact', 'Destination', 'Adjudicator', 'Beneficiary', 'Buyer']
40+
41+
42+
All_Valid_EntTypes = {
43+
('Movement.Transport', 'Vehicle'): {'VEH'},
44+
('Movement.Transport', 'Artifact'): {'VEH', 'PER', 'WEA'},
45+
('Movement.Transport', 'Destination'): {'GPE', 'FAC', 'LOC'},
46+
('Personnel.Elect', 'Person'): {'PER'},
47+
('Movement.Transport', 'Agent'): {'ORG', 'GPE', 'PER'},
48+
('Personnel.Start-Position', 'Person'): {'PER'},
49+
('Personnel.Start-Position', 'Entity'): {'ORG', 'GPE'},
50+
('Personnel.Nominate', 'Person'): {'PER'},
51+
('Conflict.Attack', 'Place'): {'GPE', 'FAC', 'LOC'},
52+
('Personnel.End-Position', 'Entity'): {'ORG', 'GPE'},
53+
('Personnel.End-Position', 'Person'): {'PER'},
54+
('Contact.Meet', 'Entity'): {'ORG', 'GPE', 'PER'},
55+
('Contact.Meet', 'Place'): {'GPE', 'FAC', 'LOC'},
56+
('Life.Marry', 'Person'): {'PER'},
57+
('Personnel.Elect', 'Entity'): {'ORG', 'GPE', 'PER'},
58+
('Conflict.Attack', 'Target'): {'ORG', 'PER', 'VEH', 'FAC', 'LOC', 'WEA'},
59+
('Conflict.Attack', 'Attacker'): {'ORG', 'GPE', 'PER'},
60+
('Transaction.Transfer-Money', 'Giver'): {'ORG', 'GPE', 'PER'},
61+
('Transaction.Transfer-Money', 'Recipient'): {'ORG', 'GPE', 'PER'},
62+
('Conflict.Demonstrate', 'Entity'): {'ORG', 'PER'},
63+
('Conflict.Demonstrate', 'Place'): {'GPE', 'FAC', 'LOC'},
64+
('Business.End-Org', 'Place'): {'GPE', 'FAC'},
65+
('Justice.Sue', 'Plaintiff'): {'ORG', 'PER'},
66+
('Life.Injure', 'Victim'): {'PER'},
67+
('Life.Injure', 'Agent'): {'GPE', 'PER'},
68+
('Life.Die', 'Victim'): {'PER'},
69+
('Life.Die', 'Agent'): {'ORG', 'GPE', 'PER'},
70+
('Personnel.Start-Position', 'Place'): {'GPE', 'FAC'},
71+
('Life.Divorce', 'Place'): {'GPE', 'FAC', 'LOC'},
72+
('Life.Die', 'Place'): {'GPE', 'FAC', 'LOC'},
73+
('Justice.Arrest-Jail', 'Person'): {'PER'},
74+
('Justice.Arrest-Jail', 'Agent'): {'ORG', 'GPE', 'PER'},
75+
('Personnel.End-Position', 'Place'): {'GPE', 'FAC'},
76+
('Contact.Phone-Write', 'Entity'): {'ORG', 'PER'}, ('Life.Injure', 'Place'): {'GPE', 'FAC', 'LOC'}, ('Transaction.Transfer-Ownership', 'Buyer'): {'ORG', 'GPE', 'PER'}, ('Transaction.Transfer-Ownership', 'Artifact'): {'ORG', 'VEH', 'FAC', 'WEA'}, ('Transaction.Transfer-Ownership', 'Seller'): {'ORG', 'GPE', 'PER'}, ('Conflict.Attack', 'Instrument'): {'VEH', 'WEA'}, ('Life.Die', 'Instrument'): {'VEH', 'WEA'}, ('Justice.Arrest-Jail', 'Place'): {'GPE', 'FAC'}, ('Movement.Transport', 'Origin'): {'GPE', 'FAC', 'LOC'}, ('Business.End-Org', 'Org'): {'ORG'}, ('Life.Injure', 'Instrument'): {'VEH', 'WEA'}, ('Transaction.Transfer-Ownership', 'Place'): {'GPE', 'FAC', 'LOC'}, ('Transaction.Transfer-Ownership', 'Beneficiary'): {'GPE', 'PER'}, ('Justice.Execute', 'Place'): {'GPE', 'FAC'}, ('Justice.Execute', 'Agent'): {'ORG', 'GPE', 'PER'}, ('Conflict.Attack', 'Victim'): {'PER'}, ('Contact.Phone-Write', 'Place'): {'GPE', 'FAC', 'LOC'}, ('Justice.Trial-Hearing', 'Defendant'): {'ORG', 'PER'}, ('Justice.Execute', 'Person'): {'PER'}, ('Movement.Transport', 'Place'): {'GPE'}, ('Personnel.Elect', 'Place'): {'GPE', 'LOC'}, ('Life.Be-Born', 'Place'): {'GPE', 'FAC', 'LOC'}, ('Justice.Charge-Indict', 'Adjudicator'): {'ORG', 'PER'}, ('Business.Start-Org', 'Org'): {'ORG'}, ('Business.Start-Org', 'Place'): {'GPE', 'FAC'}, ('Justice.Convict', 'Defendant'): {'ORG', 'PER'}, ('Justice.Convict', 'Adjudicator'): {'ORG'}, ('Justice.Sentence', 'Defendant'): {'ORG', 'PER'}, ('Justice.Sentence', 'Adjudicator'): {'ORG', 'GPE', 'PER'}, ('Business.Declare-Bankruptcy', 'Org'): {'ORG', 'PER'}, ('Justice.Release-Parole', 'Entity'): {'ORG', 'GPE', 'PER'}, ('Justice.Release-Parole', 'Person'): {'PER'}, ('Justice.Charge-Indict', 'Defendant'): {'ORG', 'PER'}, ('Justice.Trial-Hearing', 'Place'): {'GPE', 'FAC', 'LOC'}, ('Justice.Trial-Hearing', 'Adjudicator'): {'ORG', 'PER'}, ('Justice.Trial-Hearing', 'Prosecutor'): {'ORG', 'PER'}, ('Justice.Charge-Indict', 'Prosecutor'): {'ORG', 'GPE', 'PER'}, ('Justice.Fine', 'Entity'): {'ORG', 'GPE', 'PER'}, ('Business.Start-Org', 'Agent'): {'ORG', 'GPE', 'PER'}, ('Justice.Pardon', 'Adjudicator'): {'ORG', 'PER'}, ('Justice.Charge-Indict', 'Place'): {'GPE', 'FAC', 'LOC'}, ('Justice.Appeal', 'Adjudicator'): {'ORG', 'PER'}, ('Justice.Appeal', 'Plaintiff'): {'ORG', 'GPE', 'PER'}, ('Justice.Sentence', 'Place'): {'GPE', 'FAC'}, ('Life.Die', 'Person'): {'PER'}, ('Life.Be-Born', 'Person'): {'PER'}, ('Justice.Release-Parole', 'Place'): {'GPE', 'FAC'}, ('Justice.Sue', 'Defendant'): {'ORG', 'GPE', 'PER'}, ('Transaction.Transfer-Money', 'Beneficiary'): {'ORG', 'GPE', 'PER'}, ('Justice.Convict', 'Place'): {'GPE'}, ('Justice.Extradite', 'Origin'): {'GPE', 'FAC'}, ('Justice.Extradite', 'Destination'): {'GPE'}, ('Justice.Appeal', 'Place'): {'GPE', 'FAC'}, ('Business.Declare-Bankruptcy', 'Place'): {'GPE'}, ('Justice.Fine', 'Adjudicator'): {'PER'}, ('Life.Marry', 'Place'): {'GPE', 'FAC'}, ('Life.Divorce', 'Person'): {'PER'}, ('Personnel.Nominate', 'Agent'): {'ORG', 'GPE', 'PER'}, ('Business.Merge-Org', 'Org'): {'ORG'}, ('Justice.Acquit', 'Defendant'): {'PER'}, ('Justice.Sue', 'Adjudicator'): {'ORG', 'PER'}, ('Justice.Sue', 'Place'): {'GPE', 'LOC'}, ('Justice.Fine', 'Place'): {'GPE', 'FAC'}, ('Justice.Pardon', 'Place'): {'GPE'}, ('Justice.Pardon', 'Defendant'): {'PER'}, ('Justice.Acquit', 'Adjudicator'): {'PER'}, ('Transaction.Transfer-Money', 'Place'): {'GPE', 'LOC'}, ('Justice.Extradite', 'Agent'): {'ORG'}}

Code/ED/score_ED_E+.py

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
import os
2+
import json
3+
from const import EVENT_TYPE
4+
import torch
5+
from torchmetrics.classification import MulticlassCalibrationError
6+
7+
8+
def safe_div(num, denom):
9+
if denom > 0:
10+
return num / denom
11+
else:
12+
return 0
13+
14+
def compute_f1(predicted, gold, matched):
15+
precision = safe_div(matched, predicted)
16+
recall = safe_div(matched, gold)
17+
f1 = safe_div(2 * precision * recall, precision + recall)
18+
return precision, recall, f1
19+
20+
21+
def evaluate(preds, gold):
22+
assert len(preds) == len(gold)
23+
24+
for example_id in preds:
25+
preds[example_id] = list(set([tuple(i) for i in preds[example_id]]))
26+
gold[example_id] = [tuple(i) for i in gold[example_id]]
27+
28+
29+
pred_tri_num, gold_tri_num = 0, 0
30+
match_idn_num, match_cls_num, match_word_num = 0, 0, 0
31+
correct_confidence = 0
32+
incorrect_confidence = 0
33+
if_reasonable_num = 0
34+
for example_id in preds:
35+
pred_tri_num += len(preds[example_id])
36+
gold_tri_num += len(gold[example_id])
37+
38+
calibrate_record = []
39+
invalid_event_type = 0
40+
for example_id in preds:
41+
for pred_tri in preds[example_id]:
42+
start, end, event_type, trigger_word, confidence, if_reasonable = pred_tri
43+
match_idn = {item for item in gold[example_id] if item[0] == start and item[1] == end}
44+
match_word = [item for item in gold[example_id] if item[3] == trigger_word and item[2]==event_type]
45+
if match_word:
46+
match_word_num += 1
47+
correct_confidence += confidence
48+
if if_reasonable:
49+
if_reasonable_num += 1
50+
if confidence == 0:
51+
invalid_event_type += 1
52+
continue
53+
gold_label_idx = LABEL2ID[match_word[0][2]]
54+
pred_label_idx = LABEL2ID[event_type]
55+
calibrate_record.append([gold_label_idx, pred_label_idx, confidence / 100])
56+
else:
57+
incorrect_confidence += confidence
58+
if event_type not in LABEL2ID:
59+
invalid_event_type += 1
60+
continue
61+
if confidence == 0:
62+
invalid_event_type += 1
63+
continue
64+
gold_label_idx = LABEL2ID['None']
65+
pred_label_idx = LABEL2ID[event_type]
66+
calibrate_record.append([gold_label_idx, pred_label_idx, confidence / 100])
67+
if match_idn:
68+
match_idn_num += 1
69+
match_cls = {item for item in match_idn if item[2] == event_type}
70+
if match_cls:
71+
match_cls_num += 1
72+
73+
print(f"gold_tri_num: {gold_tri_num}, pred_tri_num: {pred_tri_num}, match_idn_num: {match_idn_num}, match_cls_num: {match_cls_num}, match_word_num: {match_word_num}")
74+
75+
tri_id_prec, tri_id_rec, tri_id_f = compute_f1(pred_tri_num, gold_tri_num, match_idn_num)
76+
tri_cls_prec, tri_cls_rec, tri_cls_f = compute_f1(pred_tri_num, gold_tri_num, match_cls_num)
77+
tri_word_prec, tri_word_rec, tri_word_f = compute_f1(pred_tri_num, gold_tri_num, match_word_num)
78+
print('Trigger Identification: P: {:.2f}, R: {:.2f}, F: {:.2f}'.format(tri_id_prec * 100.0, tri_id_rec * 100.0, tri_id_f * 100.0))
79+
print('Trigger Classification: P: {:.2f}, R: {:.2f}, F: {:.2f}'.format(tri_cls_prec * 100.0, tri_cls_rec * 100.0, tri_cls_f * 100.0))
80+
print('Trigger Word Cls: P: {:.2f}, R: {:.2f}, F: {:.2f}'.format(tri_word_prec * 100.0, tri_word_rec * 100.0, tri_word_f * 100.0))
81+
print('(Trigger Word Cls) Correct Mean Confidence: {:.2f}, Incorrect Mean Confidence: {:.2f}'.format(correct_confidence / match_word_num, incorrect_confidence / (pred_tri_num - match_word_num)))
82+
print(f' Auto Rate: {if_reasonable_num/match_word_num}')
83+
84+
# Compute Expected Calibration Error (ECE)
85+
assert len(calibrate_record) == (pred_tri_num - invalid_event_type)
86+
print(invalid_event_type, len(calibrate_record))
87+
label_idx, pred_idx, prob = zip(*calibrate_record)
88+
labels = torch.tensor(label_idx)
89+
preds = torch.zeros(len(calibrate_record), len(LABEL2ID), dtype=torch.float32)
90+
preds[range(len(calibrate_record)), pred_idx] = torch.tensor(prob)
91+
metric = MulticlassCalibrationError(num_classes=34, n_bins=50, norm='l1')
92+
result = metric(preds, labels)
93+
print('Expected Calibration Error: {:.5f}'.format(result))
94+
95+
96+
def filter_invalid_answer(preds):
97+
98+
def if_invalid(record):
99+
filter_words = ['unknown', 'Unknown', 'unspecified', 'not specified', 'not mentioned', 'None', 'none', 'not mentioned', 'not applicable', 'N/A']
100+
if not isinstance(record[2], str):
101+
return True
102+
elif not isinstance(record[3], str):
103+
return True
104+
elif not isinstance(record[0], int):
105+
return True
106+
elif not isinstance(record[1], int):
107+
return True
108+
elif not record[0]>=0:
109+
return True
110+
elif not record[1]>=0:
111+
return True
112+
elif record[2] in filter_words:
113+
return True
114+
elif record[3] in filter_words:
115+
return True
116+
return False
117+
118+
count = 0
119+
for example_id in preds:
120+
for record in preds[example_id][::-1]:
121+
if if_invalid(record):
122+
preds[example_id].remove(record)
123+
count += 1
124+
for example_id in preds:
125+
for record in preds[example_id]:
126+
record[1] += 1 # end + 1
127+
return count
128+
129+
130+
def read_gold_example(path):
131+
gold = {}
132+
with open(path) as f:
133+
lines = f.readlines()
134+
for line in lines:
135+
line = json.loads(line)
136+
events = line['events']
137+
gold[line['id']] = []
138+
for event in events:
139+
start = event['trigger']['start']
140+
end = event['trigger']['end']
141+
event_type = event['event_type'].replace(':','.')
142+
trigger_word = event['trigger']['text']
143+
gold[line['id']].append([start, end, event_type, trigger_word])
144+
145+
return gold
146+
147+
148+
def get_vocab():
149+
all_labels = ['None']
150+
for label in EVENT_TYPE:
151+
all_labels.append(label)
152+
label2id = {label: idx for idx, label in enumerate(all_labels)}
153+
id2label = {idx: label for idx, label in enumerate(all_labels)}
154+
return label2id, id2label
155+
156+
157+
def main(result_dir, gold_path):
158+
gold = read_gold_example(gold_path)
159+
160+
preds = {}
161+
# print(len(os.listdir(result_dir)))
162+
for file in os.listdir(result_dir):
163+
example_id = file[:-5]
164+
preds[example_id] = []
165+
file_path = os.path.join(result_dir, file)
166+
# print(file)
167+
with open(file_path, 'r', encoding='utf-8') as f:
168+
res = json.load(f)
169+
for event in res:
170+
start = event['start_word_index']
171+
end = event['end_word_index']
172+
event_type = event['event_type']
173+
trigger_word = event['trigger']
174+
confidence = event['confidence']
175+
if_reasonable = event['if_reasonable']
176+
preds[example_id].append([start, end, event_type, trigger_word, confidence, if_reasonable])
177+
178+
invalid_arg_num = filter_invalid_answer(preds)
179+
print(invalid_arg_num)
180+
evaluate(preds, gold)
181+
182+
183+
LABEL2ID,ID2LABEL = get_vocab()
184+
if __name__ == "__main__":
185+
result_dir = './Output/ED/Full_Testset/ED_E+_Closed'
186+
gold_path = './data/ACE05-E+/ED_E+_gold.json'
187+
main(result_dir, gold_path)
188+

0 commit comments

Comments
 (0)