File size: 2,387 Bytes
6616039
c73d70f
 
 
 
 
 
 
6616039
c73d70f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6616039
6721782
9b38de2
6616039
 
 
 
 
5fde0f7
6616039
 
 
 
 
 
f8b41af
 
 
 
6616039
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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()