-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword_model.py
88 lines (60 loc) · 2.32 KB
/
word_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Objective: learn a Word2Vec model
from pprint import pprint
from gensim.models import Word2Vec
from utils import get_word_model_file_name, load_tokens
def train_word_model_on_steam_tokens(model=None, steam_tokens=None, num_epochs=10):
# Warning: training will happen several times, which might be detrimental to your model!
if steam_tokens is None:
steam_tokens = load_tokens()
documents = list(steam_tokens.values())
if model is None:
model = Word2Vec(
documents,
) # training already happens here, due to the 'documents' argument!
model.train(documents, total_examples=len(documents), epochs=num_epochs)
model.save(get_word_model_file_name())
return model
def test_word(model, query_word='anime'):
similar_words = model.wv.most_similar(positive=query_word)
print(f'\nThe most similar words to the word "{query_word}" are:')
pprint(similar_words)
return similar_words
def get_word_model_vocabulary(model):
index2word_set = set(model.wv.index2word)
return index2word_set
def compute_similarity_using_word2vec_model(
query_word,
steam_tokens=None,
model=None,
enforce_training=False,
):
if steam_tokens is None:
steam_tokens = load_tokens()
if model is None:
try:
print('Loading Word2Vec model.')
model = Word2Vec.load(get_word_model_file_name())
if enforce_training:
model = train_word_model_on_steam_tokens(
model=model,
steam_tokens=steam_tokens,
)
except FileNotFoundError:
print('Training Word2Vec model from scratch.')
model = train_word_model_on_steam_tokens(
model=None,
steam_tokens=steam_tokens,
)
if query_word in get_word_model_vocabulary(model):
similar_words = test_word(model, query_word)
else:
print(
f'The word {query_word} is not part of the word model vocabulary.',
)
similar_words = None
return similar_words
if __name__ == '__main__':
steam_tokens = load_tokens()
model = Word2Vec.load(get_word_model_file_name())
for query_word in ['anime', 'fun', 'violent']:
compute_similarity_using_word2vec_model(query_word, steam_tokens, model)