Untitled

 avatar
unknown
python
2 years ago
702 B
4
Indexable
import numpy

# Local Neighbors Measure
def LNM(word: str, topn=10):

  word_1960 = e_1960.wv[word]
  word_2010 = e_2010.wv[word]
  neighbors = []

  # adding neighbors of 1960
  neighbors.extend([w[0] for w in e_1960.wv.most_similar(positive=[word_1960], topn=topn)])
  # adding neighbors of 2010
  neighbors.extend([w[0] for w in e_2010.wv.most_similar(positive=[word_2010], topn=topn)])

  # 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...