Counting word frequency in a text with Python

From XPUB & Lens-Based wiki
Revision as of 11:34, 11 March 2014 by Michael Murtaugh (talk | contribs) (Michael Murtaugh moved page WordFreq.cgi to Counting word frequency in a text with Python)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
import string

wd = {}

# for every line, every word in line, add it to the dictionary
for line in open("text.txt"):
  for word in line.split():
    word = word.strip(string.punctuation).lower()
    # wd[word] = True
    wd[word] = wd.get(word, 0) + 1 # for word counting

# get the dictionary keys, and alphabetize
allwords = wd.keys()
allwords.sort()
for word in allwords:
  # print w
  print word, wd[word]