-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_utils.py
210 lines (161 loc) · 5.21 KB
/
dataset_utils.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from preprocess import *
from wordvec_utils import *
import numpy as np
import nltk, random
from sklearn.metrics.pairwise import cosine_similarity
import random, copy
def create_one_training_example(full_text_example, max_len, wv_dict, dim=300):
text = full_text_example.lower()
words = text.split()
bag = []
mywords = []
count = 0
for word in words:
if count == max_len:
break
if word in wv_dict.vocab.keys():
v = get_wordvector(word,wv_dict,dim)
if v is not None:
count += 1
bag.append(list(v))
mywords.append(word)
for i in range(max_len-count):
bag.append(list(np.zeros(dim)))
return mywords, np.asarray(bag)
def inplace_shuffle(a,b):
c = list(zip(a, b))
random.shuffle(c)
a, b = zip(*c)
return a,b
def get_vocab_dict(fname):
f = open(fname)
lines = f.readlines()
f.close()
vocab_dict = {}
for line in lines:
words = line.split()
for word in words:
try:
vocab_dict[word] += 1
except:
vocab_dict[word] = 1
return vocab_dict
def custom_key(item):
return item[1]
def create_data4lstm_allvsone_mtl(task, train_category, test_category, wv_dict, Tx=30, Ty=1, dim=300,min_count=2750):
# TRAIN
pos = []
neg = []
for category in train_category:
f_bags_pos = open("data/TASKS/"+task+"/"+category+"/pos")
f_bags_neg = open("data/TASKS/"+task+"/"+category+"/neg")
one_pos = f_bags_pos.readlines()
one_neg = f_bags_neg.readlines()
#bags = pos + neg
f_bags_pos.close()
f_bags_neg.close()
pos.extend(one_pos)
neg.extend(one_neg)
random.shuffle(pos)
random.shuffle(neg)
pos_orig_copy = copy.deepcopy(pos)
neg_orig_copy = copy.deepcopy(neg)
while len(pos) < min_count:
random.shuffle(pos_orig_copy)
extra = copy.deepcopy(pos_orig_copy[:(min_count-len(pos))])
pos.extend(extra)
while len(neg) < min_count:
random.shuffle(neg_orig_copy)
extra = copy.deepcopy(neg_orig_copy[:(min_count-len(neg))])
neg.extend(extra)
min_num = min(len(pos), len(neg))
bag_pos = []
for text in pos[:min_num]:
bag_pos.append(create_one_training_example(text, Tx, wv_dict, dim=dim)[1])
bag_neg = []
for text in neg[:min_num]:
bag_neg.append(create_one_training_example(text, Tx, wv_dict, dim=dim)[1])
pos_labels = []
for i in range(len(bag_pos)):
pos_labels.append([1,0])
neg_labels = []
for i in range(len(bag_neg)):
neg_labels.append([0,1])
X_train = bag_pos + bag_neg
Y_train = pos_labels + neg_labels
if len(X_train) > 0:
(X_train,Y_train) = inplace_shuffle(X_train,Y_train)
Xoh = np.asarray(X_train)
Yoh = np.asarray(Y_train)
Yoh = np.reshape(Yoh, (Yoh.shape[0],1,2))
# TEST
f_bags_pos = open("data/TASKS/"+task+"/"+test_category+"/pos")
f_bags_neg = open("data/TASKS/"+task+"/"+test_category+"/neg")
pos = f_bags_pos.readlines()
neg = f_bags_neg.readlines()
bags = pos + neg
f_bags_pos.close()
f_bags_neg.close()
min_num = max(len(pos), len(neg)) # take all
bag_pos = []
for text in pos[:min_num]:
bag_pos.append(create_one_training_example(text, Tx, wv_dict, dim=dim)[1])
bag_neg = []
for text in neg[:min_num]:
bag_neg.append(create_one_training_example(text, Tx, wv_dict, dim=dim)[1])
pos_labels = []
for i in range(len(bag_pos)):
pos_labels.append([1,0])
neg_labels = []
for i in range(len(bag_neg)):
neg_labels.append([0,1])
X_test = bag_pos + bag_neg
Y_test = pos_labels + neg_labels
if len(X_test) > 0:
(X_test,Y_test) = inplace_shuffle(X_test,Y_test)
Xoh_test = np.asarray(X_test)
Yoh_test = np.asarray(Y_test)
return Xoh, Yoh, Xoh_test, Yoh_test
def create_data4lstm_DA_mtl(train_category, wv_dict, Tx=75, Ty=1, dim=300, min_count=650):
# TRAIN
bag_label = []
for index in range(len(train_category)):
category = train_category[index]
f_bags = open("data/domains_unlabeled/"+category)
lines = f_bags.readlines()
f_bags.close()
bags = []
labels = []
for text in lines:
bags.append(create_one_training_example(text, Tx, wv_dict, dim=dim)[1])
label = [0]*len(train_category)
label[index] = 1
labels.append(label)
random.shuffle(bags)
if min_count > len(bags):
extra = copy.deepcopy(bags[:(min_count-len(bags))])
bags.extend(extra)
for i in range(min_count-len(bags)):
label = [0]*len(train_category)
label[index] = 1
labels.append(label)
bag_label.append((bags,labels))
x = [len(y[0]) for y in bag_label]
min_num = min(x)
ov_bags = []
ov_labels = []
for bl in bag_label:
bags,labels = bl
ov_bags.extend(bags[:min_num])
ov_labels.extend(labels[:min_num])
(ov_bags,ov_labels) = inplace_shuffle(ov_bags,ov_labels)
X_train = ov_bags[:int(len(ov_bags)*0.85)]
Y_train = ov_labels[:int(len(ov_labels)*0.85)]
X_test = ov_bags[int(len(ov_bags)*0.85):]
Y_test = ov_labels[int(len(ov_labels)*0.85):]
X_train = np.asarray(X_train)
Y_train = np.asarray(Y_train)
X_test = np.asarray(X_test)
Y_test = np.asarray(Y_test)
Y_train = np.reshape(Y_train, (Y_train.shape[0],1,len(train_category)))
return X_train, Y_train, X_test, Y_test