File size: 4,526 Bytes
6616039
b096b37
c73d70f
 
 
 
 
 
 
b096b37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6616039
b096b37
6460fc9
 
 
0a5ef4b
 
 
 
 
 
 
 
 
 
 
 
 
 
6460fc9
0a5ef4b
6460fc9
0a5ef4b
6460fc9
 
0ab6f3c
6460fc9
 
 
0a5ef4b
c73d70f
 
 
 
 
 
 
 
 
 
 
 
 
 
eaf9808
ab6fc81
 
b096b37
ab6fc81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c73d70f
887a064
479c7c0
 
887a064
 
c73d70f
 
 
 
 
 
 
 
887a064
 
c73d70f
 
887a064
 
c73d70f
 
 
 
 
 
 
6616039
6721782
9b38de2
6616039
eaf9808
6460fc9
6616039
 
 
5fde0f7
6616039
 
 
 
 
 
f8b41af
 
 
 
6616039
 
6460fc9
6616039
 
6460fc9
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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')
'''

def sentence(cerca_una_parola):
    return f"""La parola da cercare è {cerca_una_parola}}"""


def upper(cerca_una_parola):
    return cerca_una_parola.upper()
    
demo = fr.Textbox(fn=sentence, inputs='text', outputs='text')
demo.launch()

demo2 = fr.Textbox(fn=upper, inputs='text', outputs='text')
demo2.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()
'''