943,154 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2214
  • Python RSS
Jan 29th, 2010
0

Count word frequency using dictionaries

Expand Post »
Python Syntax (Toggle Plain Text)
  1. from string import *
  2.  
  3. def removePunctuation(sentence):
  4. sentence = lower(sentence)
  5. new_sentence = ""
  6. for char in sentence:
  7. if char not in punctuation:
  8. new_sentence = new_sentence + char
  9.  
  10. return new_sentence
  11.  
  12. def wordFrequences(sentence):
  13. wordCounts = {}
  14. split_sentence = new_sentence.split()
  15. print split_sentence
  16. for entry in split_sentence:
  17. for word in entry:
  18. wordCounts[entry] = wordCounts.get (entry,0) + 1
  19. wordCounts.items()
  20. return wordCounts
  21.  
  22. sentence = "This is a test sentence, to test the function."
  23. new_sentence = removePunctuation(sentence)
  24. wordFrequences(sentence)

Hi I am trying to write a program which calculates how many times a certain word appears in a string.

Could someone help me how to do this, i.e. is there something similar instead of using .get which counts the characters.

At the moment i get the output of:
{'a': 1, 'function': 8, 'sentence': 8, 'this': 4, 'is': 2, 'to': 2, 'test': 8, 'the': 3}

I am trying to get the following:
{'this': 1, 'a': 1, 'is': 1, 'test': 2, ...}
Last edited by axa121; Jan 29th, 2010 at 3:33 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
axa121 is offline Offline
20 posts
since Nov 2009
Jan 29th, 2010
0
Re: Count word frequency using dictionaries
Ok no need for help.

I took a break and when I back I got the solution straight away.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
axa121 is offline Offline
20 posts
since Nov 2009
Jan 29th, 2010
0
Re: Count word frequency using dictionaries
If you find a solution to one of your own problems, it is considered polite to post the solution regardless because other people might also have the same problem.

As for my solution to the problem statement?

I'd probably just strip the string of any non-alphabetic characters excepting spaces and newlines, replace all newlines with spaces, split the resulting string around spaces, iterate over the resulting sequence, and add the word to the dictionary if it is not present with a count of one or increment the counter for the word. (Using the word as the dictionary key. First check that the dictionary has the key for the word, if not then add the key as one or if it does increment the key.)
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009
Jan 29th, 2010
0
Re: Count word frequency using dictionaries
Also, it is considered bad code to use the '+' operator to concatenate strings. Strings are immutable objects, so appending a string to another string creates a new string object which takes time and memory.

It is better to store each piece in a list until a concatenated string is needed, then join each piece using a string's "join" method on the list.

Python Syntax (Toggle Plain Text)
  1. string1 = 'This'
  2. string2 = 'is'
  3. string3 = 'worse.'
  4. final_string = string1 + ' ' + string2 + ' ' + string3
  5. """This is worse."""
  6.  
  7. mylist = ['This', 'is', 'better.']
  8. better_string = ' '.join(mylist)
  9. """This is better."""
Reputation Points: 106
Solved Threads: 35
Posting Whiz in Training
lrh9 is offline Offline
238 posts
since Oct 2009
Jan 30th, 2010
0
Re: Count word frequency using dictionaries
Python Syntax (Toggle Plain Text)
  1. from string import *
  2.  
  3.  
  4. def removePunctuation(sentence):
  5. sentence = lower(sentence)
  6. new_sentence = ""
  7. for char in sentence:
  8. if char not in punctuation:
  9. new_sentence = new_sentence + char
  10.  
  11. return new_sentence
  12.  
  13. def wordFrequences(sentence):
  14. wordFreq = {}
  15. split_sentence = new_sentence.split()
  16. for word in split_sentence:
  17. wordFreq[word] = wordFreq.get(word,0) + 1
  18. wordFreq.items()
  19. print wordFreq
  20.  
  21. sentence = "The first test of the function"
  22. new_sentence = removePunctuation(sentence)
  23. wordFrequences(sentence)

Here is the corrected version.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
axa121 is offline Offline
20 posts
since Nov 2009
Jan 30th, 2010
0
Re: Count word frequency using dictionaries
I think you want to use new_sentence (and is one of the positive results of posting code).
Python Syntax (Toggle Plain Text)
  1. def wordFrequences(new_sentence):
  2. wordFreq = {}
  3.  
  4. ## new_sentence was not defined
  5. split_sentence = new_sentence.split()
  6.  
  7. for word in split_sentence:
  8. wordFreq[word] = wordFreq.get(word,0) + 1
  9. wordFreq.items()
  10. print wordFreq
  11.  
  12. sentence = "The first test of the function"
  13. new_sentence = removePunctuation(sentence)
  14. wordFrequences(new_sentence)
Last edited by woooee; Jan 30th, 2010 at 1:37 pm.
Reputation Points: 738
Solved Threads: 690
Nearly a Posting Maven
woooee is offline Offline
2,295 posts
since Dec 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: wxpython question
Next Thread in Python Forum Timeline: Help noob with Skype4Py in Python





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


Follow us on Twitter


© 2011 DaniWeb® LLC