-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
210 lines (163 loc) · 5.8 KB
/
util.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
import math
import re
import _pickle
import numpy as np
from tqdm import tqdm
from math import log, log2
def load_pickle(pickle_id2word):
"""load dictionary
:param id_to_word: dictionary(id->word)
:param word_to_word: dictionary(word->id)
:return: id2word, word2id
"""
word_to_id = {}
fp = open(pickle_id2word, "rb")
id_to_word = _pickle.load(fp)
for index in id_to_word:
word_to_id[id_to_word[index]] = index
return id_to_word, word_to_id
def load_matrix(file_matrix, V):
"""load matrix
:param file_matrix: path of pre-trained matrix (output file)
:param V: vocab size
:return: matrix(list)
"""
matrix = [[0 for _ in range(V)] for _ in range(V)]
with open(file_matrix) as fp:
for line in fp:
target_id, context_id_values = line.strip().split("\t")
context_id_values = context_id_values.split()
for context_id_value in context_id_values:
context_id, value = context_id_value.split(":")
matrix[int(target_id)][int(context_id)] += float(value)
return matrix
def create_co_matrix(file_path, word_to_id, vocab_size, window_size):
"""create co-occur matrix
:param corpus: corpus(fixed into id)
:param vocab_size: vocab size
:param window_size: windows size for counting
:return: cooccur matrix
"""
V = len(word_to_id)
C = [[0 for _ in range(V)] for _ in range(V)]
with open(file_path) as fp:
for sentence in fp:
words = re.sub(r"\n", "", sentence).split()
for idx, word in enumerate(words):
if word in word_to_id:
word_id = word_to_id[word]
else:
continue
for i in range(1, window_size + 1):
left_idx = idx - i
right_idx = idx + i
if left_idx >= 0:
left_word = words[left_idx]
if left_word in word_to_id:
left_word_id = word_to_id[left_word]
C[word_id][left_word_id] += 1
if right_idx < len(words):
right_word = words[right_idx]
if right_word in word_to_id:
right_word_id = word_to_id[right_word]
C[word_id][right_word_id] += 1
return C
def threshold_cooccur(C, threshold):
"""truncate cooccur matrix by threshold value
c = c if c > threshold else 0
:return: fixed cooccur matrix C
"""
for i in range(len(C)):
cooccur_each = C[i]
C[i] = [c if c >= threshold else 0 for c in cooccur_each]
return C
def absolute_discounting(C, i, j, d):
"""SMOOTHING: absolute discounting
:param C: cooccur matrix
:param i, j: index
:param d: discounting value (0, 1)
:param V: vocab. size
:param N0: number of words count[word]==0
:return: smoothed value
"""
if C[i][j] > 0:
return C[i][j] - d
else:
V = len(C)
C_each_nonzero = [1 if c > 0 else 0 for c in C[i]]
N0 = V - sum(C_each_nonzero)
return d * (V - N0) / N0
def sppmi(C, k, eps=1e-8, has_abs_dis=False, has_cds=False):
"""compute Shifted Positive PMI (SPPMI)
:param C: cooccur matrix
:param k: number of negative samples in w2v sgns
:param has_abs_dis: bool, do absolute discounting smoothing or not
:return: SPPMI matrix
"""
V = len(C)
M = []
Nc = [sum(cooccur_each) for cooccur_each in C]
N = sum(Nc)
if has_abs_dis:
# compute constant value d
size = len(C) * len(C[0])
C_each_morethan1 = [sum([1 if c >= 1 else 0 for c in C_each]) for C_each in C]
C_each_morethan2 = [sum([1 if c >= 2 else 0 for c in C_each]) for C_each in C]
n1 = size - sum(C_each_morethan1)
n2 = size - sum(C_each_morethan2)
d = n1 / (n1 + 2 * n2)
if has_cds:
# Context Distributional Smoothing
C_cds = [[c ** 0.75 for c in C[i]] for i in range(V)]
Nc_cds = [sum(cooccur_each) for cooccur_each in C_cds]
N_cds = sum(Nc_cds)
else:
Nc_cds = Nc
N_cds = N
for i in tqdm(range(V)):
M_each = []
for j in range(V):
Cwc = absolute_discounting(C, i, j, d) if has_abs_dis else C[i][j]
shifted_pmi = log2(Cwc * N_cds / (Nc[i] * Nc_cds[j] + eps) + eps)
shifted_positive_pmi = max(0, shifted_pmi - log(k))
M_each.append(shifted_positive_pmi)
M.append(M_each)
return M
def cos_similarity(x, y, eps=1e-8):
"""compute cos similarity
:param x, y: vector
:param eps: tiny value to avoid deviding 0
:return: cos similarity
"""
nx = x / (np.sqrt(np.sum(x ** 2)) + eps)
ny = y / (np.sqrt(np.sum(y ** 2)) + eps)
return np.dot(nx, ny)
def most_similar(query, word_to_id, id_to_word, word_matrix, top=5):
"""search most similar top-k words
:param query: query word
:param word_to_id: dictionary(word->id)
:param id_to_word: dictionary(id->word)
:param word_matrix: wordvec
:param top: top-k
:return: top-k words sorted cos-similarity
"""
if query not in word_to_id:
print("%s is not found" % query)
return
print("\n[query] " + query)
query_id = word_to_id[query]
query_vec = word_matrix[query_id]
vocab_size = len(id_to_word)
if -1 in id_to_word:
vocab_size -= 1
similarity = np.zeros(vocab_size)
for i in range(vocab_size):
similarity[i] = cos_similarity(word_matrix[i], query_vec)
count = 0
for i in (-1 * similarity).argsort():
if id_to_word[i] == query:
continue
print(" %s: %.3f" % (id_to_word[i], similarity[i]))
count += 1
if count >= top:
return