-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_dataset_preparation.py
71 lines (52 loc) · 2.25 KB
/
main_dataset_preparation.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 pandas as pd
import os
from tqdm.auto import tqdm
''' DESCRIPTION '''
''' This is the file where raw datasets are prepared in order to have the same format '''
''' Dataset Profner '''
PATH_TEXTS = "raw_datasets/profner/txt-files/train/"
directory = os.fsencode(PATH_TEXTS)
raw_data_train = pd.DataFrame(columns=["text", "id"])
for file in tqdm(os.listdir(directory)):
filename = os.fsdecode(file)
if filename.endswith(".txt"):
f = open(os.path.join(PATH_TEXTS, filename), "r")
id_data = filename.split(".")[0]
data = f.read()
raw_data_train = raw_data_train.append({"text": data, "id": id_data}, ignore_index = True)
print(" ", end = "\r")
ground_truth_train = pd.read_csv("raw_datasets/profner/subtask-1/train.tsv", sep = "\t").sort_values("tweet_id")
raw_data_train = raw_data_train.join(ground_truth_train["label"])
# split in fake and true
fake_train = raw_data_train.loc[raw_data_train['label'] == 1]
true_train = raw_data_train.loc[raw_data_train['label'] == 0]
PATH_TEXTS = "raw_datasets/profner/txt-files/valid/"
directory = os.fsencode(PATH_TEXTS)
raw_data_valid = pd.DataFrame(columns=["text", "id"])
print("")
for file in tqdm(os.listdir(directory)):
filename = os.fsdecode(file)
if filename.endswith(".txt"):
f = open(os.path.join(PATH_TEXTS, filename), "r")
id_data = filename.split(".")[0]
data = f.read()
raw_data_valid = raw_data_valid.append({"text": data, "id": id_data}, ignore_index = True)
print(" ", end = "\r")
ground_truth_valid = pd.read_csv("raw_datasets/profner/subtask-1/valid.tsv", sep = "\t").sort_values("tweet_id")
raw_data_valid = raw_data_valid.join(ground_truth_valid["label"])
# split in fake and true
fake_valid = raw_data_valid.loc[raw_data_valid['label'] == 1]
true_valid = raw_data_valid.loc[raw_data_valid['label'] == 0]
fake = pd.concat([fake_train, fake_valid])
true = pd.concat([true_train, true_valid])
outdir = "datasets/profner/"
outname = "Fake.csv"
if not os.path.exists(outdir):
os.makedirs(outdir)
fullname = os.path.join(outdir, outname)
fake.to_csv(fullname, index=True)
outname = "True.csv"
if not os.path.exists(outdir):
os.makedirs(outdir)
fullname = os.path.join(outdir, outname)
true.to_csv(fullname, index=True)