[Tutor] Word Frequency Chart

Alan Gauld alan.gauld at btinternet.com
Wed Dec 24 09:43:32 CET 2008


"Ishan Puri" <ballerz4ishi at sbcglobal.net> wrote

> I am a high school student. I needed help (from the beginning) 
> making
> a word frequency chart that I can use to chart out the numerical 
> frequencies
> of words.

We are happy to help but as a rile we don;t write programs for you,
we help you write your own programs. (Teach a man to fish and you
feed him forever etc)

> program could have something like "Type filename:" when I run it 
> that
> would be fantastic.

OK, You can use raw_input for that.
And to capture the counts you can use a dictionary.
You will need to:

1) Capture the filename
2) open the file
3) iterate over the file, line by line
4) iterate over each line word by word
5) if the word exists in your dictionary increment the count,
    else add it to the dictionary with a count of 1

Thats probably enough for a first stage.
If you don't know how to do any of the steps above let us know but 
they
are all covered in any of the beginners tutiorials.

> I have pylab as well, so the code could include something that would
> make the chart when I type in the filename. That would be great.

You can make charts directly from Python or you could save the data
to a file that some other charting software can read (say Excel).
But get the basic daya capture right first, worry about the 
presentation later.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list