import sys class SubsetTree(object): alphabet = "abcdefghijklmnopqrstuvwxyz" def GetHistogram(self, word): word = sorted(word) ret = [0] * len(SubsetTree.alphabet) pos = 0 for i in range(len(SubsetTree.alphabet)): while pos < len(word) and word[pos] == SubsetTree.alphabet[i]: pos += 1 ret[i] += 1 if pos >= len(word): break if SubsetTree.alphabet[i] > word[pos]: pos += 1 return ret def __init__(self, file): self.tree = dict() for word in file: word = word.strip().lower() hist = self.GetHistogram(word) current = self.tree for i in range(len(hist) - 1): if hist[i] not in current: current[hist[i]] = dict() current = current[hist[i]] if hist[len(hist) - 1] not in current: current[hist[len(hist) - 1]] = list() current[hist[len(hist) - 1]].append(word) def Lookup(self, word): hist = self.GetHistogram(word.strip().lower()) nodes = [self.tree] for i in range(len(hist)): newnodes = list() for node in nodes: newnodes.extend(node[x] for x in node if x <= hist[i]) nodes = newnodes wordlist = sum(nodes, []) return sorted(wordlist, cmp=lambda x,y: cmp(len(y), len(x))) tree = SubsetTree(open('/usr/share/dict/words', 'r')) wordlist = [] count = 5 ptr = 0 while True: print "?", word = sys.stdin.readline() if len(word.strip()) > 0: wordlist = tree.Lookup(word) ptr = 0 print wordlist[ptr:ptr+count] ptr += 5