|
|
|
|
|
by psykotic
5065 days ago
|
|
Generating the word tree is short and simple, though. It's also useful as a standalone Unix utility. Here's my one-minute hack: import sys
import collections
tree = lambda: collections.defaultdict(lambda: [0, tree()])
def printtree(node, indent=0):
padding = indent * ' '
for word, (count, child) in node.iteritems():
print "%s%s %d" % (padding, word, count)
printtree(child, indent + 2)
wordtree = tree()
for line in sys.stdin:
node = wordtree
for word in line.split():
node[word][0] += 1
node = node[word][1]
printtree(wordtree)
|
|