Created
October 6, 2011 13:55
-
-
Save suminb/1267451 to your computer and use it in GitHub Desktop.
Counting word frequency
This file contains 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 codecs | |
import re | |
f = codecs.open('freq.txt', 'r', 'utf-8') | |
data = f.read() | |
f.close() | |
freq = {} | |
for word in re.findall(r'\w+', data, re.UNICODE): | |
if not word in freq: | |
freq[word] = 1 | |
else: | |
freq[word] += 1 | |
for k in freq: | |
print k, freq[k] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment