-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUtils.py
More file actions
283 lines (268 loc) · 13.4 KB
/
Utils.py
File metadata and controls
283 lines (268 loc) · 13.4 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import concurrent.futures
import pickle
import numpy as np
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import text_to_word_sequence
def multi_sequences_padding(all_sequences, max_sentence_len=50, max_num_utterance=10):
PAD_SEQUENCE = [0] * max_sentence_len
padded_sequences = []
sequences_length = []
for sequences in all_sequences:
sequences_len = len(sequences)
sequences_length.append(get_sequences_length(sequences, maxlen=max_sentence_len))
if sequences_len < max_num_utterance:
sequences += [PAD_SEQUENCE] * (max_num_utterance - sequences_len)
sequences_length[-1] += [0] * (max_num_utterance - sequences_len)
else:
sequences = sequences[-max_num_utterance:]
sequences_length[-1] = sequences_length[-1][-max_num_utterance:]
sequences = pad_sequences(sequences, padding='post', maxlen=max_sentence_len)
padded_sequences.append(sequences)
return padded_sequences, sequences_length
def get_sequences_length(sequences, maxlen):
sequences_length = [min(len(sequence), maxlen) for sequence in sequences]
return sequences_length
def generate_data_with_random_samples():
# generate negative samples randomly
# In training set, for each sample, we randomly sample a response as a negative candidate
# In development and test set, for each sample, we randomly sample 9 responses as negative candidates and we add a "EOS" response as a candidate to let model select when to stop
import random
import pickle
vocab = {}
positive_data = []
EOS_ID = 7
with open("./data/sample_vocab.txt", "r", encoding="utf-8") as fr:
for idx, line in enumerate(fr):
line = line.strip().split("\t")
vocab[line[0]] = idx + 1
with open("./data/sample_data.txt", "r", encoding="utf-8") as fr:
tmp = []
for line in fr:
line = line.strip()
if len(line) > 0:
line = line.split("\t")
if line[0] == "narrative":
tmp.append(line[1])
elif line[0] == "script":
tmp.append(line[1])
else:
narrative = tmp[0]
context = tmp[1:]
narrative_id = [vocab.get(word, 0) for word in narrative.split()]
context_id = [[vocab.get(word, 0) for word in sent.split()] for sent in context]
if len(narrative_id) == 0 or len(context_id) == 0:
continue
data = [context_id, narrative_id, 1]
positive_data.append(data)
tmp = []
random.shuffle(positive_data)
print("all suitable sessions: ", len(positive_data))
train_num = int(len(positive_data) * 0.9)
dev_test_num = int(len(positive_data) * 0.05)
train, dev, test = positive_data[:train_num], positive_data[train_num: train_num + dev_test_num], positive_data[train_num + dev_test_num:]
train_all, dev_all, test_all = [], [], []
for context_id, narrative_id, _ in train:
num_context = len(context_id)
for i in range(1, num_context):
context = context_id[:i]
response = context_id[i]
train_all.append([context, response, narrative_id, 1])
flag = True
while flag:
random_idx = random.randint(0, len(positive_data) - 1)
random_context = positive_data[random_idx][0]
random_idx_2 = random.randint(0, len(random_context) - 1)
random_response = random_context[random_idx_2]
if len(response) != len(random_response):
flag = False
train_all.append([context, random_response, narrative_id, 0])
else:
for idx, wid in enumerate(response):
if wid != random_response[idx]:
flag = False
train_all.append([context, random_response, narrative_id, 0])
break
print(train_all[0], train_all[1])
for context_id, narrative_id, _ in dev:
num_context = len(context_id)
for i in range(1, num_context):
context = context_id[:i]
response = context_id[i]
dev_all.append([context, response, narrative_id, 1])
count = 0
negative_samples = []
while count < 9:
random_idx = random.randint(0, len(positive_data) - 1)
random_context = positive_data[random_idx][0]
random_idx_2 = random.randint(0, len(random_context) - 1)
random_response = random_context[random_idx_2]
if random_response not in negative_samples and random_response != [EOS_ID]:
if len(response) != len(random_response):
dev_all.append([context, random_response, narrative_id, 0])
count += 1
negative_samples.append(random_response)
else:
for idx, wid in enumerate(response):
if wid != random_response[idx]:
dev_all.append([context, random_response, narrative_id, 0])
count += 1
negative_samples.append(random_response)
break
if response == [EOS_ID]:
dev_all.append([context, [EOS_ID], narrative_id, 1])
else:
dev_all.append([context, [EOS_ID], narrative_id, 0])
print(dev_all[0], dev_all[1], dev_all[2])
for context_id, narrative_id, _ in test:
num_context = len(context_id)
for i in range(1, num_context):
context = context_id[:i]
response = context_id[i]
test_all.append([context, response, narrative_id, 1])
count = 0
negative_samples = []
while count < 9:
random_idx = random.randint(0, len(positive_data) - 1)
random_context = positive_data[random_idx][0]
random_idx_2 = random.randint(0, len(random_context) - 1)
random_response = random_context[random_idx_2]
if random_response not in negative_samples and random_response != [EOS_ID]:
if len(response) != len(random_response):
test_all.append([context, random_response, narrative_id, 0])
negative_samples.append(random_response)
count += 1
else:
for idx, id in enumerate(response):
if id != random_response[idx]:
test_all.append([context, random_response, narrative_id, 0])
negative_samples.append(random_response)
count += 1
break
if response == [EOS_ID]:
test_all.append([context, [EOS_ID], narrative_id, 1])
else:
test_all.append([context, [EOS_ID], narrative_id, 0])
print(test_all[0], test_all[1], test_all[2])
context, response, narrative, label = [], [], [], []
print("train size: ", len(train_all))
for data in train_all:
context.append(data[0])
response.append(data[1])
narrative.append(data[2])
label.append(data[3])
train = [context, response, narrative, label]
pickle.dump(train, open("./data/train.multi.pkl", "wb"))
context, response, narrative, label = [], [], [], []
print("dev size: ", len(dev_all))
for data in dev_all:
context.append(data[0])
response.append(data[1])
narrative.append(data[2])
label.append(data[3])
dev = [context, response, narrative, label]
pickle.dump(dev, open("./data/dev.multi.pkl", "wb"))
context, response, narrative, label = [], [], [], []
print("test size: ", len(test_all))
for data in test_all:
context.append(data[0])
response.append(data[1])
narrative.append(data[2])
label.append(data[3])
test = [context, response, narrative, label]
pickle.dump(test, open("./data/test.multi.pkl", "wb"))
def generate_data_with_solr_samples():
# generate negative samples from solr
# this is only for development and test set since training set has only one negative sample
import pickle
import pysolr
import jieba
EOS_ID = 7
def query_comt(post, num):
# the format of the solr data: "ut1: xxxxx, ut2: xxxxx", where ut1 is the index
solr = pysolr.Solr('xxxxxx', timeout=10) # write your Solr address
post = "ut1:(" + post + ")"
results = solr.search(q=post, **{'rows': num}) # rows equal to the number of pairs you want to retrieve
return results
vocab = {}
vocab_id2word = {}
with open("./data/vocab.txt", "r", encoding="utf-8") as fr:
for idx, line in enumerate(fr):
line = line.strip().split("\t")
vocab[line[0]] = idx + 1
vocab_id2word[idx + 1] = line[0]
dev = pickle.load(open("./data/dev.multi.pkl", "rb"))
context, response, narrative, label = dev[0], dev[1], dev[2], dev[3]
num = len(response)
dev_all = []
for i in range(num):
# One positive sample, nine negative samples and a "EOS" sample. 1 + 9 + 1 = 11
if i % 11 == 0 and int(label[i]) == 1:
count = 0
context_ = context[i]
pos_response = "".join([vocab_id2word[x] for x in response[i]])
last_ut = "".join([vocab_id2word[x] for x in context_[-1]]).replace(".", "").replace("?", "").replace("\"", "").replace(":", "")
dev_all.append([context[i], response[i], narrative[i], 1])
negative_samples = query_comt(last_ut, 15)
for result in negative_samples:
if result['ut2'] != pos_response:
negtive_sample = [vocab[x] for x in jieba.lcut(result['ut2']) if x != ' ' and x != '\xa0' and x != '\u3000']
dev_all.append([context[i], negtive_sample, narrative[i], 0])
count += 1
if count == 8:
break
if count != 8:
last = 8 - count
for j in range(last):
negtive_sample = response[i + 1 + j]
dev_all.append([context[i], negtive_sample, narrative[i], 0])
if response[i] == [EOS_ID]:
dev_all.append([context[i], [EOS_ID], narrative[i], 1])
else:
dev_all.append([context[i], [EOS_ID], narrative[i], 0])
test = pickle.load(open("./data/test.multi.pkl", "rb"))
context, response, narrative, label = test[0], test[1], test[2], test[3]
num = len(response)
test_all = []
print("start test")
for i in range(num):
if i % 11 == 0 and int(label[i]) == 1:
count = 0
context_ = context[i]
pos_response = "".join([vocab_id2word[x] for x in response[i]])
last_ut = "".join([vocab_id2word[x] for x in context_[-1]]).replace(".", "").replace("?", "").replace("\"", "").replace(":", "")
test_all.append([context[i], response[i], narrative[i], 1])
negative_samples = query_comt(last_ut, 15)
for result in negative_samples:
if result['ut2'] != pos_response:
negtive_sample = [vocab[x] for x in jieba.lcut(result['ut2']) if x != ' ' and x != '\xa0' and x != '\u3000']
test_all.append([context[i], negtive_sample, narrative[i], 0])
count += 1
if count == 8:
break
if count != 8:
last = 8 - count
for j in range(last):
negtive_sample = response[i + 1 + j]
test_all.append([context[i], negtive_sample, narrative[i], 0])
if response[i] == [EOS_ID]:
test_all.append([context[i], [EOS_ID], narrative[i], 1])
else:
test_all.append([context[i], [EOS_ID], narrative[i], 0])
context, response, narrative, label = [], [], [], []
print("dev size: ", len(dev_all))
for data in dev_all:
context.append(data[0])
response.append(data[1])
narrative.append(data[2])
label.append(data[3])
dev = [context, response, narrative, label]
pickle.dump(dev, open("./data/dev.pkl", "wb"))
context, response, narrative, label = [], [], [], []
print("test size: ", len(test_all))
for data in test_all:
context.append(data[0])
response.append(data[1])
narrative.append(data[2])
label.append(data[3])
test = [context, response, narrative, label]
pickle.dump(test, open("./data/test.pkl", "wb"))