-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget_vocab.py
394 lines (340 loc) · 13.1 KB
/
get_vocab.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# coding: utf-8
import time
import json
import copy
from copy import deepcopy
import re
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
class Lang:
"""
class to save the vocab and two dict: the word->index and index->word
"""
def __init__(self):
self.word2index = {}
self.word2count = {}
self.index2word = []
self.n_words = 0 # Count word tokens
self.num_start = 0
def add_sen_to_vocab(self, sentence): # add words of sentence to vocab
for word in sentence:
if re.search("N\d+|NUM|\d+", word):
continue
if word not in self.index2word:
self.word2index[word] = self.n_words
self.word2count[word] = 1
self.index2word.append(word)
self.n_words += 1
else:
self.word2count[word] += 1
def trim(self, min_count): # trim words below a certain count threshold
keep_words = []
for k, v in self.word2count.items():
if v >= min_count:
keep_words.append(k)
print('keep_words %s / %s = %.4f' % (
len(keep_words), len(self.index2word), len(keep_words) / len(self.index2word)
))
# Reinitialize dictionaries
self.word2index = {}
self.word2count = {}
self.index2word = []
self.n_words = 0 # Count default tokens
for word in keep_words:
self.word2index[word] = self.n_words
self.index2word.append(word)
self.n_words += 1
def build_input_lang(self, trim_min_count): # build the input lang vocab and dict
if trim_min_count > 0:
self.trim(trim_min_count)
self.index2word = ["PAD", "NUM", "UNK"] + self.index2word
else:
self.index2word = ["PAD", "NUM"] + self.index2word
self.word2index = {}
self.n_words = len(self.index2word)
for i, j in enumerate(self.index2word):
self.word2index[j] = i
def build_output_lang(self, generate_num, copy_nums): # build the output lang vocab and dict
self.index2word = ["PAD", "EOS"] + self.index2word + generate_num + ["N" + str(i) for i in range(copy_nums)] +\
["SOS", "UNK"]
self.n_words = len(self.index2word)
for i, j in enumerate(self.index2word):
self.word2index[j] = i
def build_output_lang_for_tree(self, generate_num, copy_nums): # build the output lang vocab and dict
self.num_start = len(self.index2word)
self.index2word = self.index2word + generate_num + ["N" + str(i) for i in range(copy_nums)] + ["UNK"]
self.n_words = len(self.index2word)
for i, j in enumerate(self.index2word):
self.word2index[j] = i
def load_raw_data(filename): # load the json data to list(dict()) for MATH 23K
print("Reading lines...")
f = open(filename,'r')
js = ""
data = []
for i, s in enumerate(f):
js += s
i += 1
if i % 7 == 0: # every 7 line is a json
data_d = json.loads(js)
if "千米/小时" in data_d["equation"]:
data_d["equation"] = data_d["equation"][:-5]
data.append(data_d)
js = ""
return data
def transfer_num(data): # transfer num into "NUM"
print("Transfer numbers...")
pattern = re.compile("\d*\(\d+/\d+\)\d*|\d+\.\d+%?|\d+%?")
pairs = []
generate_nums = []
generate_nums_dict = {}
copy_nums = 0
count_empty=0
count_anno=0
for d in data:
nums = []
input_seq = []
seg = d["segmented_text"].encode("UTF-8").strip().split(" ")
equations = d["equation"][2:]
for s in seg:
pos = re.search(pattern, s)
if pos and pos.start() == 0:
nums.append(s[pos.start(): pos.end()])
input_seq.append("NUM")
if pos.end() < len(s):
input_seq.append(s[pos.end():])
else:
if len(s)>0:
input_seq.append(s)
else:
count_empty=count_empty+1
#input_seq,nums,flag=replace_inputseq_to_anno(input_seq,nums)
#if flag==1:
# count_anno+=1
if copy_nums < len(nums):
copy_nums = len(nums)
nums_fraction = []
for num in nums:
if re.search("\d*\(\d+/\d+\)\d*", num):
nums_fraction.append(num)
nums_fraction = sorted(nums_fraction, key=lambda x: len(x), reverse=True)
def seg_and_tag(st): # seg the equation and tag the num
res = []
for n in nums_fraction:
if n in st:
p_start = st.find(n)
p_end = p_start + len(n)
if p_start > 0:
res += seg_and_tag(st[:p_start])
if nums.count(n) == 1:
res.append("N"+str(nums.index(n)))
else:
res.append(n)
if p_end < len(st):
res += seg_and_tag(st[p_end:])
return res
pos_st = re.search("\d+\.\d+%?|\d+%?", st)
if pos_st:
p_start = pos_st.start()
p_end = pos_st.end()
if p_start > 0:
res += seg_and_tag(st[:p_start])
st_num = st[p_start:p_end]
if nums.count(st_num) == 1:
res.append("N"+str(nums.index(st_num)))
else:
res.append(st_num)
if p_end < len(st):
res += seg_and_tag(st[p_end:])
return res
for ss in st:
res.append(ss)
return res
out_seq = seg_and_tag(equations)
for s in out_seq: # tag the num which is generated
if s[0].isdigit() and s not in generate_nums and s not in nums:
generate_nums.append(s)
generate_nums_dict[s] = 0
if s in generate_nums and s not in nums:
generate_nums_dict[s] = generate_nums_dict[s] + 1
num_pos = []
for i, j in enumerate(input_seq):
if j == "NUM":
num_pos.append(i)
assert len(nums) == len(num_pos)
# pairs.append((input_seq, out_seq, nums, num_pos, d["ans"]))
pairs.append((input_seq, out_seq, nums, num_pos))
print("count_empty")
print(count_empty)
temp_g = []
for g in generate_nums:
if generate_nums_dict[g] >= 5:
temp_g.append(g)
print("*************************")
print("count_anno")
print(count_anno)
print("*************************")
return pairs, temp_g, copy_nums
# Return a list of indexes, one for each word in the sentence, plus EOS
UNK_vocab=[]
def indexes_from_sentence(lang, sentence, tree=False):
res = []
for word in sentence:
if len(word) == 0:
continue
if word in lang.word2index:
res.append(lang.word2index[word])
else:
res.append(lang.word2index["UNK"])
if word not in UNK_vocab:
UNK_vocab.append(word)
if "EOS" in lang.index2word and not tree:
res.append(lang.word2index["EOS"])
return res
def from_infix_to_prefix(expression):
st = list()
res = list()
priority = {"+": 0, "-": 0, "*": 1, "/": 1, "^": 2}
expression = deepcopy(expression)
expression.reverse()
for e in expression:
if e in [")", "]"]:
st.append(e)
elif e == "(":
c = st.pop()
while c != ")":
res.append(c)
c = st.pop()
elif e == "[":
c = st.pop()
while c != "]":
res.append(c)
c = st.pop()
elif e in priority:
while len(st) > 0 and st[-1] not in [")", "]"] and priority[e] < priority[st[-1]]:
res.append(st.pop())
st.append(e)
else:
res.append(e)
while len(st) > 0:
res.append(st.pop())
res.reverse()
return res
def prepare_data(pairs_trained, pairs_tested, trim_min_count, generate_nums, copy_nums, tree=False):
input_lang = Lang()
output_lang = Lang()
train_pairs = []
test_pairs = []
print("Indexing words...")
for pair in pairs_trained:
if not tree:
input_lang.add_sen_to_vocab(pair[0])
output_lang.add_sen_to_vocab(pair[1])
elif pair[-1]:
input_lang.add_sen_to_vocab(pair[0])
output_lang.add_sen_to_vocab(pair[1])
input_lang.build_input_lang(trim_min_count)
if tree:
output_lang.build_output_lang_for_tree(generate_nums, copy_nums)
else:
output_lang.build_output_lang(generate_nums, copy_nums)
for pair in pairs_trained:
num_stack = []
for word in pair[1]:
temp_num = []
flag_not = True
if word not in output_lang.index2word:
flag_not = False
for i, j in enumerate(pair[2]):
if j == word:
temp_num.append(i)
if not flag_not and len(temp_num) != 0:
num_stack.append(temp_num)
if not flag_not and len(temp_num) == 0:
num_stack.append([_ for _ in range(len(pair[2]))])
num_stack.reverse()
input_cell = indexes_from_sentence(input_lang, pair[0])
output_cell = indexes_from_sentence(output_lang, pair[1], tree)
# train_pairs.append((input_cell, len(input_cell), output_cell, len(output_cell),
# pair[2], pair[3], num_stack, pair[4]))
train_pairs.append((input_cell, len(input_cell), output_cell, len(output_cell),
pair[2], pair[3], num_stack))
print('Indexed %d words in input language, %d words in output' % (input_lang.n_words, output_lang.n_words))
print('Number of training data %d' % (len(train_pairs)))
for pair in pairs_tested:
num_stack = []
for word in pair[1]:
temp_num = []
flag_not = True
if word not in output_lang.index2word:
flag_not = False
for i, j in enumerate(pair[2]):
if j == word:
temp_num.append(i)
if not flag_not and len(temp_num) != 0:
num_stack.append(temp_num)
if not flag_not and len(temp_num) == 0:
num_stack.append([_ for _ in range(len(pair[2]))])
num_stack.reverse()
input_cell = indexes_from_sentence(input_lang, pair[0])
output_cell = indexes_from_sentence(output_lang, pair[1], tree)
# train_pairs.append((input_cell, len(input_cell), output_cell, len(output_cell),
# pair[2], pair[3], num_stack, pair[4]))
test_pairs.append((input_cell, len(input_cell), output_cell, len(output_cell),
pair[2], pair[3], num_stack))
print('Number of testind data %d' % (len(test_pairs)))
return input_lang, output_lang, train_pairs, test_pairs
data = load_raw_data("data/Math_23K.json")
pairs, generate_nums, copy_nums = transfer_num(data)
#把输出序列换成前序
temp_pairs = []
for p in pairs:
temp_pairs.append((p[0], from_infix_to_prefix(p[1]), p[2], p[3]))
pairs = temp_pairs
fold_size = int(len(pairs) * 0.2)
fold_pairs = []
for split_fold in range(4):
fold_start = fold_size * split_fold
fold_end = fold_size * (split_fold + 1)
fold_pairs.append(pairs[fold_start:fold_end])
fold_pairs.append(pairs[(fold_size * 4):])
best_acc_fold = []
word_all_vocab=[]
for fold in range(5):
pairs_tested = []
pairs_trained = []
for fold_t in range(5):
if fold_t == fold:
pairs_tested += fold_pairs[fold_t]
else:
pairs_trained += fold_pairs[fold_t]
#train_pair : input_cell, len(input_cell), output_cell, len(output_cell),pair[2], pair[3], num_stack
#input_cell 转换成数字的序列
#pair[2] 题中数字, pair[3] 题中数字的位置
#num_stack 如果答案中有没替换掉的数字,如果这个数字在题目中,把这个数字在文中是第几个数字,即nums的编号记下来,如果不在题目中,把整个nums的所有编导都记下来
#num_stack还要逆转,可能是为了逆序
input_lang, output_lang, train_pairs, test_pairs = prepare_data(pairs_trained, pairs_tested, 5, generate_nums,copy_nums, tree=True)
print(train_pairs[0])
print(input_lang.n_words)
print(output_lang.n_words)
'''
word_fold_vocab=[]
for word in input_lang.index2word:
word_fold_vocab.append(word)
word_all_vocab.append(word_fold_vocab)
vocab0=word_all_vocab[0]
vocab1=word_all_vocab[1]
vocab2=word_all_vocab[2]
vocab3=word_all_vocab[3]
vocab4=word_all_vocab[4]
final_vocab=[]
for word in vocab0:
if word in vocab1 and word in vocab2 and word in vocab3 and word in vocab4:
final_vocab.append(word)
output=open("data//vocab_5fold","w")
for word in final_vocab:
output.write(word+"\n")
'''
output=open("data//UNK_vocab","w")
for word in UNK_vocab:
output.write(word+"\n")