forked from dqwang122/HeterSumGraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedding.py
144 lines (134 loc) · 5.24 KB
/
embedding.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# __author__="Danqing Wang"
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import numpy as np
from tools.logger import *
class Word_Embedding(object):
def __init__(self, path, vocab):
"""
:param path: string; the path of word embedding
:param vocab: object;
"""
logger.info("[INFO] Loading external word embedding...")
self._path = path
self._vocablist = vocab.word_list()
self._vocab = vocab
def load_my_vecs(self, k=200):
"""Load word embedding"""
word_vecs = {}
with open(self._path, encoding="utf-8") as f:
count = 0
lines = f.readlines()[1:]
for line in lines:
values = line.split(" ")
word = values[0]
count += 1
if word in self._vocablist: # whether to judge if in vocab
vector = []
for count, val in enumerate(values):
if count == 0:
continue
if count <= k:
vector.append(float(val))
word_vecs[word] = vector
return word_vecs
def add_unknown_words_by_zero(self, word_vecs, k=200):
"""Solve unknown by zeros"""
zero = [0.0] * k
list_word2vec = []
oov = 0
iov = 0
for i in range(self._vocab.size()):
word = self._vocab.id2word(i)
if word not in word_vecs:
oov += 1
word_vecs[word] = zero
list_word2vec.append(word_vecs[word])
else:
iov += 1
list_word2vec.append(word_vecs[word])
logger.info("[INFO] oov count %d, iov count %d", oov, iov)
return list_word2vec
def add_unknown_words_by_avg(self, word_vecs, k=200):
"""Solve unknown by avg word embedding"""
# solve unknown words inplaced by zero list
word_vecs_numpy = []
for word in self._vocablist:
if word in word_vecs:
word_vecs_numpy.append(word_vecs[word])
col = []
for i in range(k):
sum = 0.0
for j in range(int(len(word_vecs_numpy))):
sum += word_vecs_numpy[j][i]
sum = round(sum, 6)
col.append(sum)
zero = []
for m in range(k):
avg = col[m] / int(len(word_vecs_numpy))
avg = round(avg, 6)
zero.append(float(avg))
list_word2vec = []
oov = 0
iov = 0
for i in range(self._vocab.size()):
word = self._vocab.id2word(i)
if word not in word_vecs:
oov += 1
word_vecs[word] = zero
list_word2vec.append(word_vecs[word])
else:
iov += 1
list_word2vec.append(word_vecs[word])
logger.info("[INFO] External Word Embedding iov count: %d, oov count: %d", iov, oov)
return list_word2vec
def add_unknown_words_by_uniform(self, word_vecs, uniform=0.25, k=200):
"""Solve unknown word by uniform(-0.25,0.25)"""
list_word2vec = []
oov = 0
iov = 0
for i in range(self._vocab.size()):
word = self._vocab.id2word(i)
if word not in word_vecs:
oov += 1
word_vecs[word] = np.random.uniform(-1 * uniform, uniform, k).round(6).tolist()
list_word2vec.append(word_vecs[word])
else:
iov += 1
list_word2vec.append(word_vecs[word])
logger.info("[INFO] oov count %d, iov count %d", oov, iov)
return list_word2vec
# load word embedding
def load_my_vecs_freq1(self, freqs, pro):
word_vecs = {}
with open(self._path, encoding="utf-8") as f:
freq = 0
lines = f.readlines()[1:]
for line in lines:
values = line.split(" ")
word = values[0]
if word in self._vocablist: # whehter to judge if in vocab
if freqs[word] == 1:
a = np.random.uniform(0, 1, 1).round(2)
if pro < a:
continue
vector = []
for count, val in enumerate(values):
if count == 0:
continue
vector.append(float(val))
word_vecs[word] = vector
return word_vecs