Hacker News new | past | comments | ask | show | jobs | submit login

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)



Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: