-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
140 lines (110 loc) · 4.25 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import tkinter as tk
import tkinter.ttk as ttk
import sqlite3
import time
from Levenshtein import distance
import inflect
# Define the Levenshtein distance function
def levenshtein_distance(s1, s2):
try:
return distance(s1, s2)
except Exception as e:
return 99999
# Define the custom function to find the minimum distance
def find_min_distance(*distances):
try:
valid_distances = [d for d in distances if d is not None]
if valid_distances:
min_distance = min(valid_distances)
return min_distance
return None
except Exception as e:
return None
def find_min_index(*distances):
try:
valid_distances = [d for d in distances if d is not None]
if valid_distances:
min_distance = min(valid_distances)
min_distance_index = distances.index(min_distance)
return min_distance_index
return None
except Exception as e:
return None
conn = sqlite3.connect('taxonomy_names.db')
conn.create_function('LEVENSHTEIN', 2, levenshtein_distance)
conn.create_function('FIND_MIN_DISTANCE', 4, find_min_distance)
conn.create_function('FIND_MIN_INDEX', 4, find_min_index)
cursor = conn.cursor()
# Sample list of autocomplete suggestions
autocomplete_list = []
def load_autocomplete_list():
cursor.execute("SELECT scientific_name FROM organism_names_fts LIMIT 10;")
results = cursor.fetchall()
for row in results:
# print(row)
autocomplete_list.append(row[0])
print(autocomplete_list)
def on_key_press(event):
# Get the current text in the entry widget
current_text = entry.get()
# Clear any previous autocomplete suggestions
autocomplete_box.delete(0, tk.END)
# Filter and display matching suggestions
autocomplete_list = search_organisms()
# matching_suggestions = [suggestion for suggestion in autocomplete_list if suggestion.startswith(current_text)]
for suggestion in autocomplete_list:
autocomplete_box.insert(tk.END, suggestion)
def on_suggestion_select(event):
# Get the selected suggestion from the autocomplete box
selected_suggestion = autocomplete_box.get(autocomplete_box.curselection())
# Set the selected suggestion as the text in the entry widget
entry.delete(0, tk.END)
entry.insert(tk.END, selected_suggestion)
def search_organisms():
user_input = entry.get()
print(user_input)
engine = inflect.engine()
singular_noun = engine.singular_noun(user_input)
if not singular_noun:
singular_noun = user_input
start = time.time()
cursor.execute(f'''
SELECT *,
FIND_MIN_DISTANCE(LEVENSHTEIN(scientific_name, '{user_input}'), LEVENSHTEIN(common_name, '{user_input}'),
LEVENSHTEIN(genbank_common_name, '{user_input}'), LEVENSHTEIN(synonym, '{user_input}')) AS min_distance,
FIND_MIN_INDEX(LEVENSHTEIN(scientific_name, '{user_input}'), LEVENSHTEIN(common_name, '{user_input}'),
LEVENSHTEIN(genbank_common_name, '{user_input}'), LEVENSHTEIN(synonym, '{user_input}')) AS min_index
FROM organism_names_fts
WHERE common_name MATCH '"{singular_noun}"*'
OR genbank_common_name MATCH '"{singular_noun}"*'
OR synonym MATCH '"{singular_noun}"*'
OR scientific_name MATCH '"{singular_noun}"*'
ORDER BY min_distance
LIMIT 10;
''')
results = cursor.fetchall()
end = time.time()
print(f'Result: returned in {(end-start)*1000:.4f}ms')
autocomplete_list = []
for row in results:
print(row)
autocomplete_list.append(row[row[-1]])
return autocomplete_list
load_autocomplete_list()
# Create the main window
window = tk.Tk()
window.title("Autocomplete Demo")
# Create an entry widget for user input
entry = tk.Entry(window)
entry.pack()
# Create a listbox widget for autocomplete suggestions
autocomplete_box = tk.Listbox(window)
autocomplete_box.pack()
# Bind keypress event to the entry widget
entry.bind("<KeyRelease>", on_key_press)
# Bind selection event to the autocomplete box
autocomplete_box.bind("<<ListboxSelect>>", on_suggestion_select)
# # Create a button to search for organisms
search_button = tk.Button(window, text="Search")
search_button.pack()
window.mainloop()