from textblob import TextBlob import gradio as gr import os os.system("python -m textblob.download_corpora") string_json={ 'control':'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN', } cont_list=list(string_json['control']) def get_nouns(text): json_object={} sen_list=[] noun_list={} noun_box=[] blob = TextBlob(text) for sentence in blob.sentences: #print(sentence) sen_list.append(str(sentence)) key_cnt=len(sen_list) cnt=0 go=True a="Z" g="W" if go: for ea in range(10): if go: for b in range(50): if go: for c in range(50): if go: for d in range(50): if go: #for i,ea in enumerate(key_list): #json_object[sen_list[cnt]]=f'{a}{cont_list[b]}{cont_list[c]}{cont_list[d]}' blob_n = TextBlob(sen_list[cnt]) noun_p=blob_n.noun_phrases noun_box=[] for ea in blob_n.parse().split(" "): n=ea.split("/") if n[1] == "NN": noun_box.append(n[0]) json_object[f'{a}{cont_list[b]}{cont_list[c]}{cont_list[d]}']={'sentence':sen_list[cnt],'noun_phrase':noun_p,'nouns':noun_box} for noun in noun_p: if noun in list(noun_list.keys()): noun_list[str(noun)].append(f'{a}{cont_list[b]}{cont_list[c]}{cont_list[d]}') else: noun_list[str(noun)]=[f'{a}{cont_list[b]}{cont_list[c]}{cont_list[d]}'] for nn in noun_box: if nn in list(noun_list.keys()): noun_list[str(nn)].append(f'{g}{cont_list[b]}{cont_list[c]}{cont_list[d]}') else: noun_list[str(nn)]=[f'{g}{cont_list[b]}{cont_list[c]}{cont_list[d]}'] if json_object[f'{a}{cont_list[b]}{cont_list[c]}{cont_list[d]}']=='ZNNN': #if json_object[sen_list[cnt]]=='ZNNN': #print ("Y") a="Y" g="V" b=0 c=0 d=0 if json_object[f'{a}{cont_list[b]}{cont_list[c]}{cont_list[d]}']=='YNNN': #print("X") a="X" g="U" b=0 c=0 d=0 if cnt == key_cnt-1: print('done') go=False print(list(json_object.keys())[-1]) else: cnt+=1 #print(blob.tags) # [('The', 'DT'), ('titular', 'JJ'), # ('threat', 'NN'), ('of', 'IN'), ...] #print(blob.parse()) #print(blob.noun_phrases) # WordList(['titular threat', 'blob', # 'ultimate movie monster', # 'amoeba-like mass', ...]) return json_object,noun_list def find_query(query,sen,nouns): blob_f = TextBlob(query) noun_box={} noun_list=[] for ea in blob_f.parse().split(" "): n=ea.split("/") if n[1] == "NN": noun_list.append(n[0]) # for nn in list(nouns.keys()): # if nn in noun_list: # noun_box[n[0]] # print(ea) # print(ea.split("/")) return noun_list with gr.Blocks() as app: inp = gr.Textbox(label="Paste Text",lines=10) btn = gr.Button("Load Document") with gr.Row(): query=gr.Textbox(label="Search query") search_btn=gr.Button("Search") out_box=gr.Textbox(label="Results") with gr.Row(): with gr.Column(scale=2): sen=gr.JSON(label="Sentences") with gr.Column(scale=1): nouns=gr.JSON(label="Nouns") search_btn.click(find_query,[query,sen,nouns],out_box) btn.click(get_nouns,inp,[sen,nouns]) app.launch()