Average Word Length

Thread Solved

Join Date: Mar 2007
Posts: 10
Reputation: pyguy25 is an unknown quantity at this point 
Solved Threads: 0
pyguy25 pyguy25 is offline Offline
Newbie Poster

Average Word Length

 
0
  #1
Mar 11th, 2007
This is a modified program from the word count program that I posted about. I am trying to calculate the average word length in a sentence. Here is my code so far:

  1. def main():
  2. print "This program will calculate the average word length in a sentence"
  3. print
  4. s = raw_input("Enter a sentence: ")
  5. words = string.split(s)
  6. wordCount = len(words)
  7. ch = string.split(words)
  8. charCount = len(ch)
  9. avgLength = charCount / wordCount
  10. print "The average word length of the sentence '", s,"', is", avgLength,"words."
  11. main()
I keep getting an error like 'list' object has no attribute 'split'.
Last edited by pyguy25; Mar 11th, 2007 at 12:23 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,521
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: Average Word Length

 
0
  #2
Mar 11th, 2007
I am a little confused with the 'string.split(s)' I thought it was more like s.split():
  1. s = "Just a test string"
  2. words = s.split()
  3. print words
Are there different versions of Python floating about?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Average Word Length

 
0
  #3
Mar 11th, 2007
Well, yes ... but they are strictly upgrades, not forks. The 'string.split(s)' is a now-deprecated way of writing 's.split()'
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Average Word Length

 
0
  #4
Mar 11th, 2007
I keep getting an error like 'list' object has no attribute 'split'.
And you really don't know why?
  1. words = string.split(s)
Ok, from your other post you know that words is a list. Fine.
  1. ch = string.split(words)
What are you doing here? You want to split a list? Worse than that, you want to do it with string.split()!

Here is a receipe how to find the average word-length:
1. Split the string into words (hint: We have that already )
2. Do something like total = 0. We need this later
3. Iterate over the words in the list. (hint: for word in words:)
4. Find the length of the current word (hint: a word is just a list of characters )
5. Add this length to total.

6. Ok, after we've done this with every word, total is the sum of the wordlengths.
7. Divide total by the number of words.
8. That's it.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: pyguy25 is an unknown quantity at this point 
Solved Threads: 0
pyguy25 pyguy25 is offline Offline
Newbie Poster

Re: Average Word Length

 
0
  #5
Mar 11th, 2007
Okay, I'll try that. Sorry, I'm a beginner at Python so I might ask stupid or obvious questions. Just bare with me. It'll come easier to me eventually.
Last edited by pyguy25; Mar 11th, 2007 at 2:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 10
Reputation: pyguy25 is an unknown quantity at this point 
Solved Threads: 0
pyguy25 pyguy25 is offline Offline
Newbie Poster

Re: Average Word Length

 
0
  #6
Mar 11th, 2007
ok, got it:

  1. def main():
  2. print "This program will calculate the average word length in a sentence"
  3. print
  4. s = raw_input("Enter a sentence: ")
  5. words = string.split(s)
  6. wordCount = len(words)
  7. sum = 0
  8. for word in words:
  9. ch = len(word)
  10. sum = sum + ch
  11. avg = sum / wordCount
  12. if avg is 1:
  13. print "In the sentence '", s,"', the average word length is", avg,"letter."
  14. else:
  15. print "In the sentence '", s,"', the average word length is", avg,"letters."
  16. main()
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 133
Reputation: mawe is an unknown quantity at this point 
Solved Threads: 58
mawe mawe is offline Offline
Junior Poster

Re: Average Word Length

 
0
  #7
Mar 11th, 2007
Okay, I'll try that. Sorry, I'm a beginner at Python so I might ask stupid or obvious questions. Just bare with me. It'll come easier to me eventually.
If my posts sound somewhat rude, I'm sorry for that. It's not meant this way.
Your questions are absolutely not stupid, and before you ask you try to find a solution by yourself. That's more than some others here do.

Ok, just one comment on your code:
  1. sum = 0
There is a builtin-function called sum(). It's bad style to call variables like builtins, and can lead to very confusing errors.

Here's a shorter version of your code(using sum):
  1. s = "This is a short sentence"
  2. l = s.split()
  3. print sum( [ len(word) for word in l ] ) / len(l)

Regards, mawe
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Average Word Length

 
0
  #8
Mar 11th, 2007
Good job! Just to make your code robust, you should check for dividing by zero:

  1. if wordCount != 0:
  2. avg = sum / wordCount
  3. else:
  4. avg = 0

If you want to continue to improve the code beyond that, you might consider turning the average word count into a separate function, so that it could be used by other programs for any old purpose. Doing so would have the positive side effect of cleaning up your code.

In general, it's usually better to have lots of small functions that do one thing rather than a few number of large functions that do lots of things. Your main() interacts with the user, finds the average, and prints the results ... a bit too Swiss-army-knifish for my taste.

Also, you might consider reading the Python docs tutorial on list generators. A list generator can simplfiy your code like this:

  1. total = sum([len(word) for word in words]

That one line replaces the entire sum=0 and for-loop section.

Jeff
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 148
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 40
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Average Word Length

 
0
  #9
Mar 11th, 2007
split will break if your input have punctuations. eg word.Test . This will be counted as 1?
(although a blank space comes after a full stop.)
anyway, here's another way to do it,
  1. >>> import re
  2. >>> s = "This program will calculate the average word length in a sentence.Tehas dfsdskj 323 5543"
  3. >>> len(re.findall("\w+",s))
  4. 15
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC