-
Notifications
You must be signed in to change notification settings - Fork 0
/
entity_typing_deduped-IAA_Kappa_SecondLayer.py
171 lines (134 loc) · 7.1 KB
/
entity_typing_deduped-IAA_Kappa_SecondLayer.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
import csv
from collections import defaultdict
import re
import json
import sys
from sklearn.metrics import cohen_kappa_score
import statistics
# Sort_annotatons functions, organized data according to Turkle.Username
def sort_annotations(file, prep_dict):
csv_fh = open(file, 'r', encoding='utf-8')
reader = csv.DictReader(csv_fh)
for row in reader:
argument_spans = json.loads(row['Answer.entity_typing'])
if not row['Turkle.Username'] in prep_dict:
prep_dict['Turkle.Username'] = []
for argument_span in argument_spans:
argument_span['HITId'] = row['HITId']
prep_dict[row['Turkle.Username']].append(argument_span)
return(prep_dict)
# Calculate_matches function, calculate agreement between annotators
def calculate_matches(prep_dict, agreement_dict):
total = 0 #
agreement = 0 #
for username in prep_dict:
total = 0
agreement = 0
agreements = []
t1 = prep_dict[username]
#print(username)
#print(t1)
agreement_dict[username] = []
for compare_username in prep_dict:
if username == compare_username:
continue
t2 = prep_dict[compare_username]
username_dict = {'BallotSlate': 0, 'PaperBallot': 0,
'Document': 0, 'Equipment': 0, 'Flag': 0, 'Wreckage': 0,
'BehaviorCrime': 0, 'FinanaicalCrime': 0, 'PoliticalCrime': 0, 'ViolentCrime': 0,
'Building': 0, 'GeographicalArea': 0,
'Installation': 0, 'Structure': 0, 'Way': 0,
'Country': 0, 'OrganizationOfCountries': 0, 'ProvinceState': 0, 'UrbanArea': 0,
'GeographicPoint': 0, 'Land': 0, 'Position': 0,
'Assests': 0, 'Cash': 0,
'Association': 0, 'CommericalOrganization': 0, 'CriminalOrganization': 0,
'Government': 0, 'International': 0, 'MilitaryOrganization': 0,
'PoliticalOrganization': 0,
'Combatant': 0, 'Fan': 0, 'MilitaryPersonnel': 0, 'Police': 0,
'ProfessionalPosition': 0, 'Protester': 0, 'Politician': 0,
'Political': 0,
'NumberPercentageVotes': 0, 'TurnoutVoters': 0,
'Cultural': 0, 'Ideological': 0, 'Poltical': 0, 'Religious': 0, 'Sports': 0,
'Number': 0,
'Aircraft': 0, 'MilitaryVehicle': 0, 'Tocket': 0, 'Watercraft': 0,
'WheeledVehicle': 0,
'Bomb': 0, 'Bullets': 0, 'Cannon': 0, 'Club': 0, 'DaggerKnifeSword': 0, 'Gas': 0,
'GrenadeLauncher': 0, 'Gun': 0, 'MissileSystem': 0, 'ThrownProjectile': 0,
'LOC': 0, 'MON': 0, 'ORG': 0, 'PER': 0,
'RES': 0, 'SID': 0, 'TTL': 0, 'VAL': 0, 'VEH': 0,
'WEA': 0, 'BAL': 0, 'COM': 0, 'CRM': 0, 'FAC': 0,
'GPE': 0, 'LAW': 0, -1: 0}
compare_username_dict = {'BallotSlate': 0, 'PaperBallot': 0,
'Document': 0, 'Equipment': 0, 'Flag': 0, 'Wreckage': 0,
'BehaviorCrime': 0, 'FinanaicalCrime': 0, 'PoliticalCrime': 0, 'ViolentCrime': 0,
'Building': 0, 'GeographicalArea': 0,
'Installation': 0, 'Structure': 0, 'Way': 0,
'Country': 0, 'OrganizationOfCountries': 0, 'ProvinceState': 0, 'UrbanArea': 0,
'GeographicPoint': 0, 'Land': 0, 'Position': 0,
'Assests': 0, 'Cash': 0,
'Association': 0, 'CommericalOrganization': 0, 'CriminalOrganization': 0,
'Government': 0, 'International': 0, 'MilitaryOrganization': 0,
'PoliticalOrganization': 0,
'Combatant': 0, 'Fan': 0, 'MilitaryPersonnel': 0, 'Police': 0,
'ProfessionalPosition': 0, 'Protester': 0, 'Politician': 0,
'Political': 0,
'NumberPercentageVotes': 0, 'TurnoutVoters': 0,
'Cultural': 0, 'Ideological': 0, 'Poltical': 0, 'Religious': 0, 'Sports': 0,
'Number': 0,
'Aircraft': 0, 'MilitaryVehicle': 0, 'Tocket': 0, 'Watercraft': 0,
'WheeledVehicle': 0,
'Bomb': 0, 'Bullets': 0, 'Cannon': 0, 'Club': 0, 'DaggerKnifeSword': 0, 'Gas': 0,
'GrenadeLauncher': 0, 'Gun': 0, 'MissileSystem': 0, 'ThrownProjectile': 0,
'LOC': 0, 'MON': 0, 'ORG': 0, 'PER': 0,
'RES': 0, 'SID': 0, 'TTL': 0, 'VAL': 0, 'VEH': 0,
'WEA': 0, 'BAL': 0, 'COM': 0, 'CRM': 0, 'FAC': 0,
'GPE': 0, 'LAW': 0, -1: 0}
for answers in t1:
for compare_answers in t2:
if not compare_answers['HITId'] == answers['HITId']:
continue
#print(username)
#print(compare_username)
answer_type = answers['subtype']
if answer_type == 'n/a':
username_dict[answers['type']] += 1
else:
username_dict[answer_type] += 1
answer_type = compare_answers['subtype']
if answer_type == 'n/a':
compare_username_dict[compare_answers['type']] += 1
else:
compare_username_dict[answer_type] += 1
total += 1
user_count = []
for count in username_dict:
user_count.append(username_dict[count])
#print(user_count)
compareuser_count = []
for count2 in compare_username_dict:
compareuser_count.append(compare_username_dict[count2])
#print(compareuser_count)
#print(username)
#print(compare_username)
#print(username)
#print(compare_username)
#print(cohen_kappa_score(user_count, compareuser_count))
agreements.append(cohen_kappa_score(user_count, compareuser_count))
agreement += cohen_kappa_score(user_count, compareuser_count)
agreement_dict[username].append(agreement/total)
agreement_dict[username].append(statistics.median(agreements))
print(agreement_dict)
return agreement_dict
# Main function
def main():
prep_dict = defaultdict(list)
agreement_dict = defaultdict(list)
prep_dict = sort_annotations(sys.argv[1], prep_dict)
del prep_dict['Turkle.Username']
#del prep_dict['jgualla1']
agreement_dict = calculate_matches(prep_dict, agreement_dict)
# Calculate average observed agreement between annotators
with open("entity_typing_deduped-Results_Kappa_SecondLayer.json", "w") as f1:
json.dump(agreement_dict, f1)
if __name__=="__main__":
main()