-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_w2v.py
48 lines (37 loc) · 1.51 KB
/
eval_w2v.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
import numpy as np
class Evaluate:
def __init__(self, embedding_file_path, test_file_path, results_path):
self.embedding_file = open(embedding_file_path, 'r')
self.test_file = open(test_file_path, 'r')
self.results_path = results_path
self.load_embedding()
self.evaluate()
def load_embedding(self):
self.E = {}
for line in self.embedding_file:
word, *vec = line.split()
self.E[word] = np.array(vec, dtype=float)
def evaluate(self):
with open(self.results_path, 'w') as file:
counter = 0
i = 0
for line in self.test_file:
i += 1
word1, word2, word3 = line.split()
# Ignorer les mots qui n'ont pas d'embedding
if set([word1, word2, word3]).issubset(self.E.keys()):
if self.similarity(word1, word2) > self.similarity(word1, word3):
file.write(f"{word1} {word2} {word3} 1\n")
counter += 1
else:
file.write(f"{word1} {word2} {word3} 0\n")
self.accuracy = counter / i
print(f"Accuracy: {counter / i}")
def similarity(self, word1, word2):
"""
Calculer la similarité cosinus:
cos(a, b) = (a @ b) / (||a|| * ||b||)
"""
num = np.dot(self.E[word1], self.E[word2])
denom = np.linalg.norm(self.E[word1]) * np.linalg.norm(self.E[word2])
return num / denom