Turning a text in an alphabetical list of unique words

From XPUB & Lens-Based wiki
Revision as of 14:08, 16 March 2011 by Aymeric Mansoux (talk | contribs) (Created page with "Turns a text in an alphabetical list of unique words. Attempts to strip punctuation and lowercases everything. <source lang="python"> #!/usr/bin/env python import sys, string ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Turns a text in an alphabetical list of unique words. Attempts to strip punctuation and lowercases everything.

#!/usr/bin/env python

import sys, string

words = {}
for line in sys.stdin:
    for word in line.split():
        word = word.lower().strip(string.punctuation)
        words[word] = words.get(word, 0) + 1

for word in sorted(words.keys()):
    print word,
print