blazingbunny commited on
Commit
0b19d7f
1 Parent(s): e5dd147

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -11,19 +11,19 @@ subprocess.run(["python", "-m", "spacy", "download", "en_core_web_md"])
11
  # Load the spaCy model for POS tagging and similarity
12
  nlp = spacy.load("en_core_web_md")
13
 
14
- def identify_nouns_verbs(text):
15
  # Process the text with spaCy
16
  doc = nlp(text)
17
 
18
- # Extract nouns and verbs with their positions
19
- nouns = [{"text": token.text, "begin_offset": token.idx} for token in doc if token.pos_ == "NOUN"]
20
- verbs = [{"text": token.text, "begin_offset": token.idx} for token in doc if token.pos_ == "VERB"]
21
 
22
  return {"Nouns": nouns, "Verbs": verbs}
23
 
24
  def calculate_similarity(nouns_verbs, input_list):
25
  similarities = {"Nouns": {}, "Verbs": {}}
26
-
27
  def add_similarity(word, similar_word, score, pos):
28
  if word not in similarities[pos]:
29
  similarities[pos][word] = []
@@ -55,9 +55,19 @@ def process_inputs(text, json_file):
55
  with open(json_file.name, 'r') as f:
56
  input_list = json.load(f)
57
 
58
- nouns_verbs = identify_nouns_verbs(text)
 
 
 
59
  similarities = calculate_similarity(nouns_verbs, input_list)
60
- return {"Nouns and Verbs": nouns_verbs, "Similarities": similarities}
 
 
 
 
 
 
 
61
 
62
  # Create the Gradio interface
63
  iface = gr.Interface(
 
11
  # Load the spaCy model for POS tagging and similarity
12
  nlp = spacy.load("en_core_web_md")
13
 
14
+ def identify_nouns_verbs(text, existing_nouns, existing_verbs):
15
  # Process the text with spaCy
16
  doc = nlp(text)
17
 
18
+ # Extract nouns and verbs with their positions, omitting those already in the input list
19
+ nouns = [{"text": token.text, "begin_offset": token.idx} for token in doc if token.pos_ == "NOUN" and token.text.lower() not in existing_nouns]
20
+ verbs = [{"text": token.text, "begin_offset": token.idx} for token in doc if token.pos_ == "VERB" and token.text.lower() not in existing_verbs]
21
 
22
  return {"Nouns": nouns, "Verbs": verbs}
23
 
24
  def calculate_similarity(nouns_verbs, input_list):
25
  similarities = {"Nouns": {}, "Verbs": {}}
26
+
27
  def add_similarity(word, similar_word, score, pos):
28
  if word not in similarities[pos]:
29
  similarities[pos][word] = []
 
55
  with open(json_file.name, 'r') as f:
56
  input_list = json.load(f)
57
 
58
+ existing_nouns = [word.lower() for word in input_list["Nouns"]]
59
+ existing_verbs = [word.lower() for word in input_list["Verbs"]]
60
+
61
+ nouns_verbs = identify_nouns_verbs(text, existing_nouns, existing_verbs)
62
  similarities = calculate_similarity(nouns_verbs, input_list)
63
+
64
+ # Format similarities to match the required output structure
65
+ formatted_similarities = {"Nouns": {}, "Verbs": {}}
66
+ for pos in ["Nouns", "Verbs"]:
67
+ for word, sims in similarities[pos].items():
68
+ formatted_similarities[pos][word] = [[sim[0], sim[1]] for sim in sims]
69
+
70
+ return {"Nouns and Verbs": nouns_verbs, "Similarities": formatted_similarities}
71
 
72
  # Create the Gradio interface
73
  iface = gr.Interface(