944,163 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 6387
  • Python RSS
Mar 11th, 2007
0

Average Word Length

Expand Post »
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:

Python Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pyguy25 is offline Offline
10 posts
since Mar 2007
Mar 11th, 2007
0

Re: Average Word Length

I am a little confused with the 'string.split(s)' I thought it was more like s.split():
Python Syntax (Toggle Plain Text)
  1. s = "Just a test string"
  2. words = s.split()
  3. print words
Are there different versions of Python floating about?
Reputation Points: 407
Solved Threads: 36
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Mar 11th, 2007
0

Re: Average Word Length

Well, yes ... but they are strictly upgrades, not forks. The 'string.split(s)' is a now-deprecated way of writing 's.split()'
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 11th, 2007
0

Re: Average Word Length

Quote ...
I keep getting an error like 'list' object has no attribute 'split'.
And you really don't know why?
Python Syntax (Toggle Plain Text)
  1. words = string.split(s)
Ok, from your other post you know that words is a list. Fine.
Python Syntax (Toggle Plain Text)
  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.
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Mar 11th, 2007
0

Re: Average Word Length

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pyguy25 is offline Offline
10 posts
since Mar 2007
Mar 11th, 2007
0

Re: Average Word Length

ok, got it:

Python Syntax (Toggle Plain Text)
  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()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
pyguy25 is offline Offline
10 posts
since Mar 2007
Mar 11th, 2007
0

Re: Average Word Length

Quote ...
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:
Python Syntax (Toggle Plain Text)
  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):
Python Syntax (Toggle Plain Text)
  1. s = "This is a short sentence"
  2. l = s.split()
  3. print sum( [ len(word) for word in l ] ) / len(l)

Regards, mawe
Reputation Points: 19
Solved Threads: 58
Junior Poster
mawe is offline Offline
133 posts
since Sep 2005
Mar 11th, 2007
0

Re: Average Word Length

Good job! Just to make your code robust, you should check for dividing by zero:

Python Syntax (Toggle Plain Text)
  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:

Python Syntax (Toggle Plain Text)
  1. total = sum([len(word) for word in words]

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

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 11th, 2007
0

Re: Average Word Length

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,
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 75
Solved Threads: 44
Junior Poster
ghostdog74 is offline Offline
156 posts
since Apr 2006

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: Assure n is a number
Next Thread in Python Forum Timeline: simple random function





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


Follow us on Twitter


© 2011 DaniWeb® LLC