-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.number_of_COH_analysis.py
439 lines (323 loc) · 17.3 KB
/
2.number_of_COH_analysis.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# produces correlations for number of coherence relations vs narrative quality measures (line 101), number of COH vs
# argument quality measures (191), number of COH vs argument presence (275), and number of COH vs narrative presence (358)
import csv
import re
import os
import sys
import scipy
import pandas as pd
import scipy.stats as st
from scipy.stats import pearsonr
import statistics
from statistics import mean
import time
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import copy
from clean_up_SE_coh import simplify_all_SE_types
from clean_up_SE_coh import clean_up_coh_rels
from extract_annotations import fill_in_human_grover, fill_in_containers
### determine path to the annotations
file_path = os.path.abspath(__file__)
path = os.path.dirname(file_path)+"/"
### Extract and print out document-annotator assignments
regex = re.compile('[^a-zA-Z]')
annotators = {"0":[],"2":[],"1":[]}
with open('1.info.csv', 'r') as f:
reader = csv.reader(f)
for idx,row in enumerate(reader):
if idx != 0 and len(row) > 1 and "file name" not in row:
index = re.sub('[^0-9]','',row[0])
for key in annotators:
if key.lower() in [i.strip() for i in row[1].split(",")]:
annotators[key].append(int(index))
print("***")
print("per annotator count")
print("***")
for annotator in annotators:
print(annotator)
print(len(annotators[annotator]))
### EXTRACT THE HUMAN, GROVER, OR DAVINCI SOURCE OF EACH DOCUMENT
# Create lists for keeping track of human and AI generations
h_docs = []
g_docs = []
d_docs = []
fill_in_human_grover(h_docs, g_docs, d_docs)
### Extract Situation Entities, Coherence Relations and Document-level ratings
# from each annotated document
# Create containers
G_SE_container = {"0":{},"2":{},"1":{}}
G_Coh_container = {"0":{},"2":{},"1":{}}
G_Doc_container = {"0":{},"2":{},"1":{}}
H_SE_container = {"0":{},"2":{},"1":{}}
H_Coh_container = {"0":{},"2":{},"1":{}}
H_Doc_container = {"0":{},"2":{},"1":{}}
D_SE_container = {"0":{},"2":{},"1":{}}
D_Coh_container = {"0":{},"2":{},"1":{}}
D_Doc_container = {"0":{},"2":{},"1":{}}
SE_accounted_for = [] # to prevent double-counting of shared documents
Coh_accounted_for = [] # to prevent double-counting of shared documents
doc_counter = 0
doc_counter = fill_in_containers(h_docs, g_docs, d_docs, G_SE_container, G_Coh_container,
G_Doc_container, H_SE_container, H_Coh_container, H_Doc_container, D_SE_container, D_Coh_container,
D_Doc_container, SE_accounted_for, Coh_accounted_for, doc_counter)
Coh_types = ['elab', 'temp', 've', 'ce', 'same', 'contr', 'sim', 'attr', 'examp', 'cond', 'rep', 'deg', 'gen']
def annotator_tag(Doc_container): # tags every doc_id with its annotator number
tagged_dict = {}
for annotator in Doc_container.keys():
tagged_dict[annotator] = {}
for k,v in Doc_container[annotator].items():
tagged_dict[annotator][str(k) + str(annotator)] = v
return tagged_dict
# applying the tags to the containers
H_Doc_container = annotator_tag(H_Doc_container)
G_Doc_container = annotator_tag(G_Doc_container)
D_Doc_container = annotator_tag(D_Doc_container)
H_Coh_container = annotator_tag(H_Coh_container)
G_Coh_container = annotator_tag(G_Coh_container)
D_Coh_container = annotator_tag(D_Coh_container)
########################################################
### COHERENCE RELATION FREQUENCY'S CORRELATION WITH NARRATIVE QUALITY MEASURES -> instructions for running function are below
# nar_correlation_calculator (starting line 177)
def narrative_Coh_counts(Doc_container, Coh_container, index): # produces a dict with the number of COH from
# each document, called by nar_correlation_calculator
quality_dict = {}
for annotator in Doc_container.keys():
for doc_id in Doc_container[annotator].keys():
for Coh_id in Coh_container[annotator].keys():
ratings = Doc_container[annotator][doc_id]
Coh_lists = Coh_container[annotator][doc_id]
if doc_id == Coh_id: # to match IDs across dicts
if (ratings[index] != 'NA') and (ratings[index] != '0'): # counting the number of COH across all documents
for list in Coh_lists:
if type(list[4]) == str:
if doc_id not in quality_dict:
quality_dict[doc_id] = {}
quality_dict[doc_id] = 0
quality_dict[doc_id] += 1
return quality_dict
def narrative_ratings(Doc_container, index): # produces a dict with the rating number of a given quality measure from
# each from each document, called by nar_correlation_calculator
rating_dict = {}
for annotator in Doc_container.keys():
for doc_id in Doc_container[annotator].keys():
ratings = Doc_container[annotator][doc_id]
if (ratings[index] != 'NA') and (ratings[index] != '0'):
if(doc_id not in rating_dict): # If dictionary does not have a dictionary for doc_id, make one
rating_dict[doc_id] = {}
if(ratings[index] not in rating_dict[doc_id]): # Places rating in doc_id's dictionary
rating_dict[doc_id] = ratings[index]
return rating_dict
def nar_correlation_calculator(Doc_container, Coh_container): # uses both of the previous functions to find correlations
# the number of COH and each narrative quality measure in all of the documents with narrative
indices = [4,5,6,7]
for index in indices:
quality_dict = narrative_Coh_counts(Doc_container, Coh_container, index)
rating_dict = narrative_ratings(Doc_container, index)
quality_list = []
rating_list = []
for k,v in rating_dict.items(): # creates list of quality ratings to be fed into pearsonr
rating_list.append(int(v))
for k1,v1 in quality_dict.items(): # creates list of COH counts to be fed into pearsonr
quality_list.append(int(v1))
r,p = pearsonr(quality_list, rating_list)
if index == 4 and abs(r) > 0.2:
print('NUMBER OF COHERENCE RELATIONS CORRELATED WITH PLAUSIBILITY:', 'r = ', round(r, 2), 'p = ', round(p, 2))
if index == 5 and abs(r) > 0.2:
print('NUMBER OF COHERENCE RELATIONS CORRELATED WITH COMPLETENESS:', 'r = ', round(r, 2), 'p = ', round(p, 2))
if index == 6 and abs(r) > 0.2:
print('NUMBER OF COHERENCE RELATIONS CORRELATED WITH CONSISTENCY:', 'r = ', round(r, 2), 'p = ', round(p, 2))
if index == 7 and abs(r) > 0.2:
print('NUMBER OF COHERENCE RELATIONS CORRELATED WITH COVERAGE:', 'r = ', round(r, 2), 'p = ', round(p, 2))
return
#Uncomment for grover results
#print(nar_correlation_calculator(G_Doc_container, G_Coh_container))
#based on 78 documents
#Uncomment for GPT-3 results
#print(nar_correlation_calculator(D_Doc_container, D_Coh_container))
#based on 24 documents
#Uncomment for human results
#print(nar_correlation_calculator(H_Doc_container, H_Coh_container))
#based on 78 documents
########################################################
########################################################
### COHERENCE RELATION FREQUENCY'S CORRELATION WITH ARGUMENT QUALITY MEASURES -> instructions for running function are below
# arg_correlation_calculator (starting line 261)
def argument_Coh_counts(Doc_container, Coh_container, index): # produces a dict with number of COH from
# each document, called by arg_correlation_calculator
quality_dict = {}
for annotator in Doc_container.keys():
for doc_id in Doc_container[annotator].keys():
for Coh_id in Coh_container[annotator].keys():
ratings = Doc_container[annotator][doc_id]
Coh_lists = Coh_container[annotator][doc_id]
if doc_id == Coh_id: # to match IDs across dicts
if (ratings[index] != 'NA') and (ratings[index] != '0'): # counting the number of COH across all documents
for list in Coh_lists:
if type(list[4]) == str:
if doc_id not in quality_dict:
quality_dict[doc_id] = {}
quality_dict[doc_id] = 0
quality_dict[doc_id] += 1
return quality_dict
def argument_ratings(Doc_container, index): # produces a dict with the rating number of a given quality measure from
# each from each document, called by arg_correlation_calculator
rating_dict = {}
for annotator in Doc_container.keys():
for doc_id in Doc_container[annotator].keys():
ratings = Doc_container[annotator][doc_id]
if (ratings[index] != 'NA') and (ratings[index] != '0'):
if(doc_id not in rating_dict): # If dictionary does not have a dictionary for doc_id, make one
rating_dict[doc_id] = {}
if(ratings[index] not in rating_dict[doc_id]): # Places rating in doc_id's dictionary
rating_dict[doc_id] = ratings[index]
return rating_dict
def arg_correlation_calculator(Doc_container, Coh_container): # uses both of the previous functions to find correlations
# the number of COH and each argument measure in all of the documents with argument
indexes = [13,14]
for index in indexes:
quality_dict = argument_Coh_counts(Doc_container, Coh_container, index)
rating_dict = argument_ratings(Doc_container, index)
quality_list = []
rating_list = []
for k,v in rating_dict.items(): # creates list of quality ratings to be fed into pearsonr
rating_list.append(int(v))
for k1,v1 in quality_dict.items(): # creates list of COH counts to be fed into pearsonr
quality_list.append(int(v1))
r,p = pearsonr(quality_list, rating_list)
if index == 13 and abs(r) > 0.2:
print('NUMBER OF COHERENCE RELATIONS CORRELATED WITH COGENCY:', 'r = ', round(r, 2), 'p = ', round(p, 2))
if index == 14 and abs(r) > 0.2:
print('NUMBER OF COHERENCE RELATIONS CORRELATED WITH EFFECTIVENESS:', 'r = ', round(r, 2), 'p = ', round(p, 2))
return
#Uncomment for grover results
#print(arg_correlation_calculator(G_Doc_container, G_Coh_container))
#based on 71 documents
#Uncomment for GPT-3 results
#print(arg_correlation_calculator(D_Doc_container, D_Coh_container))
#based on 52 documents
#Uncomment for human results
#print(arg_correlation_calculator(H_Doc_container, H_Coh_container))
#based on 115 documents
########################################################
########################################################
### NUMBER OF COHERENCE RELATIONS CORRELATION WITH PRESENCE OF ARGUMENT -> instructions for running function are below
# p_of_arg_correlation_calculator (starting line 344)
def p_of_argument_Coh_counts(Doc_container, Coh_container): # produces a dict with counts of the number of COH in
# each document, called by p_of_arg_correlation_calculator
quality_dict = {}
for annotator in Doc_container.keys():
for doc_id in Doc_container[annotator].keys():
for Coh_id in Coh_container[annotator].keys():
Coh_lists = Coh_container[annotator][doc_id]
if doc_id == Coh_id: # to match IDs across dicts
for list in Coh_lists: # counting the number of COH across all documents
if type(list[4]) == str:
if doc_id not in quality_dict:
quality_dict[doc_id] = {}
quality_dict[doc_id] = 0
quality_dict[doc_id] += 1
if doc_id not in quality_dict: # so COH types that don't appear in doc aren't left out of the dict
quality_dict[doc_id] = {}
quality_dict[doc_id] = 0
return quality_dict
def presence_of_argument(Doc_container, index): # produces a list of argument presence for each document (with 0 for
# no narrative and 1 for some narrative), called by p_of_arg_correlation_calculator
rating_dict = {}
for annotator in Doc_container.keys():
for doc_id in Doc_container[annotator].keys():
ratings = Doc_container[annotator][doc_id]
if (ratings[index] == 'NA') or (ratings[index] == '0'):
if(doc_id not in rating_dict): # Adds 0 if no narrative
rating_dict[doc_id] = {}
rating_dict[doc_id] = 0
if (ratings[index] != 'NA') and (ratings[index] != '0'): # Adds 1 if some narrative
if(doc_id not in rating_dict):
rating_dict[doc_id] = {}
rating_dict[doc_id] = 1
return rating_dict
def p_of_arg_correlation_calculator(Doc_container, Coh_container): # uses both of the previous functions to find
# correlations between the number of Coh and presence of argument
quality_dict = p_of_argument_Coh_counts(Doc_container, Coh_container)
rating_dict = presence_of_argument(Doc_container, 12)
quality_list = []
rating_list = []
for k,v in rating_dict.items(): # creates list of 0s and 1s to be fed into pointbiserialr
rating_list.append(int(v))
for k1,v1 in quality_dict.items(): # creates list of COH counts to be fed into pointbiserialr
quality_list.append(int(v1))
r,p = scipy.stats.pointbiserialr(rating_list, quality_list)
if abs(r) > 0.2:
print('NUMBER OF COHERENCE RELATIONS CORRELATED WITH PRESENCE OF ARGUMENT:', 'r =', round(r, 2), ', p =', round(p, 2))
return
#Uncomment for grover results
#print(p_of_arg_correlation_calculator(G_Doc_container, G_Coh_container))
#based on 180 documents
#Uncomment for GPT-3 results
#print(p_of_arg_correlation_calculator(D_Doc_container, D_Coh_container))
#based on 72 documents
#Uncomment for human results
#print(p_of_arg_correlation_calculator(H_Doc_container, H_Coh_container))
#based on 192 documents
########################################################
########################################################
### NUMBER OF COHERENCE RELATIONS CORRELATION WITH PRESENCE OF NARRATIVE -> instructions for running function are below
# p_of_nar_correlation_calculator (starting line 427)
def p_of_narrative_Coh_counts(Doc_container, Coh_container): # produces a dict with counts of the number of COH in
# each document, called by p_of_nar_correlation_calculator
quality_dict = {}
for annotator in Doc_container.keys():
for doc_id in Doc_container[annotator].keys():
for Coh_id in Coh_container[annotator].keys():
Coh_lists = Coh_container[annotator][doc_id]
if doc_id == Coh_id: # to match IDs across dicts
for list in Coh_lists: # counting the number of COH across all documents
if type(list[4]) == str:
if doc_id not in quality_dict:
quality_dict[doc_id] = {}
quality_dict[doc_id] = 0
quality_dict[doc_id] += 1
if doc_id not in quality_dict: # so COH types that don't appear in doc aren't left out of the dict
quality_dict[doc_id] = {}
quality_dict[doc_id] = 0
return quality_dict
def presence_of_narrative(Doc_container, index): # produces a list of narrative presence for each document (with 0 for
# no narrative and 1 for some narrative), called by p_of_nar_correlation_calculator
rating_dict = {}
for annotator in Doc_container.keys():
for doc_id in Doc_container[annotator].keys():
ratings = Doc_container[annotator][doc_id]
if (ratings[index] == 'NA') or (ratings[index] == '0'):
if(doc_id not in rating_dict): # Adds 0 if no narrative
rating_dict[doc_id] = {}
rating_dict[doc_id] = 0
if (ratings[index] != 'NA') and (ratings[index] != '0'): # Adds 1 if some narrative
if(doc_id not in rating_dict):
rating_dict[doc_id] = {}
rating_dict[doc_id] = 1
return rating_dict
def p_of_nar_correlation_calculator(Doc_container, Coh_container): # uses both of the previous functions to find
# correlations between the number of Coh and presence of argument
quality_dict = p_of_narrative_Coh_counts(Doc_container, Coh_container)
rating_dict = presence_of_narrative(Doc_container, 3)
quality_list = []
rating_list = []
for k,v in rating_dict.items(): # creates list of 0s and 1s to be fed into pointbiserialr
rating_list.append(int(v))
for k1,v1 in quality_dict.items(): # creates list of COH counts to be fed into pointbiserialr
quality_list.append(int(v1))
r,p = scipy.stats.pointbiserialr(rating_list, quality_list)
if abs(r) > 0.2:
print('NUMBER OF COHERENCE RELATIONS CORRELATED WITH PRESENCE OF NARRATIVE:', 'r =', round(r, 2), ', p =', round(p, 2))
return
#Uncomment for grover results
#print(p_of_nar_correlation_calculator(G_Doc_container, G_Coh_container))
#based on 180 documents
#Uncomment for GPT-3 results
#print(p_of_nar_correlation_calculator(D_Doc_container, D_Coh_container))
#based on 72 documents
#Uncomment for human results
#print(p_of_nar_correlation_calculator(H_Doc_container, H_Coh_container))
#based on 192 documents
########################################################