Average Word Count in a text

Thread Solved

Join Date: May 2009
Posts: 3
Reputation: ararik is an unknown quantity at this point 
Solved Threads: 1
ararik ararik is offline Offline
Newbie Poster

Average Word Count in a text

 
0
  #1
May 29th, 2009
Hi,

I am very new to Python and computer programming language. I have been working on a text file where I want to find the average length of words in a text file. Well, to start with:

Let's say I have only one sentence in my text file (we can worry about the multiple sentences later). Here's the text:

"But Buffet wrote that he remains hopeful about the long-term prospects for his company and the nation despite the turmoil shaking the world's economies."

I can find the length of each word in this sentence by using the following code;

>>> myfile = open("c:/test/oneline.txt","r")
>>> for line in myfile:
... words=line.split()
... wordcounts = len(words)
... for word in words:
... lengthword = len(word)
... print lengthword

which gives me
3
6
5
4
2
7
7
....and so on.

My problem is writing the rest of the code i.e. summing this up and dividing it by the total number of words.

Any help?

Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: Average Word Count in a text

 
0
  #2
May 29th, 2009
First of all, please read this. It is on the very beginning of the forum. Your indent cannot be reconstructed from your post, so your code can only be guessed.

I think your problem is not, that you are new to python. In any language there would be a problem with that solution.

How would you say your algo in real words?
You want to divide the sum of wordlentghts with the sum of word pieces.
  1. sum_of_wordlengths=0
  2. sum_of_wordpieces=0
  3. For all line in the file:
  4. sum_of_wordlengths=sum_of_wordlengths+ length of words in the line
  5. sum_of_wordpieces=sum_of_wordpieces+ number of words in the line
  6. print sum_of_wordlengths/sum_of_wordpieces

Your code does not sum up the pieces and the lenghts, just stores and prints the last length.
Last edited by slate; May 29th, 2009 at 10:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Average Word Count in a text

 
0
  #3
May 30th, 2009
ararik, please do not double post!

You are almost there, as slate says, simply sum up word counts and word lenghts:
  1. myfile = open(filename, "r")
  2. wordcount_sum = 0
  3. wordlength_sum = 0
  4. for line in myfile:
  5. words = line.split()
  6. # sum up the word counts
  7. wordcount_sum += len(words)
  8. for word in words:
  9. # sum up the word lengths
  10. wordlength_sum += len(word)
  11.  
  12. # invoke floating point division for Python versions < 3.0
  13. wordlength_average = wordlength_sum/float(wordcount_sum)
Last edited by sneekula; May 30th, 2009 at 11:22 am.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 3
Reputation: ararik is an unknown quantity at this point 
Solved Threads: 1
ararik ararik is offline Offline
Newbie Poster

Re: Average Word Count in a text

 
0
  #4
Jun 2nd, 2009
Thank you slate and sneekula!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC