Untitled
unknown
python
3 years ago
1.3 kB
13
Indexable
import numpy
# Local Neighbors Measure
def LNM(word: str, n_neighbors=10):
word_1960 = e_1960.wv[word]
word_2010 = e_2010.wv[word]
neighbors_1960, neighbors_2010 = [], []
# adding neighbors of 1960
neighbors_1960.extend([w[0] for w in e_1960.wv.most_similar(positive=[word_1960], topn=30)])
# adding neighbors of 2010
neighbors_2010.extend([w[0] for w in e_2010.wv.most_similar(positive=[word_2010], topn=30)])
neighbors = []
cnt = 0
for nb in neighbors_1960:
try:
# controllo se la parola esiste nel 2010
_ = e_2010.wv[nb]
neighbors.append(e_1960.wv[nb])
cnt += 1
if cnt == n_neighbors - 1:
break
except:
continue
cnt = 0
for nb in neighbors_2010:
try:
# controllo se la parola esiste nel 1960
_ = e_1960.wv[nb]
neighbors.append(e_2010.wv[nb])
cnt += 1
if cnt == n_neighbors - 1:
break
except:
continue
# word similarity with each neighbor
sim_1960 = []
sim_2010 = []
for nb in neighbors:
sim_1960.append(1-cosine(nb, word_1960))
sim_2010.append(1-cosine(nb, word_2010))
# calculate and return the similarity between the two vectors
return 1 - cosine(np.array(sim_1960), np.array(sim_2010))
Editor is loading...