Mark7549 commited on
Commit
6e2fc6f
1 Parent(s): cc280c0

Added functions for cosine sim of 1 word in 2 time slices

Browse files
Files changed (2) hide show
  1. app.py +14 -18
  2. word2vec.py +19 -0
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- from word2vec import get_cosine_similarity
3
 
4
 
5
  def greet(name, name2, name3):
@@ -16,7 +16,7 @@ with gr.Blocks() as demo:
16
  # Tab 2
17
  with gr.Tab("Cosine similarity"):
18
  gr.Markdown("## Cosine similarity")
19
-
20
  iface = gr.Interface(
21
  fn=get_cosine_similarity,
22
  inputs=[
@@ -27,23 +27,19 @@ with gr.Blocks() as demo:
27
  outputs=gr.Textbox(label="Result"),
28
  submit_btn='Calculate'
29
  )
30
-
31
- # with gr.Row():
32
- # word1 = gr.Textbox(label='Word 1', placeholder='χρηστήριον').value
33
- # word2 = gr.Textbox(label='Word 2', placeholder='λίκνον').value
34
-
35
- # with gr.Row():
36
- # time_slice = gr.Radio(label='Time slice', choices=["archaic_cbow", "classical_cbow", "early_roman_cbow", "hellen_cbow", "late_roman_cbow"]).value
37
 
38
- # with gr.Row():
39
- # calc_button = gr.Button("Calculate")
40
-
41
-
42
- # button = gr.Interface(fn=greet, inputs="textbox", outputs="textbox")
43
- # # calc_button.click(get_cosine_similarity(word1, word2, time_slice))
44
-
45
- # # calc_button = gr.Button("Calculate")
46
- # # calc_button.click(inputs=[word1, word2, time_slice], outputs=get_cosine_similarity)
 
 
 
47
 
48
 
49
  # Tab 3
 
1
  import gradio as gr
2
+ from word2vec import get_cosine_similarity, get_cosine_similarity_one_word
3
 
4
 
5
  def greet(name, name2, name3):
 
16
  # Tab 2
17
  with gr.Tab("Cosine similarity"):
18
  gr.Markdown("## Cosine similarity")
19
+ gr.Markdown('### Two different words')
20
  iface = gr.Interface(
21
  fn=get_cosine_similarity,
22
  inputs=[
 
27
  outputs=gr.Textbox(label="Result"),
28
  submit_btn='Calculate'
29
  )
 
 
 
 
 
 
 
30
 
31
+ gr.Markdown('### One word in different time slices')
32
+ iface = gr.Interface(
33
+ fn=get_cosine_similarity_one_word,
34
+ inputs=[
35
+ gr.Textbox(label='Word', placeholder='χρηστήριον'),
36
+ gr.Radio(label='Time slice', choices=["archaic_cbow", "classical_cbow", "early_roman_cbow", "hellen_cbow", "late_roman_cbow"]),
37
+ gr.Radio(label='Time slice', choices=["archaic_cbow", "classical_cbow", "early_roman_cbow", "hellen_cbow", "late_roman_cbow"])
38
+
39
+ ],
40
+ outputs=gr.Textbox(label="Result"),
41
+ submit_btn='Calculate'
42
+ )
43
 
44
 
45
  # Tab 3
word2vec.py CHANGED
@@ -2,6 +2,7 @@ from gensim.models import Word2Vec
2
  from collections import defaultdict
3
  import os
4
 
 
5
  def load_word2vec_model(model_path):
6
  '''
7
  Load a word2vec model from a file
@@ -83,6 +84,24 @@ def get_cosine_similarity(word1, word2, time_slice):
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():
 
2
  from collections import defaultdict
3
  import os
4
 
5
+
6
  def load_word2vec_model(model_path):
7
  '''
8
  Load a word2vec model from a file
 
84
  model = load_word2vec_model(f'models/{time_slice}.model')
85
  dict = model_dictionary(model)
86
  return cosine_similarity(dict[word1], dict[word2])
87
+
88
+
89
+ def get_cosine_similarity_one_word(word, time_slice1, time_slice2):
90
+ '''
91
+ Return the cosine similarity of one word in two different time slices
92
+ '''
93
+
94
+ # Return if path does not exist
95
+ if not os.path.exists(f'models/{time_slice1}.model') or not os.path.exists(f'models/{time_slice2}.model'):
96
+ return
97
+
98
+ model1 = load_word2vec_model(f'models/{time_slice1}.model')
99
+ model2 = load_word2vec_model(f'models/{time_slice2}.model')
100
+
101
+ dict1 = model_dictionary(model1)
102
+ dict2 = model_dictionary(model2)
103
+
104
+ return cosine_similarity(dict1[word], dict2[word])
105
 
106
 
107
  def main():