Skip to content

Instantly share code, notes, and snippets.

@angadn
Last active November 10, 2017 15:15
Show Gist options
  • Save angadn/0f45362a646b25a254abf63b70bdf611 to your computer and use it in GitHub Desktop.
Save angadn/0f45362a646b25a254abf63b70bdf611 to your computer and use it in GitHub Desktop.
Gensim Test
"""
Tests gensim with the Google News Word2Vec model
"""
import gensim
import numpy
from scipy import spatial
M = gensim.models.KeyedVectors.load_word2vec_format(
"./GoogleNews-vectors-negative300.bin", binary=True
)
while True:
try:
vec1 = numpy.zeros(300)
for w in raw_input("Word 1: ").split(" "):
vec1 = vec1 + M.wv[w]
vec2 = numpy.zeros(300)
for w in raw_input("Word 2: ").split(" "):
vec2 = vec2 + M.wv[w]
print(1 - spatial.distance.cosine(vec1, vec2))
except Exception as e:
print(e.message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment