-
Notifications
You must be signed in to change notification settings - Fork 1
/
files_to_tsv.py
40 lines (31 loc) · 1.67 KB
/
files_to_tsv.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
import csv
import argparse
from os import listdir
from os.path import join
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data-dir', default='data_dir')
parser.add_argument('--dataset-split', default='train', choices=['train', 'dev', 'test'])
args = parser.parse_args()
data = dict()
for file_name in listdir(join(args.data_dir, '%s-labels' % args.dataset_split)):
file_number = int(''.join(list(filter(str.isdigit, file_name))))
with open(join(args.data_dir, '%s-labels' % args.dataset_split, file_name), 'r') as file:
reader = csv.reader(file, delimiter='\t')
for i, row in enumerate(reader):
if row[2] == 'non-propaganda':
data[(file_number, int(row[1]))] = [0]
elif row[2] == 'propaganda':
data[(file_number, int(row[1]))] = [1]
else:
data[(file_number, int(row[1]))] = [-1]
for file_name in listdir(join(args.data_dir, '%s-articles' % args.dataset_split)):
file_number = int(''.join(list(filter(str.isdigit, file_name))))
with open(join(args.data_dir, '%s-articles' % args.dataset_split, file_name), 'r') as file:
for i, line in enumerate(file):
data[(file_number, i + 1)].insert(0, line.strip())
with open(join(args.data_dir, '%s-data.tsv' % args.dataset_split), 'w') as file:
writer = csv.writer(file, delimiter='\t')
writer.writerow(['file_number', 'line_number', 'text', 'label'])
for file_number, line_number in data:
writer.writerow([file_number, line_number] + data[(file_number, line_number)])