Spaces:
Runtime error
Runtime error
azaninello
commited on
Commit
•
c73d70f
1
Parent(s):
51f75df
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,54 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def sentence_builder(cerca_una_parola, place, activity_list, morning):
|
5 |
return f"""The {cerca_una_parola}s went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}"""
|
|
|
1 |
import gradio as gr
|
2 |
+
import nltk
|
3 |
+
import simplemma
|
4 |
+
from nltk.tokenize import word_tokenize
|
5 |
+
from nltk.tokenize import sent_tokenize
|
6 |
+
from nltk.probability import FreqDist
|
7 |
+
from simplemma import text_lemmatizer
|
8 |
+
nltk.download('punkt')
|
9 |
|
10 |
+
file = "text.txt"
|
11 |
+
|
12 |
+
def get_lists(file):
|
13 |
+
with open(file, 'r', encoding='utf-8') as f:
|
14 |
+
text = f.read()
|
15 |
+
|
16 |
+
word_tokenized_text = word_tokenize(text, language='italian')
|
17 |
+
word_tokenized_text_lower = [word.lower() for word in word_tokenized_text]
|
18 |
+
|
19 |
+
sent_tokenized_text = sent_tokenize(text, language='italian')
|
20 |
+
sent_tokenized_text_lower = [sent.lower() for sent in sent_tokenized_text]
|
21 |
+
|
22 |
+
return word_tokenized_text, word_tokenized_text_lower, sent_tokenized_text, sent_tokenized_text_lower
|
23 |
+
|
24 |
+
words, words_lower, sentences, sentences_lower = get_lists(file)
|
25 |
+
|
26 |
+
def search_engine(sentences_lower, sentences, round=False):
|
27 |
+
if round == False:
|
28 |
+
target=input("Inserisci una o più parole da cercare.\n")
|
29 |
+
else:
|
30 |
+
target=input("Inserisci un'altra parola da cercare.\n")
|
31 |
+
|
32 |
+
|
33 |
+
result = []
|
34 |
+
for i,sent in enumerate(sentences_lower):
|
35 |
+
if target.lower() in sent:
|
36 |
+
result.append(sentences[i])
|
37 |
+
|
38 |
+
if len(result) == 0:
|
39 |
+
print(f"Non ho trovato la parola '{target}' nei testi.\n")
|
40 |
+
search_engine(round = True)
|
41 |
+
|
42 |
+
else:
|
43 |
+
print(f"""Ho trovato {len(result)} {"frasi" if len(result) > 1 else "frase"} in cui è presente la parola {target}.\n""")
|
44 |
+
show = input("Quante frasi vuoi vedere? Scrivi un numero oppure 'tutte', se vuoi vederle tutte :-) \n")
|
45 |
+
|
46 |
+
try:
|
47 |
+
for num,sent in enumerate(result[:int(show)]):
|
48 |
+
print(f"{num+1}: {sent}")
|
49 |
+
except:
|
50 |
+
for num,sent in enumerate(result):
|
51 |
+
print(f"{num+1}: {sent}")
|
52 |
|
53 |
def sentence_builder(cerca_una_parola, place, activity_list, morning):
|
54 |
return f"""The {cerca_una_parola}s went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}"""
|