forked from stanfordnlp/GloVe
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
46 lines (36 loc) · 1.29 KB
/
test.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
import numpy as np
import scipy
from scipy import spatial
#print(np.__version__) #1.17.4
#print(scipy.__version__) #1.3.3
class BN_Glove(object):
def word2vec(self, glove_path, test_word):
embeddings_dict = {}
with open(glove_path, 'r') as f:
for line in f:
values = line.split()
word = values[0]
vector = np.asarray(values[1:], "float32")
embeddings_dict[word] = vector
result_vec = embeddings_dict[test_word]
return result_vec
def closest_word(self,glove_path, test_word):
def find_closest_embeddings(embedding):
return sorted(embeddings_dict.keys(), key=lambda word: spatial.distance.euclidean(embeddings_dict[word], embedding))
embeddings_dict = {}
with open(glove_path, 'r') as f:
for line in f:
values = line.split()
word = values[0]
vector = np.asarray(values[1:], "float32")
embeddings_dict[word] = vector
result = find_closest_embeddings(embeddings_dict[test_word])[:10]
return result
if __name__=="__main__":
glove_path = "bn_glove.39M.100d.txt"
word = "গ্রাম"
bng = BN_Glove()
res = bng.closest_word(glove_path, word)
print(res)
vec = bng.word2vec(glove_path, word)
print(vec)