Created
July 12, 2012 13:32
-
-
Save vgel/3098126 to your computer and use it in GitHub Desktop.
A autocomplete-like word suggester. Run with `python <file> "/usr/share/dict/words"` Enter words at command line and hit enter for suggestions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
t={} | |
def w(o): | |
c=t | |
for l in o: | |
c[l]=c[l]if l in c else{} | |
c=c[l] | |
c[None]=None | |
g=lambda t,b,i:reduce(lambda i,l:i+[b]if l is None else g(t[l],b+l,i),t,i) | |
map(w,open(sys.argv[1]).read().split("\n")) | |
while 1: | |
b=raw_input() | |
print g(reduce(lambda c,l:c[l]if l in c else[],b,t),b,[]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment