Created
October 24, 2014 15:40
-
-
Save bbengfort/8f86631688a7186bf663 to your computer and use it in GitHub Desktop.
Getting a normalized FreqDist
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import nltk | |
import string | |
def tokenize(text): | |
stopwords = set(nltk.corpus.stopwords.words('english')) | |
for token in nltk.word_tokenize(text): | |
if token in stopwords or token in string.punctuation: | |
continue | |
yield token.lower() | |
def count(text): | |
return nltk.FreqDist(tokenize(text)) | |
if __name__ == "__main__": | |
for token, count in count("The cat in the hat sat on the cat mat, with aplumb.").values(): | |
print "%s: %i" % (token, count) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment