-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_antonym.py
47 lines (29 loc) · 967 Bytes
/
predict_antonym.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
import pickle
from sklearn.metrics.pairwise import cosine_similarity
first_10=[]
def odd_one_out(words,antonym_of, word_vectors):
"""Accepts a list of words and returns the odd word"""
all_word_vectors=[]
# Generate all word embeddings for the given list
for w in words:
try:
wv=word_vectors[w]
except:
continue
all_word_vectors.append(wv)
avg_vector = word_vectors[antonym_of]
#Iterate over every word and find similarity
odd_one_out = None
for w in words:
try:
temp=word_vectors[w]
except:
continue
sim = cosine_similarity([temp],[avg_vector])
sim=round(float(sim),2)
if(sim<=0):
continue
t=[w,sim]
first_10.append(t)
#print("Similarity btw %s and avg vector is %.2f"%(w,sim))
return first_10