-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreateDatasets.py
71 lines (63 loc) · 2.96 KB
/
createDatasets.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
import json
import os
import pandas as pd
import joblib
def load_politifact_large_and_gossip_cop():
path_to_data = "./Dataset"
data = pd.DataFrame(
columns=['article_index', 'provider', 'url', 'title', 'text', 'label'])
# loop through providers of news
for provider in os.listdir(path_to_data):
if not provider.startswith('.'):
# loop through real/fake sets of news
for news_type in os.listdir(path_to_data + "/" + provider):
if not news_type.startswith('.'):
# loop through each article folder
for articles in os.listdir(path_to_data + "/" + provider + "/" + news_type):
# open json file in folder
try:
with open(
path_to_data + "/" + provider + '/' + news_type +
'/' + articles + "/news content.json",
'r') as read_file:
file = json.load(read_file)
index = len(data)
data.loc[index] = [articles, provider, file['url'],
file['title'], file['text'], news_type]
except:
pass
data = data[data['text'].map(len) > 0]
if not os.path.isdir('./Data'):
os.makedirs('./Data')
joblib.dump(data, './Data/unprocessed_large.h5')
def load_politifact_small_and_buzzfeed():
path_to_data = './Dataset2'
data = pd.DataFrame(
columns=['article_index', 'provider', 'url', 'title', 'text', 'label'])
# loop through providers of news
for provider in os.listdir(path_to_data):
# avoid hidden folders
if not provider.startswith('.'):
# loop through real/fake sets of news
for news_type in os.listdir(path_to_data + "/" + provider):
if not news_type.startswith('.'):
# loop through each webpage
for webpage in os.listdir(path_to_data + "/" + provider + "/" + news_type):
# open json file
try:
with open(
path_to_data + "/" + provider + '/' + news_type + '/' + webpage,
'r') as read_file:
file = json.load(read_file)
index = len(data)
data.loc[index] = [webpage, provider, file['url'],
file['title'], file['text'], news_type]
except:
pass
data = data[data['text'].map(len) > 0]
joblib.dump(data, './Data/unprocessed_small.h5')
def main():
load_politifact_large_and_gossip_cop()
load_politifact_small_and_buzzfeed()
if __name__ == "__main__":
main()