-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
121 lines (91 loc) · 2.76 KB
/
search.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import sqlite3
import time
import sys
conn = sqlite3.connect('./data/simplewiki.db')
cursor = conn.cursor()
algorithm_sql = ""
results = None
result_offset = 0
result_size = 10
def SelectAlgorithm(algo, display=False):
global algorithm_sql
with open("./algorithm/"+algo+'.sql', 'r') as f:
algorithm_sql = f.read()
if display:
print(f' Loaded algorithm {algo}')
SelectAlgorithm("jaccard")
def ShowSimilar(target):
cursor.execute("Select a.title from articles a where a.title like ? order by length(a.title) limit 10;", ( "%"+target+"%" ,))
results = cursor.fetchall()
print(" Try: " + ", ".join(str(val[0]) for val in results))
pass
def Search(target):
global results
global result_offset
target = target[0].upper() + target[1:]
cursor.execute("SELECT id FROM articles WHERE title = ?", (target,))
res = cursor.fetchone()
if res is None:
ShowSimilar(target)
return
target_id = res[0]
cursor.execute("select * from redirects where from_article_id= ?", (target_id,))
res = cursor.fetchone()
if res is not None:
target_id = res[1]
cursor.execute("SELECT title FROM articles WHERE id = ?", (target_id,))
res = cursor.fetchone()
print(f' redirect {res[0]}')
start_time = time.time()
placeholders = algorithm_sql.count("?")
cursor.execute(algorithm_sql, (target_id, )*placeholders)
results = cursor.fetchall()
result_offset = 0
# End timer and display time in milliseconds
end_time = time.time()
execution_time_ms = (end_time - start_time) * 1000
print(f"{execution_time_ms:.2f} ms")
ShowResults()
def ShowResults():
global results
global result_offset
if results is None:
print('No search present')
return
nx = result_offset + result_size
formatted_results = "\n".join(f'{row[1]:.2f} \033[36m{row[0]}\033[0m {row[2]}' for row in results[result_offset:nx])
result_offset = nx
print(formatted_results)
def RunCommand(cmd):
global result_size
cmd = cmd.split(" ")
match cmd[0]:
case ".next":
ShowResults()
case ".algo":
SelectAlgorithm(cmd[1], True)
case '.limit':
result_size = int(cmd[1])
case ".exit":
sys.exit(0)
case _:
print(f'Unknown command {cmd[0]}')
def DisplayCounts():
cursor.execute("SELECT COUNT(*) FROM articles")
article_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM redirects")
redirect_count = cursor.fetchone()[0]
cursor.execute("SELECT COUNT(*) FROM links")
link_count = cursor.fetchone()[0]
print(f"Number of")
print(f" - articles : {article_count}")
print(f" - links : {link_count}")
print(f" - redirects: {redirect_count}")
if __name__ == '__main__':
DisplayCounts()
while True:
cmd = input("> ")
if cmd[0] == ".":
RunCommand(cmd)
else:
Search(cmd)