-
Notifications
You must be signed in to change notification settings - Fork 0
/
by_coss.py
455 lines (408 loc) · 19.9 KB
/
by_coss.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
from datetime import datetime
from pathlib import Path
import matplotlib.pyplot as plt
import networkx as nx
import numpy as np
import pandas as pd
from transformers import pipeline
import consts
import functions as f
from modules.preprocessing import io
# 'aic' is short for 'article in consideration'
# 'ea' is short for 'earlier article'
#
# paraphrase threshold: e.g 0.75, means if simscore of a sentence-pair is > 0.75 then this pair is plagiarized
# reused_threshold: e.g 0.8, means if more than 80% paragraphs/sentences from aic are plagizarized, then aic is plagiarized
# comission_bias_threshold: e.g 0.8, means if more than 80% of plagiarized paragraphs are slanted, than aic is bias by commission (to the slanted labal (L/R))
def analyze_coss(df, features_collection, polarity_classifier, folder, paraphrase_threshold=0.75, commission_bias_threshold=0.8, omission_bias_threshold=0.5, reused_threshold=0.5, topic_name='Unknown'):
node_list = []
edges = []
topic_info = {
"topic_id": topic_name,
"network": "",
"graph": {
"nodes": [],
"edges": []
},
"articles": []
}
for _, row in df.iterrows():
aic_id = int(row['id'])
aic_title = row['title']
aic_label = row['label']
results = {
'aic_id': row['id'],
'aic_datetime': row['datetime'],
'aic_label': aic_label,
'aic_total_paragraphs': 0,
'aic_reused_paragraphs': 0,
'aic_reused_percent': 0,
'aic_non_reused_paragraphs': 0,
'aic_non_reused_percent': 0,
'is_earliest': False,
'by_source_selection': {
'verdict': 'No',
'committed_articles': [],
'biased_source_label': {
'LEFT': 0,
'CENTER': 0,
'RIGHT': 0
},
},
'by_commission': {
'verdict': 'No',
'aic_reused_label': {
'LEFT': 0,
'CENTER': 0,
'RIGHT': 0
},
'aic_reused_label_percent': {
'LEFT': 0,
'CENTER': 0,
'RIGHT': 0
}
},
'by_omission': {
'verdict': 'No',
'omitted_articles': [],
'aic_non_reused_label': {
'LEFT': 0,
'CENTER': 0,
'RIGHT': 0
},
'aic_non_reused_label_percent': {
'LEFT': 0,
'CENTER': 0,
'RIGHT': 0
}
},
# 'biased_by_these_sources': [],
# 'biased_source_label': {
# 'LEFT': 0,
# 'CENTER': 0,
# 'RIGHT': 0
# },
'is_biased_by_source_selection': "No",
'earlier_articles': [],
}
aic_marked_reused = []
aic_marked_nonreused = []
#
# Prepare nodes for Directed graph
#
node_list.append((str(results['aic_id']), str(consts.COLOR_CODE[results["aic_label"]])))
topic_info["graph"]["nodes"].append({"id": int(results['aic_id']), "label": str(results['aic_id']), "color": str(consts.COLOR_CODE[results["aic_label"]]) })
is_earliest = True
# compared to other articles called earlier-article or ea
for feature in features_collection:
if aic_id != int(feature['article_1_id']) and aic_id != int(feature['article_2_id']):
continue
datetime_1 = datetime.strptime(feature['article_1_publish_date'], '%d/%m/%Y %H:%M:%S')
datetime_2 = datetime.strptime(feature['article_2_publish_date'], '%d/%m/%Y %H:%M:%S')
reversed = False
if int(feature['article_1_id']) == aic_id: # article_1 = source, article_2 = reused
if datetime_2 > datetime_1:
continue
if int(feature['article_2_id']) == aic_id: # article_1 = reused, article_2 = source
reversed = True
if datetime_2 < datetime_1:
continue
is_earliest = False
sim_scores = np.asarray(feature['features'])
if reversed:
ea = {
'id': feature['article_1_id'],
'label': feature['article_1_label'],
'datetime': feature['article_1_publish_date'],
'paragraphs_length': feature['article_1_paragraph_length'],
'sentences': feature['article_1_sentences'] }
aic = {
'id': feature['article_2_id'],
'label': feature['article_2_label'],
'datetime': feature['article_2_publish_date'],
'paragraphs_length': feature['article_2_paragraph_length'],
'sentences': feature['article_2_sentences'] }
else:
sim_scores = sim_scores.transpose()
ea = {
'id': feature['article_2_id'],
'label': feature['article_2_label'],
'datetime': feature['article_2_publish_date'],
'paragraphs_length': feature['article_2_paragraph_length'],
'sentences': feature['article_2_sentences']
}
aic = {
'id': feature['article_1_id'],
'label': feature['article_1_label'],
'datetime': feature['article_1_publish_date'],
'paragraphs_length': feature['article_1_paragraph_length'],
'sentences': feature['article_1_sentences']
}
results['aic_total_paragraphs'] = aic['paragraphs_length']
if aic_marked_reused == []:
aic_marked_reused = [False for i in range(results['aic_total_paragraphs'])]
if aic_marked_nonreused == []:
aic_marked_nonreused = [False for i in range(results['aic_total_paragraphs'])]
analyzed = {
'ea_id': ea['id'],
'ea_label': ea['label'],
'ea_datetime': ea['datetime'],
'ea_total_paragraphs': ea['paragraphs_length'],
'ea_total_reused_paragraphs': 0,
'ea_reused_ratio': 0,
'ea_total_non_reused_paragraphs': 0,
'ea_non_reused_ratio': 0,
'ea_by_source_selection': 'No',
'ea_reused_label': {
'LEFT': 0,
'CENTER': 0,
'RIGHT': 0
},
'ea_reused_label_percent': {
'LEFT': 0,
'CENTER': 0,
'RIGHT': 0
},
'ea_non_reused_label': {
'LEFT': 0,
'CENTER': 0,
'RIGHT': 0
},
'ea_non_reused_label_percent': {
'LEFT': 0,
'CENTER': 0,
'RIGHT': 0
},
'ea_reused_details': [],
'ea_non_reused_details': []
}
ea_marked_reused = [False for i in range(ea['paragraphs_length'])]
for i in range(ea['paragraphs_length']):
for j in range(aic['paragraphs_length']):
if sim_scores[i][j] > paraphrase_threshold:
ea_marked_reused[i] = True
analyzed['ea_total_reused_paragraphs'] = ea_marked_reused.count(True)
analyzed['ea_reused_ratio'] = analyzed['ea_total_reused_paragraphs'] / analyzed['ea_total_paragraphs']
analyzed['ea_total_non_reused_paragraphs'] = ea_marked_reused.count(False)
analyzed['ea_non_reused_ratio'] = analyzed['ea_total_non_reused_paragraphs'] / analyzed['ea_total_paragraphs']
# Stats
for i in range(ea['paragraphs_length']):
ea_classified_label = polarity_classifier(ea['sentences'][i])[0]
if ea_marked_reused[i] == True:
analyzed['ea_reused_label'][ea_classified_label["label"]] += 1
else:
analyzed['ea_non_reused_label'][ea_classified_label["label"]] += 1
for label in consts.Labels:
if analyzed['ea_total_reused_paragraphs'] > 0:
analyzed['ea_reused_label_percent'][label] = analyzed['ea_reused_label'][label] / analyzed['ea_total_reused_paragraphs']
if analyzed['ea_total_non_reused_paragraphs'] > 0:
analyzed['ea_non_reused_label_percent'][label] = analyzed['ea_non_reused_label'][label] / analyzed['ea_total_non_reused_paragraphs']
# Details
for i in range(ea['paragraphs_length']):
ea_classified_label = polarity_classifier(ea['sentences'][i])[0]
reused_detail = {
'text': ea['sentences'][i],
'label': ea_classified_label,
'reused_by_aic': []
}
if ea_marked_reused[i] == True:
# has_reused = False
for j in range(aic['paragraphs_length']):
# sim_scores[i][j] = similarity score between earlier_article's paragraph i-th and article_in_consideration's paragraph j-th
# if sim_scores > threshold, that means aic has reused paragraph i-th in its paragraph j-th
aic_classified_label = polarity_classifier(aic['sentences'][j])[0]
if sim_scores[i][j] > paraphrase_threshold:
reused_detail['reused_by_aic'].append(aic['sentences'][j])
if aic_marked_reused[j] == False:
aic_marked_reused[j] = True
results['by_commission']['aic_reused_label'][aic_classified_label['label']] += 1
else:
analyzed['ea_non_reused_details'].append({
'text': ea['sentences'][i],
'label': ea_classified_label,
})
if len(reused_detail['reused_by_aic']) > 0:
analyzed['ea_reused_details'].append(reused_detail)
# By commission
results['aic_reused_paragraphs'] = aic_marked_reused.count(True)
results['aic_reused_percent'] = results['aic_reused_paragraphs'] / results['aic_total_paragraphs']
by_commission_max_reused_percent = 0
by_commission_max_reused_label = None
if results['aic_reused_paragraphs'] > 0:
for label in consts.Labels:
results['by_commission']['aic_reused_label_percent'][label] = results['by_commission']['aic_reused_label'][label] / results['aic_reused_paragraphs']
if results['by_commission']['aic_reused_label_percent'][label] > by_commission_max_reused_percent:
by_commission_max_reused_percent = results['by_commission']['aic_reused_label_percent'][label]
by_commission_max_reused_label = label
is_by_commission = ''
if results['aic_reused_percent'] > commission_bias_threshold and by_commission_max_reused_label != "CENTER" and by_commission_max_reused_label != None:
is_by_commission = f'Yes, to the {by_commission_max_reused_label} {"{:0.2%}".format(by_commission_max_reused_percent)}'
results['by_commission']['verdict'] = is_by_commission
# By omission
if analyzed['ea_non_reused_ratio'] > omission_bias_threshold:
highestOmittedLabel = None
highestOmittedPercent = 0
for label in consts.Labels:
if analyzed['ea_non_reused_label_percent'][label] > highestOmittedPercent:
highestOmittedPercent = analyzed['ea_non_reused_label_percent'][label]
highestOmittedLabel = label
if highestOmittedLabel == "CENTER":
results['by_omission']['omitted_articles'].append(analyzed['ea_id'])
results['by_omission']['aic_non_reused_label'][analyzed['ea_label']] += 1
# By source selection
if analyzed['ea_total_reused_paragraphs'] > 0:
for label in consts.Labels:
analyzed['ea_reused_label_percent'][label] = analyzed['ea_reused_label'][label] / analyzed['ea_total_reused_paragraphs']
is_by_source_selection = ''
analyzed['ea_reused_ratio'] = round(analyzed['ea_total_reused_paragraphs'] / analyzed['ea_total_paragraphs'], 2)
if analyzed['ea_reused_ratio'] > reused_threshold:
highest_percent = 0
highest_label = None
for label in consts.Labels:
if analyzed['ea_reused_label_percent'][label] > highest_percent:
highest_percent = analyzed['ea_reused_label_percent'][label]
highest_label = label
if highest_label != 'CENTER':
is_by_source_selection = f'Yes, to the {highest_label}. Percentage: {"{:0.2%}".format(highest_percent)}'
analyzed['ea_by_source_selection'] = is_by_source_selection
results['by_source_selection']['committed_articles'].append(ea['id'])
results['by_source_selection']['biased_source_label'][highest_label] += 1
results['earlier_articles'].append(analyzed)
if is_earliest:
results['is_biased'] = 'This is the earliest article'
results['is_earliest'] = True
# By omission conclusion
is_by_omission = "No"
if len(results['by_omission']['omitted_articles']) > 0:
is_by_omission = "Yes"
for label in consts.Labels:
results['by_omission']['aic_non_reused_label_percent'][label] = round(results['by_omission']['aic_non_reused_label'][label] / len(results['by_omission']['omitted_articles']), 2)
results['by_omission']['verdict'] = is_by_omission
# By source selection conclusion
if len(results['by_source_selection']['committed_articles']) > 0:
max_l = 0
max_label = None
for label in consts.Labels:
if results['by_source_selection']['biased_source_label'][label] > max_l:
max_l = results['by_source_selection']['biased_source_label'][label]
max_label = label
if max_label != 'CENTER':
results['by_source_selection']['verdict'] = f"Yes, to the {max_label}"
#
# Prepare edges for Directed graph
#
for ea_id in results['by_source_selection']['committed_articles']:
edges.append((str(ea_id), str(aic_id)))
topic_info["graph"]["edges"].append({
"from": int(ea_id),
"to": int(aic_id),
"value": analyzed['ea_reused_ratio'],
"label": f'{"{:0.2%}".format(analyzed["ea_reused_ratio"])}',
})
results_folder = folder if folder != None else f'./{FOLDER}/by_coss'
Path(results_folder).mkdir(parents=True, exist_ok=True)
# datetime: _{datetime.now().strftime('%Y-%m-%d_%H-%M-%S')}
results_filename = f"./{results_folder}/by_coss_{DATASET}_of_article_{aic_id}.json"
chart_filename = f"./{results_folder}/by_coss_{DATASET}_of_article_{aic_id}.png"
topic_info['articles'].append({
"article_id": aic_id,
"article_title": f'{aic_title} + ({aic_label})',
"analyzed": results_filename,
"chart": chart_filename if (len(results['by_source_selection']['committed_articles']) > 0) else ''
})
io.write_json(results_filename, results)
if len(results['by_source_selection']['committed_articles']) > 0:
# STACKED BAR CHART
build_chart(results, chart_filename)
# #
# # BUILD DIRECTED GRAPH
# #
if len(node_list) and len(edges):
graph_filename = f"./{results_folder}/by_coss_{DATASET}_network_topic_{topic_name}.png"
graph_title = f'Plagiarism map among articles in topic {topic_name}'
topic_info['network'] = graph_filename
build_graph(node_list=node_list, edges=edges, filename=graph_filename, graph_title=graph_title)
return topic_info
def build_chart(results, filename):
plt.figure()
L = []
R = []
C = []
ea_ids = []
for ea in results['earlier_articles']:
if ea['ea_by_source_selection'] != 'No':
L.append(ea['ea_reused_label']['LEFT'])
C.append(ea['ea_reused_label']['CENTER'])
R.append(ea['ea_reused_label']['RIGHT'])
ea_ids.append(f'{ea["ea_id"]}\n{ea["ea_label"]}')
df = pd.DataFrame({
'Left': L,
'Right': R,
'Center': C
})
colors = ['red', 'blue', 'gray']
ax = df.plot(stacked=True, kind='bar', color=colors, figsize=(10,10))
for bar in ax.patches:
height = bar.get_height()
width = bar.get_width()
x = bar.get_x()
y = bar.get_y()
label_text = int(height)
label_x = x + width / 2
label_y = y + height / 2
if(label_text != 0):
ax.text(label_x, label_y, label_text, ha='center', va='center', color='white', weight='bold')
ax.set_xticklabels(ea_ids)
ax.set_title(f'Number of paragraphs resued by article {results["aic_id"]} ({results["aic_label"]}) and their classified polarity')
ax.set_xlabel('Id of earlier articles')
ax.set_ylabel('Number of reused paragraphs')
plt.savefig(filename)
plt.cla()
#
# element of node_list is a tuple of (name, color). e.g: ('1', 'red'), ('2', 'gray')...
# element of edges is a tuple of names. E.g: ('1', '2') means node 1 to node 2
#
def build_graph(node_list, edges, filename, graph_title):
G = nx.DiGraph()
for n in node_list:
G.add_node(n[0], color=n[1])
G.add_edges_from(edges)
colors = [node[1]['color'] for node in G.nodes(data=True)]
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot()
ax.set_title(graph_title)
plt.ylabel('Red nodes denote leaning left articles, Blue nodes denote leaning right articles, Center nodes denote neutral articles')
nx.draw(G, ax=ax, node_color=colors, with_labels=True, font_color='white')
plt.title(graph_title)
plt.savefig(filename)
plt.cla()
if __name__ == "__main__":
DATASET = 'GROUNDNEWS'
DATASET_VERSION = 'Full'
FOLDER = consts.dataset[DATASET][DATASET_VERSION]['FOLDER']
FILES = consts.dataset[DATASET][DATASET_VERSION]['FILES']
files_info = []
for i, file in enumerate(FILES):
df = f.read_data(FOLDER, [file])
df = df.dropna()
try:
topic_id = file.split("/")[1]
features = f.read_features(FOLDER, f'./{topic_id}/features.json')
except Exception as e:
print(e)
continue
classifier = pipeline("text-classification", model=f'./model/{consts.polarity_classifier_path}')
file_info = analyze_coss(
df=df,
folder=f'./{FOLDER}/by_coss',
features_collection=features,
polarity_classifier=classifier,
paraphrase_threshold=consts.paraphrase_threshold,
topic_name=topic_id
)
files_info.append(file_info)
folder = f'./{FOLDER}/by_coss'
io.write_json(f"./{folder}/files_info.json", files_info)
if consts.openShell:
f.showToast("Media Bias by COSS - Main")
f.openShell()