This repository was archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathautocomplete.py
More file actions
52 lines (44 loc) · 1.32 KB
/
autocomplete.py
File metadata and controls
52 lines (44 loc) · 1.32 KB
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
import redis
PREFIX = 'trie'
TERMINAL = '+'
r = redis.Redis(db=6)
def add_word(word):
key = PREFIX + ':'
pipeline = r.pipeline(True)
for c in word:
r.zadd(key, c, ord(c))
key += c
r.zadd(key, TERMINAL, 0)
pipeline.execute()
def suggest(text):
for c in r.zrange(PREFIX + ":" + text, 0, -1):
if c == TERMINAL:
yield text
else:
for t in suggest(text + c):
yield t
def format_suggestions(s_list):
s_list = list(s_list)
l = len(list(s_list))
if not l:
return ''
if l == 1:
return '[%s]' % s_list[0]
return '[%s],%s'[:50] % (s_list[0], ','.join(s_list[1:5]))
if __name__ == '__main__':
last_word = ''
insert = False
# read any new words into the redis trie
for line in open('/var/log/keystroke.log', 'r'):
stripped_word = line.strip()
if stripped_word and 3 < len(stripped_word) < 30 and '!' not in stripped_word:
add_word(stripped_word)
if 2 < len(stripped_word) and '!' not in stripped_word:
last_word = stripped_word
if stripped_word[-4:] == '!!!!':
insert = True
# get the suggestion for the last typed thing
if insert:
print 'AAAAAAA'
elif last_word:
print format_suggestions(suggest(last_word))