Skip to content

Instantly share code, notes, and snippets.

@vgel
Created July 12, 2012 13:32
Show Gist options
  • Save vgel/3098126 to your computer and use it in GitHub Desktop.
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
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