Spaces:
Build error
Build error
| from textblob import TextBlob | |
| import gradio as gr | |
| import math | |
| import os | |
| os.system("python -m textblob.download_corpora") | |
| control_json={'control':'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','char':'','leng':62} | |
| string_json={'control':'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN','char':'OPQRSTUVWXYZ','leng':50} | |
| cont_list=list(string_json['control']) | |
| def get_sen_list(text): | |
| sen_list=[] | |
| blob = TextBlob(text) | |
| for sentence in blob.sentences: | |
| sen_list.append(str(sentence)) | |
| return sen_list | |
| def proc_sen(sen_list,cnt): | |
| blob_n = TextBlob(sen_list[cnt]) | |
| noun_p=blob_n.noun_phrases | |
| noun_box1=[] | |
| for ea in blob_n.parse().split(" "): | |
| n=ea.split("/") | |
| if n[1] == "NN": | |
| noun_box1.append(n[0]) | |
| json_object={'sen_num':cnt,'sentence':sen_list[cnt],'noun_phrase':noun_p,'nouns':noun_box1} | |
| return json_object | |
| def proc_nouns(sen_list): | |
| noun_list={} | |
| for nn in list(sen_list.keys()): | |
| for nnn in sen_list[nn]['nouns']: | |
| if nnn in list(noun_list.keys()): | |
| noun_list[str(nnn)].append(nn) | |
| else: | |
| noun_list[str(nnn)]=[nn] | |
| return noun_list | |
| def get_nouns(text,steps=1): | |
| control_len=control_json['leng']-steps | |
| control_char=list(control_json['control']) | |
| control_char_val=list(control_json['control'][:control_len]) | |
| control_val=list(control_json['control'][control_len:]) | |
| char_len=len(control_char) | |
| val_len=len(control_val) | |
| print(control_char) | |
| print(control_val) | |
| json_out={} | |
| noun_list={} | |
| step_list=[] | |
| step_cont_box=[] | |
| sen_list=get_sen_list(text) | |
| key_cnt=len(sen_list) | |
| print(key_cnt) | |
| #noun_cnt=len(noun_box) | |
| #print(noun_cnt) | |
| big_cnt=0 | |
| cnt=0 | |
| go=True | |
| n_cnt=0 | |
| nx=key_cnt | |
| while True: | |
| if nx > 1: | |
| n_cnt+=1 | |
| nx = nx/char_len | |
| else: | |
| print("#######") | |
| print(n_cnt) | |
| print(nx) | |
| print("#######") | |
| steps=n_cnt | |
| break | |
| for ii in range(steps): | |
| print(ii) | |
| step_cont_box.append(0) | |
| #print (step_cont_box) | |
| mod=0 | |
| pos=len(step_cont_box)-1 | |
| if go: | |
| for i, ea in enumerate(sen_list): | |
| if go: | |
| if cnt > char_len-1: | |
| #print(step_cont_box) | |
| go1=True | |
| for ii,ev in enumerate(step_cont_box): | |
| if go: | |
| if ev >= char_len-1: | |
| step_cont_box[ii]=0 | |
| if go1==True: | |
| step_cont_box[ii-1]=step_cont_box[ii-1]+1 | |
| go1=False | |
| cnt=1 | |
| else: | |
| step_cont_box[pos]=cnt | |
| cnt+=1 | |
| print(step_cont_box) | |
| out_js="" | |
| for iii,j in enumerate(step_cont_box): | |
| print(j) | |
| out_js = out_js+control_char[j] | |
| sen_obj=proc_sen(sen_list,i) | |
| #json_out[out_js]={'nouns':ea} | |
| json_out[out_js]=sen_obj | |
| big_cnt+=1 | |
| if big_cnt==key_cnt: | |
| print("DONE") | |
| go=False | |
| noun_list=proc_nouns(json_out) | |
| return json_out, noun_list | |
| def find_query(query,sen,nouns): | |
| blob_f = TextBlob(query) | |
| noun_box={} | |
| noun_list=[] | |
| sen_box=[] | |
| for ea in blob_f.parse().split(" "): | |
| n=ea.split("/") | |
| if n[1] == "NN": | |
| noun_list.append(n[0]) | |
| nouns_l=list(nouns.keys()) | |
| for nn in nouns_l: | |
| for nl in noun_list: | |
| if nl in nn: | |
| if nl in noun_box: | |
| for ea_n in nouns[nn]: | |
| noun_box[str(nl)].append(ea_n) | |
| else: | |
| noun_box[str(nl)]=[] | |
| for ea_n in nouns[nn]: | |
| noun_box[str(nl)].append(ea_n) | |
| for ea in noun_box.values(): | |
| for vals in ea: | |
| sen_box.append({'sen_num':sen[vals]['sen_num'],'sentence':sen[vals]['sentence']}) | |
| return noun_box,sen_box | |
| 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") | |
| sen_box=gr.JSON(label="Sentences") | |
| 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,sen_box]) | |
| btn.click(get_nouns,[inp],[sen,nouns]) | |
| app.launch() | |