-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
75 lines (59 loc) · 1.75 KB
/
api.py
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
from flask import Flask, render_template, request
app = Flask(__name__)
# Method for KMP algorithm
def KMP(pattern, string):
ls = len(string)
lp = len(pattern)
table = prefix(pattern, lp)
m = 0
n = 0
index = []
while m != ls:
if string[m] == pattern[n]:
m = m+1;
n = n+1;
else:
n = table[n-1]
if n == lp:
index.append(m-n)
n = table[n-1]
elif n == 0:
m = m+1;
return index
# Method for the prefix table that summarizes the indices of the pattern
def prefix(pattern, lp):
table = [0]*lp
n = 0
m = 1
while (m != lp):
if pattern[m] == pattern[n]:
n = n+1;
table[m] = n
m = m+1
elif n != 0:
n = table[n-1]
else:
table[m]=0
m = m+1
return table
@app.route("/", methods=["GET", "POST"])
def home():
if request.method == "POST":
# Dictionary key that retrieves the pattern from the submit box
pattern = request.form["pattern"]
# Convert to uppercase the submitted pattern
pattern = pattern.upper()
# Open and convert the text into string sequence
text_file = open("sequence.txt")
string = text_file.read()
text_file.close()
selected_index = KMP(pattern, string)
# Count the number of indices that are retrieved from KMP method
count = 0
for i in selected_index:
count += 1
return render_template("home.html", count = count)
return render_template("home.html")
# Run the application on the local server
if __name__ == "__main__":
app.run(host= "127.0.0.1", debug=True)