| import gradio as gr |
|
|
|
|
| def sort_doc(in_list: list, steps_in: int, control: int, prev_list: str): |
| control_json={'control':'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','char':'','leng':62} |
|
|
| if isinstance(in_list, str): |
| in_list=in_list.split() |
| |
| |
| |
| |
| uni_box=[] |
| for ea in in_list: |
| if not ea in uni_box: |
| uni_box.append(ea) |
| key_cnt=len(uni_box) |
| print(key_cnt) |
|
|
| control_char=list(control_json['control']) |
| char_len=len(control_char) |
| if not steps_in: |
| n_cnt=0 |
| nx=key_cnt |
| while True: |
| if nx >= 1: |
| n_cnt+=1 |
| nx = nx/char_len |
| else: |
| steps=n_cnt |
| break |
| if steps_in: |
| steps=steps_in |
| step_cont_box=[] |
| |
|
|
| |
| |
| json_out={} |
| rev_out={} |
| big_cnt=0 |
| cnt=0 |
| go=True |
|
|
| if prev_list: |
| print("LOD") |
| last_key=list(prev_list.keys())[-1] |
| print(last_key) |
| for ea_dig in last_key: |
| ea_dig=control_json['control'].index(ea_dig) |
| ea_dig=int(ea_dig) |
| print(f'{ea_dig} :: {list(control_json["control"][ea_dig])[0]}') |
| |
| step_cont_box.append(ea_dig) |
| print(step_cont_box) |
| cnt=int(step_cont_box[-1])+1 |
| if not prev_list: |
| print("NOT LOD") |
| for ii in range(steps): |
| print(ii) |
| step_cont_box.append(0) |
|
|
| if control: |
| new_step_cont_box=[] |
| control_val_box=[] |
| |
| cont_box_len=len(step_cont_box) |
| control_len=control_json['leng']-control |
| control_char=list(control_json['control'][:control_len]) |
| control_val=list(control_json['control'][control_len:]) |
| val_len=len(control_val) |
| |
| for ea in control_val: |
| control_val_box.append(ea) |
| print(f'CONTROL_VAL_BOX:: {control_val_box}') |
| new_step_cont_box.append(control_json['control'].index(control_val_box[-1])) |
| for iii in step_cont_box: |
| new_step_cont_box.append(iii) |
| step_cont_box=new_step_cont_box |
| pos=len(step_cont_box)-1 |
| if go: |
| for i, ea in enumerate(in_list): |
| if not ea in list(rev_out.keys()): |
| |
| if go: |
| if cnt > char_len-1: |
| |
| 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 |
| |
| out_js="" |
| for iii,j in enumerate(step_cont_box): |
| |
| out_js = out_js+control_char[j] |
| |
| json_out[out_js]=in_list[i] |
| rev_out[in_list[i]]=out_js |
| big_cnt+=1 |
| if big_cnt==key_cnt: |
| print("DONE") |
| go=False |
| return json_out,rev_out |
|
|
|
|
| def sort_doc_OG(in_list: list, steps_in: int, control: int, prev_list: str): |
| control_json={'control':'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ','char':'','leng':62} |
| key_cnt=len(in_list) |
| print(key_cnt) |
|
|
| |
| |
|
|
| control_char=list(control_json['control']) |
| char_len=len(control_char) |
| if not steps_in: |
| n_cnt=0 |
| nx=key_cnt |
| while True: |
| if nx >= 1: |
| n_cnt+=1 |
| nx = nx/char_len |
| else: |
| steps=n_cnt |
| break |
| if steps_in: |
| steps=steps_in |
| if control: |
| control_len=control_json['leng']-control |
| control_char=list(control_json['control'][:control_len]) |
| control_val=list(control_json['control'][control_len:]) |
| val_len=len(control_val) |
| control_val_box=[] |
| for ea in control_val: |
| control_val_box.append(ea) |
| print(f'CONTROL_VAL_BOX:: {control_val_box}') |
| |
| json_out={} |
| |
| big_cnt=0 |
| cnt=0 |
| go=True |
| step_cont_box=[] |
|
|
| if prev_list: |
| print("LOD") |
| last_key=list(prev_list.keys())[-1] |
| print(last_key) |
| for ea_dig in last_key: |
| ea_dig=control_json['control'].index(ea_dig) |
| ea_dig=int(ea_dig) |
| print(f'{ea_dig} :: {list(control_json["control"][ea_dig])[0]}') |
| |
| step_cont_box.append(ea_dig) |
| print(step_cont_box) |
| cnt=int(step_cont_box[-1])+1 |
| if not prev_list: |
| print("NOT LOD") |
| for ii in range(steps): |
| print(ii) |
| step_cont_box.append(0) |
|
|
| pos=len(step_cont_box)-1 |
| if go: |
| for i, ea in enumerate(in_list): |
| if go: |
| if cnt > char_len-1: |
| |
| 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 |
| |
| out_js="" |
| for iii,j in enumerate(step_cont_box): |
| |
| out_js = out_js+control_char[j] |
| json_out[out_js]=in_list[i] |
| big_cnt+=1 |
| if big_cnt==key_cnt: |
| print("DONE") |
| go=False |
| return json_out |
|
|
|
|
| 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, value="['skdflsdj','sd','sdfsdf','sdfsddsfsd','sdfsdfd','df']") |
| with gr.Row(): |
| steps=gr.Slider(label="Steps",minimum=0,maximum=12,step=1,value=0) |
| control=gr.Slider(label="Control",minimum=0,maximum=32,step=1,value=0) |
| prev_doc=gr.File() |
| 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]) |
| btn.click(sort_doc,[inp,steps,control,prev_doc],[sen,nouns]) |
| app.launch() |
| |
|
|
|
|