blazingbunny commited on
Commit
a0b6258
1 Parent(s): 9f70cb5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -22,18 +22,14 @@ def calculate_similarity(word1, word2):
22
  cos_sim = torch.nn.functional.cosine_similarity(embeddings1, embeddings2, dim=0)
23
  return cos_sim.item()
24
 
25
- def display_top_5(reference_word, word_list):
26
- similarities = []
27
- for word in word_list.splitlines():
28
- similarity = calculate_similarity(reference_word, word)
29
- similarities.append((word, similarity))
30
-
31
  # Sort by similarity (descending)
32
  top_5_similarities = sorted(similarities, key=lambda item: item[1], reverse=True)[:5]
33
 
34
  st.subheader("Top 5 Most Similar Words:")
35
  for word, similarity in top_5_similarities:
36
  st.write(f"- '{word}': {similarity:.4f}")
 
37
 
38
  # Streamlit interface
39
  st.title("Word Similarity Checker")
@@ -43,11 +39,15 @@ word_list = st.text_area("Enter a list of words (one word per line):")
43
 
44
  if st.button("Analyze"):
45
  if reference_word and word_list:
46
- words = word_list.splitlines()
47
- for word in words:
 
48
  similarity = calculate_similarity(reference_word, word)
49
- st.write(f"Similarity between '{reference_word}' and '{word}': {similarity:.4f}")
50
- display_top_5(reference_word, word_list)
 
 
51
  else:
52
  st.warning("Please enter a reference word and a list of words.")
53
 
 
 
22
  cos_sim = torch.nn.functional.cosine_similarity(embeddings1, embeddings2, dim=0)
23
  return cos_sim.item()
24
 
25
+ def display_top_5(similarities):
 
 
 
 
 
26
  # Sort by similarity (descending)
27
  top_5_similarities = sorted(similarities, key=lambda item: item[1], reverse=True)[:5]
28
 
29
  st.subheader("Top 5 Most Similar Words:")
30
  for word, similarity in top_5_similarities:
31
  st.write(f"- '{word}': {similarity:.4f}")
32
+
33
 
34
  # Streamlit interface
35
  st.title("Word Similarity Checker")
 
39
 
40
  if st.button("Analyze"):
41
  if reference_word and word_list:
42
+ # Calculate similarities for the reference phrase against the word list
43
+ similarities = []
44
+ for word in word_list.splitlines():
45
  similarity = calculate_similarity(reference_word, word)
46
+ similarities.append((word, similarity))
47
+
48
+ # Find top 5 (We should only do this once outside the loop)
49
+ display_top_5(similarities)
50
  else:
51
  st.warning("Please enter a reference word and a list of words.")
52
 
53
+