azaninello commited on
Commit
84028a4
1 Parent(s): b7ad680

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -16
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
- '''
3
  import nltk
4
  import simplemma
5
  from nltk.tokenize import word_tokenize
@@ -7,30 +7,62 @@ from nltk.tokenize import sent_tokenize
7
  from nltk.probability import FreqDist
8
  from simplemma import text_lemmatizer
9
  nltk.download('punkt')
10
- '''
11
 
12
- def sentence(cerca_una_parola):
13
- return f"""La parola da cercare è {cerca_una_parola}"""
14
 
 
 
 
15
 
16
- def upper(parola):
17
- return parola.upper()
18
 
19
- demo = gr.Blocks()
20
 
21
- with demo:
22
- cerca_parola = gr.Textbox()
23
- text = gr.Textbox()
24
- up = gr.Textbox()
25
- #number = gr.Number()
26
 
27
- b1 = gr.Button("Inserisci parole")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
 
30
- b1.click(sentence, inputs=cerca_parola, outputs=text)
 
 
 
 
 
 
31
 
32
- b2 = gr.Button("Falla maiuscola")
33
- b2.click(upper, inputs=text, outputs=up)
 
34
 
35
  demo.launch()
36
 
 
1
  import gradio as gr
2
+
3
  import nltk
4
  import simplemma
5
  from nltk.tokenize import word_tokenize
 
7
  from nltk.probability import FreqDist
8
  from simplemma import text_lemmatizer
9
  nltk.download('punkt')
 
10
 
11
+ file = "text.txt"
 
12
 
13
+ def get_lists(file):
14
+ with open(file, 'r', encoding='utf-8') as f:
15
+ text = f.read()
16
 
17
+ sent_tokenized_text = sent_tokenize(text, language='italian')
18
+ sent_tokenized_text_lower = [sent.lower() for sent in sent_tokenized_text]
19
 
20
+ return sent_tokenized_text, sent_tokenized_text_lower
21
 
22
+ sentences, sentences_lower = get_lists(file)
23
+
24
+ def search_engine(target):
 
 
25
 
26
+ result = []
27
+ for i,sent in enumerate(sentences_lower):
28
+ if target.lower() in sent:
29
+ result.append(sentences[i])
30
+ return result
31
+
32
+ def bot(result):
33
+ if len(result) == 0:
34
+ return f"Non ho trovato la parola '{target}' nei testi.\n"
35
+
36
+ else:
37
+ return (f"""Ho trovato {len(result)} {"frasi" if len(result) > 1 else "frase"} in cui è presente la parola {target}.\n""", result)
38
+
39
+
40
+ def numerical(number):
41
+ return int(number)
42
+
43
+ def show_results(result, number):
44
+ display = []
45
+ try:
46
+ for num,sent in enumerate(result[1][:int(number)]):
47
+ display.append(f"{num+1}: {sent}\n")
48
+ return display
49
+ except:
50
+ for num,sent in enumerate(result):
51
+ display.append(f"{num+1}: {sent}")
52
+ return display
53
 
54
 
55
+ demo = gr.Blocks()
56
+
57
+ with demo:
58
+ target = gr.Textbox()
59
+ b1 = gr.Button("Inserisci la parola da cercare")
60
+ result = search_engine(target)
61
+ b1.click(bot, inputs=result, outputs='text')
62
 
63
+ number = numerical(gr.Textbox())
64
+ b2 = gr.Button("Quanti esempi vuoi vedere?")
65
+ b2.click(upper, inputs=[result, number], outputs=f"""{str(n+1) + ' :' + s + '\n' for n,s in enumerate(display)}""")
66
 
67
  demo.launch()
68