Counting word frequency in a text with Python: Difference between revisions

From XPUB & Lens-Based wiki
(New page: Exercise to produce a "word cloud" based on frequency of words in a text.)
 
No edit summary
Line 1: Line 1:
Exercise to produce a "word cloud" based on frequency of words in a text.
<source lang="python">
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]
</source>

Revision as of 14:52, 17 February 2009

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]