-
Notifications
You must be signed in to change notification settings - Fork 21
/
insurance_qa_data_helpers.py
226 lines (205 loc) · 7.35 KB
/
insurance_qa_data_helpers.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from __future__ import print_function
import numpy as np
import random
empty_vector = []
for i in range(0, 100):
empty_vector.append(float(0.0))
onevector = []
for i in range(0, 10):
onevector.append(float(1))
zerovector = []
for i in range(0, 10):
zerovector.append(float(0))
def build_vocab():
code = int(0)
vocab = {}
vocab['UNKNOWN'] = code
code += 1
for line in open('insuranceQA/train'):
items = line.strip().split(' ')
for i in range(2, 4):
words = items[i].split('_')
for word in words:
if not word in vocab:
vocab[word] = code
code += 1
for line in open('insuranceQA/test1'):
items = line.strip().split(' ')
for i in range(2, 4):
words = items[i].split('_')
for word in words:
if not word in vocab:
vocab[word] = code
code += 1
return vocab
def rand_qa(qalist):
index = random.randint(0, len(qalist) - 1)
return qalist[index]
def read_alist():
alist = []
for line in open('insuranceQA/train'):
items = line.strip().split(' ')
alist.append(items[3])
print('read_alist done ......')
return alist
def vocab_plus_overlap(vectors, sent, over, size):
global onevector
global zerovector
oldict = {}
words = over.split('_')
if len(words) < size:
size = len(words)
for i in range(0, size):
if words[i] == '<a>':
continue
oldict[words[i]] = '#'
matrix = []
words = sent.split('_')
if len(words) < size:
size = len(words)
for i in range(0, size):
vec = read_vector(vectors, words[i])
newvec = vec.copy()
#if words[i] in oldict:
# newvec += onevector
#else:
# newvec += zerovector
matrix.append(newvec)
return matrix
def load_vectors():
vectors = {}
for line in open('insuranceQA/vectors.nobin'):
items = line.strip().split(' ')
if (len(items) < 101):
continue
vec = []
for i in range(1, 101):
vec.append(float(items[i]))
vectors[items[0]] = vec
return vectors
def read_vector(vectors, word):
global empty_vector
if word in vectors:
return vectors[word]
else:
return empty_vector
#return vectors['</s>']
def load_test_and_vectors():
testList = []
for line in open('insuranceQA/test1'):
testList.append(line.strip())
vectors = load_vectors()
return testList, vectors
def load_train_and_vectors():
trainList = []
for line in open('insuranceQA/train'):
trainList.append(line.strip())
vectors = load_vectors()
return trainList, vectors
def load_data_val_10(testList, vectors, index):
x_train_1 = []
x_train_2 = []
x_train_3 = []
items = testList[index].split(' ')
x_train_1.append(vocab_plus_overlap(vectors, items[2], items[3], 200))
x_train_2.append(vocab_plus_overlap(vectors, items[3], items[2], 200))
x_train_3.append(vocab_plus_overlap(vectors, items[3], items[2], 200))
return np.array(x_train_1), np.array(x_train_2), np.array(x_train_3)
def read_raw():
raw = []
for line in open('insuranceQA/train'):
items = line.strip().split(' ')
if items[0] == '1':
raw.append(items)
return raw
def encode_sent(vocab, string, size):
x = []
words = string.split('_')
for i in range(0, size):
if words[i] in vocab:
x.append(vocab[words[i]])
else:
x.append(vocab['UNKNOWN'])
return x
def load_data_6(vocab, alist, raw, size, seq_size=200):
x_train_1 = []
x_train_2 = []
x_train_3 = []
for i in range(0, size):
items = raw[random.randint(0, len(raw) - 1)]
nega = rand_qa(alist)
x_train_1.append(encode_sent(vocab, items[2], seq_size))
x_train_2.append(encode_sent(vocab, items[3], seq_size))
x_train_3.append(encode_sent(vocab, nega, seq_size))
return np.array(x_train_1), np.array(x_train_2), np.array(x_train_3)
def load_data_val_6(testList, vocab, index, batch, seq_size=200):
x_train_1 = []
x_train_2 = []
x_train_3 = []
for i in range(0, batch):
true_index = index + i
if (true_index >= len(testList)):
true_index = len(testList) - 1
items = testList[true_index].split(' ')
x_train_1.append(encode_sent(vocab, items[2], seq_size))
x_train_2.append(encode_sent(vocab, items[3], seq_size))
x_train_3.append(encode_sent(vocab, items[3], seq_size))
return np.array(x_train_1), np.array(x_train_2), np.array(x_train_3)
def load_data_9(trainList, vectors, size):
x_train_1 = []
x_train_2 = []
y_train = []
for i in range(0, size):
pos = trainList[random.randint(0, len(trainList) - 1)]
posItems = pos.strip().split(' ')
x_train_1.append(vocab_plus_overlap(vectors, posItems[2], posItems[3], 200))
x_train_2.append(vocab_plus_overlap(vectors, posItems[3], posItems[2], 200))
y_train.append([1, 0])
neg = trainList[random.randint(0, len(trainList) - 1)]
negItems = neg.strip().split(' ')
x_train_1.append(vocab_plus_overlap(vectors, posItems[2], negItems[3], 200))
x_train_2.append(vocab_plus_overlap(vectors, negItems[3], posItems[2], 200))
y_train.append([0, 1])
return np.array(x_train_1), np.array(x_train_2), np.array(y_train)
def load_data_val_9(testList, vectors, index):
x_train_1 = []
x_train_2 = []
items = testList[index].split(' ')
x_train_1.append(vocab_plus_overlap(vectors, items[2], items[3], 200))
x_train_2.append(vocab_plus_overlap(vectors, items[3], items[2], 200))
return np.array(x_train_1), np.array(x_train_2)
def load_data_10(vectors, qalist, raw, size):
x_train_1 = []
x_train_2 = []
x_train_3 = []
items = raw[random.randint(0, len(raw) - 1)]
nega = rand_qa(qalist)
x_train_1.append(vocab_plus_overlap(vectors, items[2], items[3], 200))
x_train_2.append(vocab_plus_overlap(vectors, items[3], items[2], 200))
x_train_3.append(vocab_plus_overlap(vectors, nega, items[2], 200))
return np.array(x_train_1), np.array(x_train_2), np.array(x_train_3)
def load_data_11(vectors, qalist, raw, size):
x_train_1 = []
x_train_2 = []
x_train_3 = []
items = raw[random.randint(0, len(raw) - 1)]
nega = rand_qa(qalist)
x_train_1.append(vocab_plus_overlap(vectors, items[2], items[3], 200))
x_train_2.append(vocab_plus_overlap(vectors, items[3], items[2], 200))
x_train_3.append(vocab_plus_overlap(vectors, nega, items[2], 200))
return np.array(x_train_1), np.array(x_train_2), np.array(x_train_3)
def batch_iter(data, batch_size, num_epochs, shuffle=True):
data = np.array(data)
data_size = len(data)
num_batches_per_epoch = int(len(data)/batch_size) + 1
for epoch in range(num_epochs):
# Shuffle the data at each epoch
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_data = data[shuffle_indices]
else:
shuffled_data = data
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
yield shuffled_data[start_index:end_index]