blazingbunny commited on
Commit
addbb21
1 Parent(s): 790f6dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -9
app.py CHANGED
@@ -5,10 +5,10 @@ import spacy
5
  import subprocess
6
 
7
  # Download the spaCy model if it is not already downloaded
8
- subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
9
 
10
- # Load the spaCy model for POS tagging
11
- nlp = spacy.load("en_core_web_sm")
12
 
13
  def identify_nouns_verbs(text):
14
  # Process the text with spaCy
@@ -20,14 +20,47 @@ def identify_nouns_verbs(text):
20
 
21
  return {"Nouns": nouns, "Verbs": verbs}
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Create the Gradio interface
24
  iface = gr.Interface(
25
- fn=identify_nouns_verbs,
26
- inputs=gr.Textbox(lines=10, placeholder="Enter your text here..."),
27
- outputs=gr.JSON(),
28
- title="Noun and Verb Identifier",
29
- description="Enter a document or text to identify the nouns and verbs."
 
 
 
30
  )
31
 
32
  if __name__ == "__main__":
33
- iface.launch()
 
5
  import subprocess
6
 
7
  # Download the spaCy model if it is not already downloaded
8
+ subprocess.run(["python", "-m", "spacy", "download", "en_core_web_md"])
9
 
10
+ # Load the spaCy model for POS tagging and similarity
11
+ nlp = spacy.load("en_core_web_md")
12
 
13
  def identify_nouns_verbs(text):
14
  # Process the text with spaCy
 
20
 
21
  return {"Nouns": nouns, "Verbs": verbs}
22
 
23
+ def calculate_similarity(nouns_verbs, input_list):
24
+ similarities = {"Nouns": {}, "Verbs": {}}
25
+
26
+ for noun in nouns_verbs["Nouns"]:
27
+ noun_token = nlp(noun)
28
+ for word in input_list["Nouns"]:
29
+ word_token = nlp(word)
30
+ similarity = noun_token.similarity(word_token)
31
+ if similarity > 0.7: # Adjust threshold as needed
32
+ if noun not in similarities["Nouns"]:
33
+ similarities["Nouns"][noun] = []
34
+ similarities["Nouns"][noun].append((word, similarity))
35
+
36
+ for verb in nouns_verbs["Verbs"]:
37
+ verb_token = nlp(verb)
38
+ for word in input_list["Verbs"]:
39
+ word_token = nlp(word)
40
+ similarity = verb_token.similarity(word_token)
41
+ if similarity > 0.7: # Adjust threshold as needed
42
+ if verb not in similarities["Verbs"]:
43
+ similarities["Verbs"][verb] = []
44
+ similarities["Verbs"][verb].append((word, similarity))
45
+
46
+ return similarities
47
+
48
+ def process_inputs(text, input_list):
49
+ nouns_verbs = identify_nouns_verbs(text)
50
+ similarities = calculate_similarity(nouns_verbs, input_list)
51
+ return {"Nouns and Verbs": nouns_verbs, "Similarities": similarities}
52
+
53
  # Create the Gradio interface
54
  iface = gr.Interface(
55
+ fn=process_inputs,
56
+ inputs=[
57
+ gr.inputs.Textbox(lines=10, placeholder="Enter your text here..."),
58
+ gr.inputs.JSON(label="Input List", placeholder='{"Nouns": ["word1", "word2"], "Verbs": ["word1", "word2"]}')
59
+ ],
60
+ outputs=gr.outputs.JSON(),
61
+ title="Noun and Verb Identifier with Similarity Check",
62
+ description="Enter a document or text to identify the nouns and verbs, and check for similarities with a given list of words."
63
  )
64
 
65
  if __name__ == "__main__":
66
+ iface.launch()