-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathdesign-search-autocomplete-system.py
58 lines (41 loc) · 1.8 KB
/
design-search-autocomplete-system.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
import heapq
from collections import defaultdict
class AutocompleteSystem:
def __init__(self, sentences: List[str], times: List[int]):
self.rev_idx = defaultdict(lambda: list())
self.sentence_buffer = ""
for sentence, times in zip(sentences, times):
self._add_sentence(sentence, times)
def _add_sentence(self, sentence, times):
for pos in range(len(sentence)):
for sub_pos in range(len(self.rev_idx[sentence[:pos + 1]])):
c, s = self.rev_idx[sentence[:pos + 1]][sub_pos]
if s == sentence:
self.rev_idx[sentence[:pos + 1]][sub_pos] = (c + times, sentence)
break
else:
self.rev_idx[sentence[:pos + 1]].append((times, sentence))
#heapq.heapify(self.rev_idx[sentence[:pos + 1]])
self.rev_idx[sentence[:pos + 1]].sort(key=lambda x: str(10000 - x[0]) + x[1])
def _get_top_k_for_substr(self, substr, k):
#tmp = []
#prev_times = 0
#result = []
#for times, sentence in heapq.nlargest(k, self.rev_idx[substr], lambda x: x[0]):
# if times != prev_times:
# result.extend(sorted(tmp))
# tmp = []
# prev_times = times
# tmp.append(sentence)
#result.extend(sorted(tmp))
return [s for c, s in self.rev_idx[substr][:k]]
def input(self, c: str) -> List[str]:
if c != "#":
self.sentence_buffer += c
else:
self._add_sentence(self.sentence_buffer, 1)
self.sentence_buffer = ""
return self._get_top_k_for_substr(self.sentence_buffer, 3)
# Your AutocompleteSystem object will be instantiated and called as such:
# obj = AutocompleteSystem(sentences, times)
# param_1 = obj.input(c)