-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie.py
More file actions
89 lines (77 loc) · 1.94 KB
/
trie.py
File metadata and controls
89 lines (77 loc) · 1.94 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
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
"""A basic implementation of a trie."""
def trie(words):
root = {}
for word in words:
current = root
for letter in word:
current = current.setdefault(letter, {})
current["*"] = "*"
return root
def find(T, w):
current = T
for letter in w:
if letter not in current:
return None # Not found
current = current[letter]
return current # Returns node in tree
def _dfs(T):
for k in T:
if k == "*":
yield ""
else:
for v in _dfs(T[k]):
yield k + v
def complete(T, w):
C = find(T, w) # continuations
if not C:
return []
for c in _dfs(C):
yield w + c
if __name__ == "__main__":
import sys
import termios
import tty
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(fd)
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
DICT = "/usr/share/dict/words"
words = []
with open(DICT, "r") as fin:
for line in fin:
w = line.strip().lower()
if w.isalnum():
words.append(w)
T = trie(words)
w = ""
inp = ""
entered = []
print("Enter word: ")
while True:
inp = getch()
if inp == "Q":
break
elif ord(inp) == 13:
entered.append(w)
w = ""
inp = ""
elif ord(inp) == 127:
inp = ""
if w:
w = w[: len(w) - 1]
w = w + inp
if w:
for idx, x in enumerate(list(complete(T, w))):
print(1 + idx, x)
if idx >= 29:
break
print("=" * 20)
print(f'"{w}" (Type Q to exit, backspace to erase, enter to accept)')
print()
for w in entered:
print(w)