File size: 1,530 Bytes
add0074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr


def search_pattern(fulltext, pattern, method):
    fulltext = fulltext.replace('\n','')
    pattern = pattern.replace('\n','')
    if method == 'Naive-Exact-Match':
        import time
        start = time.time()
        occurrence = 0
        for i in range(0,len(fulltext)-len(pattern)+1):
          found = 1
          for l in range(len(pattern)):
             if fulltext[i+l] != pattern[l]:
                 found = 0
          if found:   
             occurrence += 1
             print("Found ", fulltext[i:i+len(pattern)], ' locating at position ', i+1)
        
        end = time.time()
        time_elapse = end - start

    return occurrence, time_elapse
    
    
### configure inputs/outputs
set_fulltext = gr.inputs.Textbox(label="Full Text")
set_pattern = gr.inputs.Textbox(label="Pattern")
set_method = gr.Radio(["Naive-Exact-Match"])
set_results = gr.outputs.Textbox(label="Occurrence")
set_times = gr.outputs.Textbox(label="Time elapse")


### configure gradio, detailed can be found at https://www.gradio.app/docs/#i_slider
interface = gr.Interface(fn=search_pattern, 
                         inputs=[set_fulltext, set_pattern,set_method], 
                         outputs=[set_results,set_times],
                         examples_per_page = 2,
                         title="CSCI1020 Demo 1: Sequence Match", 
                         theme = 'huggingface',
                         layout = 'vertical'
                         )
                      
interface.launch(debug=True)