Mark7549 commited on
Commit
cc280c0
·
1 Parent(s): 23863e2

updated word2vec with get_cos_sim function for frontend

Browse files
Files changed (1) hide show
  1. word2vec.py +18 -2
word2vec.py CHANGED
@@ -1,5 +1,6 @@
1
  from gensim.models import Word2Vec
2
  from collections import defaultdict
 
3
 
4
  def load_word2vec_model(model_path):
5
  '''
@@ -67,10 +68,25 @@ def cosine_similarity(vector_a, vector_b):
67
 
68
  similarity = dot_prod / (mag_a * mag_b)
69
  return similarity
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
 
72
  def main():
73
- model = load_word2vec_model('../models/archaic_cbow.model')
74
  archaic_cbow_dict = model_dictionary(model)
75
 
76
  score = cosine_similarity(archaic_cbow_dict['Πελοπόννησος'], archaic_cbow_dict['σπάργανον'])
@@ -80,7 +96,7 @@ def main():
80
  # print(vector)
81
 
82
  # Iterate over all words and print their vectors
83
- # iterate_over_words(model)
84
 
85
 
86
  if __name__ == "__main__":
 
1
  from gensim.models import Word2Vec
2
  from collections import defaultdict
3
+ import os
4
 
5
  def load_word2vec_model(model_path):
6
  '''
 
68
 
69
  similarity = dot_prod / (mag_a * mag_b)
70
  return similarity
71
+
72
+
73
+ def get_cosine_similarity(word1, word2, time_slice):
74
+ '''
75
+ Return the cosine similarity of two words
76
+ '''
77
+ # TO DO: MOET NETTER
78
+
79
+ # Return if path does not exist
80
+ if not os.path.exists(f'models/{time_slice}.model'):
81
+ return
82
+
83
+ model = load_word2vec_model(f'models/{time_slice}.model')
84
+ dict = model_dictionary(model)
85
+ return cosine_similarity(dict[word1], dict[word2])
86
 
87
 
88
  def main():
89
+ model = load_word2vec_model('models/archaic_cbow.model')
90
  archaic_cbow_dict = model_dictionary(model)
91
 
92
  score = cosine_similarity(archaic_cbow_dict['Πελοπόννησος'], archaic_cbow_dict['σπάργανον'])
 
96
  # print(vector)
97
 
98
  # Iterate over all words and print their vectors
99
+ iterate_over_words(model)
100
 
101
 
102
  if __name__ == "__main__":