Skip to content

Instantly share code, notes, and snippets.

@PandaWhoCodes
Created January 27, 2020 18:25
Show Gist options
  • Save PandaWhoCodes/6f21790dd357de08ab233e335b60e13b to your computer and use it in GitHub Desktop.
Save PandaWhoCodes/6f21790dd357de08ab233e335b60e13b to your computer and use it in GitHub Desktop.
Word to vec using genism
from gensim.models import Word2Vec
# from tests import get_all_text
def save_model():
# define training data
sentences = get_all_text()
# print(sentences)
# train model
model = Word2Vec(sentences, min_count=1)
# summarize the loaded model
print(model)
# summarize vocabulary
words = list(model.wv.vocab)
print(words)
# access vector for one word
print(model['update'])
# save model
model.save('model.bin')
def load_model():
# load model
new_model = Word2Vec.load('model.bin')
# print(new_model)
return new_model
def get_similar(model, word1):
# woman + king - man
# return model.most_similar(positive=['woman', 'king'], negative=['man'])
return model.most_similar(word1)
model = load_model()
print(get_similar(model, "wcc2"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment