-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathword2vec_v2.0.py
421 lines (367 loc) · 15.1 KB
/
word2vec_v2.0.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
__author__ = 'multiangle'
import math
import File_Interface as FI
from operator import itemgetter as _itemgetter
import numpy as np
import jieba
from sklearn import preprocessing
from collections import Counter
import numpy as np
class Word2Vec():
def __init__(self, vec_len=15000, learn_rate=0.025, win_len=5, model='cbow'):
self.cutted_text_list = None
self.vec_len = vec_len
self.learn_rate = learn_rate
self.win_len = win_len
self.model = model
self.word_dict = None # each element is a dict, including: word,possibility,vector,huffmancode
self.huffman = None # the object of HuffmanTree
def Load_Word_Freq(self,word_freq_path):
# load the info of word frequence
# will generate a word dict
if self.word_dict is not None:
raise RuntimeError('the word dict is not empty')
word_freq = FI.load_pickle(word_freq_path)
self.__Gnerate_Word_Dict(word_freq)
def __Gnerate_Word_Dict(self,word_freq):
# generate a word dict
# which containing the word, freq, possibility, a random initial vector and Huffman value
if not isinstance(word_freq,dict) and not isinstance(word_freq,list):
raise ValueError('the word freq info should be a dict or list')
word_dict = {}
if isinstance(word_freq,dict):
# if word_freq is in type of dictionary
sum_count = sum(word_freq.values())
for word in word_freq:
temp_dict = dict(
word = word,
freq = word_freq[word],
possibility = word_freq[word]/sum_count,
vector = np.random.random([1,self.vec_len]),
Huffman = None
)
word_dict[word] = temp_dict
else:
# if word_freq is in type of list
freq_list = [x[1] for x in word_freq]
sum_count = sum(freq_list)
for item in word_freq:
temp_dict = dict(
word = item[0],
freq = item[1],
possibility = item[1]/sum_count,
vector = np.random.random([1,self.vec_len]),
Huffman = None
)
word_dict[item[0]] = temp_dict
self.word_dict = word_dict
def Import_Model(self,model_path):
model = FI.load_pickle(model_path) # a dict, {'word_dict','huffman','vec_len'}
self.word_dict = model.word_dict
self.huffman = model.huffman
self.vec_len = model.vec_len
self.learn_rate = model.learn_rate
self.win_len = model.win_len
self.model = model.model
def Export_Model(self,model_path):
data=dict(
word_dict = self.word_dict,
huffman = self.huffman,
vec_len = self.vec_len,
learn_rate = self.learn_rate,
win_len = self.win_len,
model = self.model
)
FI.save_pickle(data,model_path)
def Train_Model(self,text_list):
# generate the word_dict and huffman tree
if self.huffman==None:
# if the dict is not loaded, it will generate a new dict
if self.word_dict==None :
wc = WordCounter(text_list)
self.__Gnerate_Word_Dict(wc.count_res.larger_than(5))
self.cutted_text_list = wc.text_list
# generate a huffman tree according to the possibility of words
self.huffman = HuffmanTree(self.word_dict,vec_len=self.vec_len)
print('word_dict and huffman tree already generated, ready to train vector')
# start to train word vector
before = (self.win_len-1) >> 1
after = self.win_len-1-before
if self.model=='cbow':
method = self.__Deal_Gram_CBOW
else:
method = self.__Deal_Gram_SkipGram
if self.cutted_text_list:
# if the text has been cutted
total = self.cutted_text_list.__len__()
count = 0
for line in self.cutted_text_list:
line_len = line.__len__()
for i in range(line_len):
method(line[i],line[max(0,i-before):i]+line[i+1:min(line_len,i+after+1)])
count += 1
print('{c} of {d}'.format(c=count,d=total))
else:
# if the text has note been cutted
for line in text_list:
line = list(jieba.cut(line,cut_all=False))
line_len = line.__len__()
for i in range(line_len):
method(line[i],line[max(0,i-before):i]+line[i+1:min(line_len,i+after+1)])
print('word vector has been generated')
def __Deal_Gram_CBOW(self,word,gram_word_list):
if not self.word_dict.__contains__(word):
return
word_huffman = self.word_dict[word]['Huffman']
gram_vector_sum = np.zeros([1,self.vec_len])
for i in range(gram_word_list.__len__())[::-1]:
item = gram_word_list[i]
if self.word_dict.__contains__(item):
gram_vector_sum += self.word_dict[item]['vector']
else:
gram_word_list.pop(i)
if gram_word_list.__len__()==0:
return
e = self.__GoAlong_Huffman(word_huffman,gram_vector_sum,self.huffman.root)
for item in gram_word_list:
self.word_dict[item]['vector'] += e
self.word_dict[item]['vector'] = preprocessing.normalize(self.word_dict[item]['vector'])
def __Deal_Gram_SkipGram(self,word,gram_word_list):
if not self.word_dict.__contains__(word):
return
word_vector = self.word_dict[word]['vector']
for i in range(gram_word_list.__len__())[::-1]:
if not self.word_dict.__contains__(gram_word_list[i]):
gram_word_list.pop(i)
if gram_word_list.__len__()==0:
return
for u in gram_word_list:
u_huffman = self.word_dict[u]['Huffman']
e = self.__GoAlong_Huffman(u_huffman,word_vector,self.huffman.root)
self.word_dict[word]['vector'] += e
self.word_dict[word]['vector'] = preprocessing.normalize(self.word_dict[word]['vector'])
def __GoAlong_Huffman(self,word_huffman,input_vector,root):
node = root
e = np.zeros([1,self.vec_len])
for level in range(word_huffman.__len__()):
huffman_charat = word_huffman[level]
q = self.__Sigmoid(input_vector.dot(node.value.T))
grad = self.learn_rate * (1-int(huffman_charat)-q)
e += grad * node.value
node.value += grad * input_vector
node.value = preprocessing.normalize(node.value)
if huffman_charat=='0':
node = node.right
else:
node = node.left
return e
def __Sigmoid(self,value):
return 1/(1+math.exp(-value))
class HuffmanTreeNode():
def __init__(self,value,possibility):
# common part of leaf node and tree node
self.possibility = possibility
self.left = None
self.right = None
# value of leaf node will be the word, and be
# mid vector in tree node
self.value = value # the value of word
self.Huffman = "" # store the huffman code
def __str__(self):
return 'HuffmanTreeNode object, value: {v}, possibility: {p}, Huffman: {h}' \
.format(v=self.value,p=self.possibility,h=self.Huffman)
class HuffmanTree():
def __init__(self, word_dict, vec_len=15000):
self.vec_len = vec_len # the length of word vector
self.root = None
word_dict_list = list(word_dict.values())
node_list = [HuffmanTreeNode(x['word'],x['possibility']) for x in word_dict_list]
self.build_tree(node_list)
# self.build_CBT(node_list)
self.generate_huffman_code(self.root, word_dict)
def build_tree(self,node_list):
# node_list.sort(key=lambda x:x.possibility,reverse=True)
# for i in range(node_list.__len__()-1)[::-1]:
# top_node = self.merge(node_list[i],node_list[i+1])
# node_list.insert(i,top_node)
# self.root = node_list[0]
while node_list.__len__()>1:
i1 = 0 # i1表示概率最小的节点
i2 = 1 # i2 概率第二小的节点
if node_list[i2].possibility < node_list[i1].possibility :
[i1,i2] = [i2,i1]
for i in range(2,node_list.__len__()): # 找到最小的两个节点
if node_list[i].possibility<node_list[i2].possibility :
i2 = i
if node_list[i2].possibility < node_list[i1].possibility :
[i1,i2] = [i2,i1]
top_node = self.merge(node_list[i1],node_list[i2])
if i1<i2:
node_list.pop(i2)
node_list.pop(i1)
elif i1>i2:
node_list.pop(i1)
node_list.pop(i2)
else:
raise RuntimeError('i1 should not be equal to i2')
node_list.insert(0,top_node)
self.root = node_list[0]
def build_CBT(self,node_list): # build a complete binary tree
node_list.sort(key=lambda x:x.possibility,reverse=True)
node_num = node_list.__len__()
before_start = 0
while node_num>1 :
for i in range(node_num>>1):
top_node = self.merge(node_list[before_start+i*2],node_list[before_start+i*2+1])
node_list.append(top_node)
if node_num%2==1:
top_node = self.merge(node_list[before_start+i*2+2],node_list[-1])
node_list[-1] = top_node
before_start = before_start + node_num
node_num = node_num>>1
self.root = node_list[-1]
def generate_huffman_code(self, node, word_dict):
# # use recursion in this edition
# if node.left==None and node.right==None :
# word = node.value
# code = node.Huffman
# print(word,code)
# word_dict[word]['Huffman'] = code
# return -1
#
# code = node.Huffman
# if code==None:
# code = ""
# node.left.Huffman = code + "1"
# node.right.Huffman = code + "0"
# self.generate_huffman_code(node.left, word_dict)
# self.generate_huffman_code(node.right, word_dict)
# use stack butnot recursion in this edition
stack = [self.root]
while (stack.__len__()>0):
node = stack.pop()
# go along left tree
while node.left or node.right :
code = node.Huffman
node.left.Huffman = code + "1"
node.right.Huffman = code + "0"
stack.append(node.right)
node = node.left
word = node.value
code = node.Huffman
# print(word,'\t',code.__len__(),'\t',node.possibility)
word_dict[word]['Huffman'] = code
def merge(self,node1,node2):
top_pos = node1.possibility + node2.possibility
top_node = HuffmanTreeNode(np.zeros([1,self.vec_len]), top_pos)
if node1.possibility >= node2.possibility :
top_node.left = node1
top_node.right = node2
else:
top_node.left = node2
top_node.right = node1
return top_node
class WordCounter():
# can calculate the freq of words in a text list
# for example
# >>> data = ['Merge multiple sorted inputs into a single sorted output',
# 'The API below differs from textbook heap algorithms in two aspects']
# >>> wc = WordCounter(data)
# >>> print(wc.count_res)
# >>> MulCounter({' ': 18, 'sorted': 2, 'single': 1, 'below': 1, 'inputs': 1, 'The': 1, 'into': 1, 'textbook': 1,
# 'API': 1, 'algorithms': 1, 'in': 1, 'output': 1, 'heap': 1, 'differs': 1, 'two': 1, 'from': 1,
# 'aspects': 1, 'multiple': 1, 'a': 1, 'Merge': 1})
def __init__(self, text_list):
self.text_list = text_list
self.stop_word = self.Get_Stop_Words()
self.count_res = None
self.Word_Count(self.text_list)
def Get_Stop_Words(self):
ret = []
ret = FI.load_pickle('./static/stop_words.pkl')
return ret
def Word_Count(self,text_list,cut_all=False):
filtered_word_list = []
count = 0
for line in text_list:
res = jieba.cut(line,cut_all=cut_all)
res = list(res)
text_list[count] = res
count += 1
filtered_word_list += res
self.count_res = MulCounter(filtered_word_list)
for word in self.stop_word:
try:
self.count_res.pop(word)
except:
pass
class MulCounter(Counter):
# a class extends from collections.Counter
# add some methods, larger_than and less_than
def __init__(self,element_list):
super().__init__(element_list)
def larger_than(self,minvalue,ret='list'):
temp = sorted(self.items(),key=_itemgetter(1),reverse=True)
low = 0
high = temp.__len__()
while(high - low > 1):
mid = (low+high) >> 1
if temp[mid][1] >= minvalue:
low = mid
else:
high = mid
if temp[low][1]<minvalue:
if ret=='dict':
return {}
else:
return []
if ret=='dict':
ret_data = {}
for ele,count in temp[:high]:
ret_data[ele]=count
return ret_data
else:
return temp[:high]
def less_than(self,maxvalue,ret='list'):
temp = sorted(self.items(),key=_itemgetter(1))
low = 0
high = temp.__len__()
while ((high-low) > 1):
mid = (low+high) >> 1
if temp[mid][1] <= maxvalue:
low = mid
else:
high = mid
if temp[low][1]>maxvalue:
if ret=='dict':
return {}
else:
return []
if ret=='dict':
ret_data = {}
for ele,count in temp[:high]:
ret_data[ele]=count
return ret_data
else:
return temp[:high]
if __name__ == '__main__':
# text = FI.load_pickle('./static/demo.pkl')
# text =[ x['dealed_text']['left_content'][0] for x in text]
data = ['Merge multiple sorted inputs into a single sorted output','The API below differs from textbook heap algorithms in two aspects']
wv = Word2Vec(vec_len=500)
wv.Train_Model(data)
# FI.save_pickle(wv.word_dict,'./static/wv.pkl')
#
# data = FI.load_pickle('./static/wv.pkl')
# x = {}
# for key in data:
# temp = data[key]['vector']
# temp = preprocessing.normalize(temp)
# x[key] = temp
# FI.save_pickle(x,'./static/normal_wv.pkl')
# x = FI.load_pickle('./static/normal_wv.pkl')
# def cal_simi(data,key1,key2):
# return data[key1].dot(data[key2].T)[0][0]
# keys=list(x.keys())
# for key in keys:
# print(key,'\t',cal_simi(x,'姚明',key))