-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_extraction.py
68 lines (58 loc) · 1.76 KB
/
data_extraction.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
import os
import shutil
import json
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_text as text
from official.nlp import optimization # to create AdamW optimizer
import matplotlib.pyplot as plt
tf.get_logger().setLevel('ERROR')
import csv
source_labels = dict()
with open('labels.csv') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=',')
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
source_labels[row[0]] = row[1]
line_count += 1
print(f'Processed {line_count} lines.')
count = 0
data = []
directory = 'nela-gt-2020/newsdata'
for file_name in os.listdir(directory):
file = open(os.path.join(directory, file_name))
data.extend(json.load(file))
count += 1
print(f'loaded {count} files')
count_fake = 0
count_true = 0
extracted_data = []
for datum in data:
source = datum['source']
if source in source_labels:
if source_labels[source] == '0':
count_true += 1
extracted_data.append(
{
'content': datum['content'],
'label': 0
}
)
elif source_labels[source] == '2':
count_fake += 1
extracted_data.append(
{
'content': datum['content'],
'label': 1
}
)
print(f'there are {count_fake} fake articles and {count_true} reliable ones')
keys = extracted_data[0].keys()
with open('fake_news.csv', 'w', newline='') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(extracted_data)