forked from salesforce/GeDi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proc_data.py
142 lines (95 loc) · 3.3 KB
/
proc_data.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
import numpy as np
import csv
import random
def proc_and_binarize(dir):
fid = open(dir+ "/train.tsv")
train= fid.read()
train = train.split("\n")[:-1]
fid = open(dir+ "/dev.tsv")
test = fid.read()
test = test.split("\n")[:-1]
topics = ["world","sports","business","science"]
true_test = []
false_test = []
true_train = []
false_train = []
range_arr = list(range(0,len(topics)))
for i in range(0,len(test)):
line = test[i].split('\t')
label = line[0]
if not(len(line) ==3):
print("skipping " +str(i))
continue
if label[0] =="\"":
label = label[1:-1]
label = int(label)-1
text = line[2]
if text[0] =="\"":
text = text[1:-1]
if text[0] == " ":
text = text[1:]
choice_array = range_arr[:label]+range_arr[label+1:]
ps_label = random.choice(choice_array)
true_ex = topics[label] + text
false_ex = topics[ps_label] + text
true_test.append(true_ex)
false_test.append(false_ex)
for i in range(0,len(train)):
line = train[i].split('\t')
if not(len(line) ==3):
print("skipping " +str(i))
continue
label = line[0]
if label[0] =="\"":
label = label[1:-1]
label = int(label)-1
text = line[2]
if text[0] =="\"":
text = text[1:-1]
if text[0] == " ":
text = text[1:]
choice_array = range_arr[:label]+range_arr[label+1:]
ps_label = random.choice(choice_array)
true_ex = topics[label] + text
false_ex = topics[ps_label] + text
true_train.append(true_ex)
false_train.append(false_ex)
return true_train,false_train,true_test,false_test
def main():
fid = open("data/AG-news/train.csv")
text_train = fid.read()
fid = open("data/AG-news/test.csv")
text_test = fid.read()
fid.close()
csv.writer(open("data/AG-news/train.tsv", 'w+'), delimiter='\t').writerows(csv.reader(open("data/AG-news/train.csv")))
csv.writer(open("data/AG-news/dev.tsv", 'w+'), delimiter='\t').writerows(csv.reader(open("data/AG-news/test.csv")))
true_train, false_train, true_test, false_test = proc_and_binarize("data/AG-news")
random.shuffle(true_train)
random.shuffle(false_train)
random.shuffle(true_test)
random.shuffle(false_test)
false_lines = []
true_lines = []
for i in range(0,len(false_test)):
false_lines.append(false_test[i] + "\t0" + "\n")
for i in range(0,len(false_test)):
true_lines.append(true_test[i] + "\t1" + "\n")
test_lines = false_lines+true_lines
random.shuffle(test_lines)
false_lines = []
true_lines = []
for i in range(0,len(false_train)):
false_lines.append(false_train[i] + "\t0" + "\n")
for i in range(0,len(true_train)):
true_lines.append(true_train[i] + "\t1" + "\n")
train_lines = false_lines+true_lines
random.shuffle(train_lines)
train_split_all= "\n" + "".join(train_lines)
test_split_all= "\n" + "".join(test_lines)
fid = open("data/AG-news/train.tsv",'w')
fid.write(train_split_all)
fid.close()
fid = open("data/AG-news/dev.tsv",'w')
fid.write(test_split_all)
fid.close()
main()