By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,996 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,996 IT Pros & Developers. It's quick & easy.

Plot words from reading a file.

P: 1
I need to plot this information. I wrote the code to calculate the frequency of words in a file, but I need it to plot the 15 most frequent words in a bar graph. I really don't even know how to start. Any help?

Expand|Select|Wrap|Line Numbers
  1. import string
  2. def compItems((w1,c1), (w2,c2)):
  3.     if c1 > c2:
  4.         return - 1
  5.     elif c1 == c2:
  6.         return cmp(w1, w2)
  7.     else:return 1
  8. def main():
  9.     print "This program analyzes word frequency in a file"
  10.     print "and prints a report on the n most frequent words.\n"
  11.  
  12.     # get the sequence of words from the file
  13.     fname = raw_input("File to analyze: ")
  14.     text = open(fname,'r').read()
  15.     text = string.lower(text)
  16.     for ch in """!"#$%&()*+,-./:;<=>?@[\\]?_'`{|}?""":
  17.         text = string.replace(text, ch,' ')
  18.         words = string.split(text)
  19.  
  20.     # construct a dictionary of word counts
  21.     counts = {}
  22.     for w in words:
  23.         try:
  24.             counts[w] = counts[w] + 1
  25.         except KeyError:
  26.             counts[w] = 1
  27.  
  28.     # output analysis of n most frequent words.
  29.     n = 0
  30.     for words in counts:
  31.         n+=1
  32.     items =counts.items()
  33.     items.sort(compItems)
  34.     for i in range(n):
  35.         print "%-10s%5d" % items[i]
  36.  
  37. main()
May 1 '11 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.