Spaces:
Runtime error
Runtime error
import gradio as gr | |
import nltk | |
import simplemma | |
from nltk.tokenize import word_tokenize | |
from nltk.tokenize import sent_tokenize | |
from nltk.probability import FreqDist | |
from simplemma import text_lemmatizer | |
nltk.download('punkt') | |
file = "text.txt" | |
def get_lists(file): | |
with open(file, 'r', encoding='utf-8') as f: | |
text = f.read() | |
word_tokenized_text = word_tokenize(text, language='italian') | |
word_tokenized_text_lower = [word.lower() for word in word_tokenized_text] | |
sent_tokenized_text = sent_tokenize(text, language='italian') | |
sent_tokenized_text_lower = [sent.lower() for sent in sent_tokenized_text] | |
return word_tokenized_text, word_tokenized_text_lower, sent_tokenized_text, sent_tokenized_text_lower | |
words, words_lower, sentences, sentences_lower = get_lists(file) | |
def search_engine(sentences_lower, sentences, round=False): | |
if round == False: | |
target=input("Inserisci una o più parole da cercare.\n") | |
else: | |
target=input("Inserisci un'altra parola da cercare.\n") | |
result = [] | |
for i,sent in enumerate(sentences_lower): | |
if target.lower() in sent: | |
result.append(sentences[i]) | |
if len(result) == 0: | |
print(f"Non ho trovato la parola '{target}' nei testi.\n") | |
search_engine(round = True) | |
else: | |
print(f"""Ho trovato {len(result)} {"frasi" if len(result) > 1 else "frase"} in cui è presente la parola {target}.\n""") | |
show = input("Quante frasi vuoi vedere? Scrivi un numero oppure 'tutte', se vuoi vederle tutte :-) \n") | |
try: | |
for num,sent in enumerate(result[:int(show)]): | |
print(f"{num+1}: {sent}") | |
except: | |
for num,sent in enumerate(result): | |
print(f"{num+1}: {sent}") | |
def sentence_builder(cerca_una_parola, place, activity_list, morning): | |
return f"""The {cerca_una_parola}s went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}""" | |
demo = gr.Interface( | |
sentence_builder, | |
[ | |
gr.Textbox(), | |
gr.Radio(["park", "zoo", "road"]), | |
gr.CheckboxGroup(["ran", "swam", "ate", "slept"]), | |
gr.Checkbox(label="Is it the morning?"), | |
], | |
"text", | |
examples=[ | |
["cats", "park", ["ran", "swam"], True], | |
["dog", "zoo", ["ate", "swam"], False], | |
["bird", "road", ["ran"], False], | |
["cat", "zoo", ["ate"], True], | |
], | |
) | |
if __name__ == "__main__": | |
demo.launch() |