473,385 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Plot words from reading a file.

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
0 1342

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

Similar topics

14
by: deko | last post by:
Do I need to use flock() when reading a file into an array? It's possible that the file in question could be open at the time the file('filename') request is made. I realize flock() is required...
19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
12
by: dough | last post by:
I want to read in lines from a file and then seperate the words so i can do a process on each of the words. Say the text file "readme.txt" contains the following: In the face of criticism from...
3
by: charlz | last post by:
im been having difficuties in reading and storing from an input file. the problem goes like this: Write a program that reads in a whole bunch of words from all file (each word is no more than 20...
3
by: miss time | last post by:
Hi all, my java friends ^-^ I have next week quiz in reading file text ,and understand the topic very well. can any one give some question related to this topic .this help me more to...
1
by: shrimpy | last post by:
hi every one, i am new to python, and coz i want to write a handy command for my linux machine, to find a word in all the files which are under the current folder. the code is half done, but...
1
Coldfire
by: Coldfire | last post by:
Hi, The strange problem i am having is, the input element of type='file' not reading file names after 20 file elements. It simple returns null on reading the 'name' of file. The code is...
1
by: bjoarn | last post by:
I have an Application C# handling file reading, building index on this file, using dll wrapped with SWIG. The dll is originaly programmed in C++. Dll reports back to the the C# programm throug...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.