-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWord_Embedding_Helper.py
154 lines (143 loc) · 6.09 KB
/
Word_Embedding_Helper.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
import pickle
import numpy as np
import os
from utilities.thulac import Thulac
from utilities.SavedData import getPath
class HaoxiEmbedding(object):
word_num = 0
vec_len = 0
word2id = None
vec = None
dictionary = None
small_word_num = 0
small_word2id = None
small_vec = None
def __init__(self, dictionary, civil=False, ):
# print("begin to load word embedding")
self.dictionary = dictionary
if not civil:
self.embedding_bin = getPath('word2vec_bin')
self.small_word2id_path = os.path.join(self.embedding_bin, 'small_word2id.pkl')
self.small_vec_nor_path = os.path.join(self.embedding_bin, 'small_vec_nor.npy')
else:
self.embedding_bin = getPath('word2vec_bin_civil')
self.small_word2id_path = os.path.join(self.embedding_bin, 'small_word2id_civil.pkl')
self.small_vec_nor_path = os.path.join(self.embedding_bin, 'small_vec_nor_civil.npy')
if os.path.exists(self.small_word2id_path):
with open(self.small_word2id_path, 'rb') as f:
self.small_word_num, self.vec_len = pickle.load(f)
self.small_word2id = pickle.load(f)
self.small_vec = np.load(self.small_vec_nor_path)
elif dictionary == None:
with open(os.path.join(self.embedding_bin, 'word2id.pkl'), 'rb') as f:
self.small_word_num, self.vec_len = pickle.load(f)
self.small_word2id = pickle.load(f)
self.small_vec = np.load(os.path.join(self.embedding_bin, 'vec_nor.npy'))
else:
print("loading haoxi embeddings")
with open(os.path.join(self.embedding_bin, 'word2id.pkl'), "rb") as f:
(self.word_num, self.vec_len) = pickle.load(f)
self.word2id = pickle.load(f)
self.vec = np.load(os.path.join(self.embedding_bin, 'vec_nor.npy'))
print("load word embedding succeed")
self.save_small_embedding()
def transform_word(self, word, id=False):
if not id:
if self.word2id == None:
try:
return self.small_vec[self.small_word2id[word]].astype(dtype=np.float32)
except:
return self.small_vec[self.small_word2id['<UNK>']].astype(dtype=np.float32)
try:
return self.vec[self.word2id[word]].astype(dtype=np.float32)
except:
return self.vec[self.word2id['<UNK>']].astype(dtype=np.float32)
else:
if self.word2id == None:
try:
return self.small_word2id[word]
except:
return self.small_word2id['<UNK>']
try:
return self.word2id[word]
except:
return self.word2id['<UNK>']
def _word2id(self, word):
try:
return self.word2id[word]
except:
return self.word2id['UNK']
def transform_setence(self, sentence):
sentence = sentence.split()
return [self.transform_word(word) for word in sentence]
def transform(self, sentences):
'''transform sentences into indexs, accecpt sentence string list or generator, sentence should be tokenized by whitespace'''
for sentence in sentences:
word_index_list = self.transform_setence(sentence)
yield word_index_list, len(word_index_list)
def save_small_embedding(self):
self.small_word2id = self.dictionary.vocabulary_._mapping
self.small_vec = [self.vec[self._word2id("UNK")]]
print("saving small vec data")
small_id2word = {id: word for word, id in self.small_word2id.items()}
for i in range(len(small_id2word)):
if i == 0:
continue
self.small_vec.append(self.vec[self._word2id(small_id2word[i])])
self.small_vec = np.array(self.small_vec, dtype=np.float32)
np.save(self.small_vec_nor_path, self.small_vec)
with open(self.small_word2id_path, 'wb') as f:
pickle.dump([len(self.small_word2id), self.vec_len], f)
pickle.dump(self.small_word2id, f)
print('saving finished')
def get_cause_embedding_inistializer(self, causes, vec=True, id=True, name=False):
'''cause [[num_words] num_causes]'''
cause_vecs = []
cause_ids = []
names = []
fenci = Thulac()
causes = [fenci.cut(cause) for cause in causes]
pad_word = self.transform_word('<UNK>', id=True)
for cause in causes:
cause_vec = np.array([0 for _ in range(self.vec_len)], dtype=np.float32)
cause_id = []
cause = clear_cause(cause)
if name:
names.append(cause)
for word in cause:
if vec:
cause_vec += self.transform_word(word)
else:
cause_id.append(self.transform_word(word, id=id))
if vec:
if np.linalg.norm(cause_vec) != 0:
cause_vec /= np.linalg.norm(cause_vec)
cause_vecs.append(cause_vec)
else:
cause_ids.append(cause_id)
if name:
return names
if vec:
return np.array(cause_vecs)
else:
max_length = 0
for cause_id in cause_ids:
if len(cause_id) > max_length:
max_length = len(cause_id)
max_length += 5
cause_ids_prime = []
cause_ids_length = []
for cause_id in cause_ids:
cause_ids_length.append(len(cause_id))
cause_id += (max_length - len(cause_id)) * [pad_word]
cause_ids_prime.append(cause_id)
return np.array(cause_ids_prime), np.array(cause_ids_length)
def clear_cause(cause):
# check
if cause[-1][-1] == "罪" and len(cause[-1]) > 1:
last_word = cause.pop()
cause += [last_word[:-1] + '罪']
if cause[-1][-2:] == '纠纷' and len(cause[-1]) > 2:
last_word = cause.pop()
cause += [last_word[:-1] + '纠纷']
return cause