-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccurate-search-python.py
161 lines (130 loc) · 5.56 KB
/
accurate-search-python.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import re
from typing import List, Dict, Set, Tuple
class AccurateSearch:
def __init__(self):
self.c: List[Dict] = []
self.t: Dict = None # Initialize to None, create only when needed
def add_text(self, id: int, text: str, distance_behind: int = 0):
if id is None:
raise ValueError("id is a required parameter")
cleaned_text = self.cleanup_text(text)
if len(cleaned_text) > 1000:
last_space = cleaned_text.rfind(" ", 0, 1000)
cleaned_text = cleaned_text[:last_space]
self._insert_sorted({"i": id, "t": cleaned_text}, self.c)
words = cleaned_text.split()
if self.t is None:
self.t = {"d": {}, "i": [], "n": {}}
distance_behind = min(int(distance_behind), 1000)
for word in words:
node = self.t
for char in word:
if char not in node["n"]:
node["n"][char] = {"d": {}, "i": [], "n": {}}
node = node["n"][char]
position = cleaned_text.index(word) + 1
if distance_behind:
position += distance_behind
if id in node["d"]:
node["d"][id] = min(node["d"][id], position)
else:
node["d"][id] = position
node["i"].append(id)
def search(self, query: str) -> List[int]:
results = self.accurate_search(query)
if not results:
results = self.accurate_search(self.full_cleanup_text(query))
if not results:
results = self.fuzzy_search(query)
return results
def accurate_search(self, query: str) -> List[int]:
words = list(set(self.cleanup_text(query).split()))
if not self.t:
raise ValueError("There is no text added to search index")
results: Dict[int, List[float]] = {}
visited: Dict[str, Set[int]] = {}
for word in words:
if word:
node = self.t
for char in word:
if char not in node["n"]:
break
node = node["n"][char]
else:
self._process_word(word, node, results, visited, 4)
return self._sort_results(results)
def fuzzy_search(self, query: str) -> List[int]:
cleaned_query = self.full_cleanup_text(query)
for i in range(len(query) - 1, 1, -1):
cleaned_query += " " + query[:i]
words = [w for w in cleaned_query.split() if len(w) > 1]
cleaned_query = " ".join(words)
return self.accurate_search(cleaned_query)
def suggestions(self, query: str, limit: int) -> List[str]:
cleaned_query = self.cleanup_text(query)
suggestions = []
for item in self.c:
text = item["t"]
if len(text) > len(cleaned_query):
index = text.find(cleaned_query)
if index >= 0 and (index == 0 or text[index - 1] == " "):
space_index = text.find(" ", index + len(cleaned_query) + 1)
suggestion = text[index:space_index] if space_index > 0 else text[index:]
if len(suggestion) > len(cleaned_query) and suggestion not in suggestions:
suggestions.append(suggestion)
if len(suggestions) >= limit:
break
return sorted(suggestions, key=len)
def remove(self, id: int):
self.c = [item for item in self.c if item["i"] != id]
if self.t:
self._remove_from_tree(id, self.t)
def cleanup_text(self, text: str) -> str:
text = re.sub(r'<[^>]*>?', ' ', text)
text = text.lower()
text = re.sub(r'[`~!%^*()_|=?;:",.<>\{\}\[\]\\\/]', ' ', text)
return text.strip()
def full_cleanup_text(self, text: str) -> str:
text = re.sub(r'<[^>]*>?', ' ', text)
text = text.lower()
text = re.sub(r'[^a-z0-9 ]', ' ', text)
return text.strip()
def _process_word(self, word: str, node: Dict, results: Dict[int, List[float]], visited: Dict[str, Set[int]], score: float):
if word not in visited:
visited[word] = set()
for id in node["i"]:
if id not in visited[word]:
visited[word].add(id)
if id not in results:
results[id] = [100000, 0]
results[id][0] -= score
results[id][1] += node["d"][id]
for char, child_node in node["n"].items():
self._process_word(word, child_node, results, visited, max(score / 2, 1))
def _sort_results(self, results: Dict[int, List[float]]) -> List[int]:
sorted_results = sorted(results.items(), key=lambda x: (x[1][0], x[1][1]))
return [id for id, _ in sorted_results]
def _remove_from_tree(self, id: int, node: Dict):
if id in node["i"]:
node["i"].remove(id)
if id in node["d"]:
del node["d"][id]
for child_node in node["n"].values():
self._remove_from_tree(id, child_node)
@staticmethod
def _insert_sorted(item: Dict, arr: List[Dict]):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if AccurateSearch._compare_items(item, arr[mid]) > 0:
low = mid + 1
else:
high = mid - 1
arr.insert(low, item)
@staticmethod
def _compare_items(a: Dict, b: Dict) -> int:
if 'r' in a:
return -1 if 'r' in b else -1
if 'r' in b:
return 1
return a['i'] - b['i']