Created
January 27, 2020 18:25
-
-
Save PandaWhoCodes/6f21790dd357de08ab233e335b60e13b to your computer and use it in GitHub Desktop.
Word to vec using genism
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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