-
-
Save vyraun/2e50a003963ce8388b5ac6c1aae431aa to your computer and use it in GitHub Desktop.
Python snippet for generating word frequencies for Wordle (using NLTK)
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
#! /usr/bin/env python | |
# wordcount.py: parse & return word frequency | |
import sys, nltk | |
f = open(sys.argv[1], 'rU') | |
txt = f.read() | |
f.close() | |
tokens = nltk.word_tokenize(txt) # tokenize text | |
clean_tokens = [] | |
for word in tokens: | |
word = word.lower() | |
if word.isalpha(): # drop all non-words | |
clean_tokens.append(word) | |
# make frequency distribution of words | |
fd = nltk.FreqDist(clean_tokens) | |
for token in fd: | |
print token, ':', fd[token] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment