943,740 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1202
  • Python RSS
May 29th, 2009
0

Average Word Count in a text

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ararik is offline Offline
3 posts
since May 2009
May 29th, 2009
0

Re: Average Word Count in a text

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.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 56
Solved Threads: 65
Posting Whiz in Training
slate is offline Offline
242 posts
since Jun 2008
May 30th, 2009
0

Re: Average Word Count in a text

ararik, please do not double post!

You are almost there, as slate says, simply sum up word counts and word lenghts:
python Syntax (Toggle Plain Text)
  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.
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Jun 2nd, 2009
0

Re: Average Word Count in a text

Thank you slate and sneekula!
Reputation Points: 10
Solved Threads: 1
Newbie Poster
ararik is offline Offline
3 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: wxPython - how do I set a windows on the top of others??
Next Thread in Python Forum Timeline: Simple File Operation Question





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC