motore-ICC / app.py
azaninello's picture
Update app.py
7de2177
raw
history blame
6.06 kB
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()
sent_tokenized_text = sent_tokenize(text, language='italian')
sent_tokenized_text_lower = [sent.lower() for sent in sent_tokenized_text]
return sent_tokenized_text, sent_tokenized_text_lower
sentences, sentences_lower = get_lists(file)
def search_engine(target):
result = []
for i,sent in enumerate(sentences_lower):
if target.lower() in sent:
result.append(sentences[i])
if len(result) == 0:
return f"Non ho trovato la parola '{target}' nei testi.\n"
stringed_results = ''
for r in result:
stringed_results += str(r) + '\n'
else:
return f"""Ho trovato {len(result)} {"frasi" if len(result) > 1 else "frase"} in cui è presente la parola {target}.\n {stringed_results}"""
demo = gr.Interface(fn=search_engine, inputs='text', outputs='text')
demo.launch()
'''
def numerical(number):
return int(number)
def show_results(result, number):
display = []
try:
for num,sent in enumerate(result[1][:int(number)]):
display.append(f"{num+1}: {sent}\n")
return display
except:
for num,sent in enumerate(result):
display.append(f"{num+1}: {sent}")
return display
demo = gr.Blocks()
with demo:
target = gr.Textbox("Inserisci la parola da cercare")
b1 = gr.Button()
b2 = gr.Button()
result = b2.click(bot, inputs=result, outputs='text')
b1.click(search_engine, inputs=target, outputs='text')
number = numerical(gr.Textbox())
b2 = gr.Button("Quanti esempi vuoi vedere?")
b2.click(upper, inputs=[result, number], outputs=f"""{s for s in display}""")
demo.launch()
'''
'''
demo = gr.Interface([fn=sentence, inputs='text', outputs='text'], [n=upper, inputs=cerca_una_parola, outputs='text'])
demo.launch()
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],
],
)
'''
'''
def update(name):
return f"Welcome to Gradio, {name}!"
def search_engine_bot(target):
result = []
for i,sent in enumerate(sentences_lower):
if target.lower() in sent:
result.append(sentences[i])
if len(result) == 0:
return (f"Non ho trovato la parola '{target}' nei testi.\n", result)
else:
return (f"""Ho trovato {len(result)} {"frasi" if len(result) > 1 else "frase"} in cui è presente la parola {target}.\n""", result)
with gr.Blocks() as demo:
gr.Markdown("Inerisci la parola da cercare.")
with gr.Row():
target = gr.Textbox(placeholder="What is your name?")
out = gr.Textbox()
btn = gr.Button("Run")
btn.click(fn=search_engine_bot, inputs=target, outputs=out)
demo.launch()
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 num_sentences(show=''):
try:
number = int(show)
return number
except:
number = ''
return number
def show_results(result, number):
display = []
try:
for num,sent in enumerate(result[1][:int(number)]):
display.append(f"{num+1}: {sent}\n")
return display
except:
for num,sent in enumerate(result):
display.append(f"{num+1}: {sent}")
return display
def search_engine(parola_da_cercare):
sentences_lower = sentences_lower
sentences = sentences
target= parola_da_cercare #input("Inserisci una o più parole da cercare.\n")
#target= input("Inserisci un'altra parola da cercare.") #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:
return(f"Non ho trovato la parola '{target}' nei testi.\n")
#search_engine(round=True)
else:
return(f"""Ho trovato {len(result)} {"frasi" if len(result) > 1 else "frase"} in cui è presente la parola {target}.\n""")
show = gr.Number(label="Quante frasi vuoi vedere? Scrivi un numero oppure 'tutte', se vuoi vederle tutte :-) \n", show_label=True)
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(search_engine, inputs="text", outputs="text")
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()
'''