User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Python section within the Software Development category of DaniWeb, a massive community of 428,379 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,578 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Python advertiser: Programming Forums
Views: 397 | Replies: 5
Reply
Join Date: Jun 2008
Posts: 4
Reputation: atsuko is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
atsuko atsuko is offline Offline
Newbie Poster

Count given word

  #1  
Jun 9th, 2008
Hi there,

I am new to python.
Can somebody tell me how can I count a given word from a file.
I found lots of solution for counting all the words in a file, but not for some particular ones.

Thanks in advance
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2008
Posts: 35
Reputation: Shadow14l is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 1
Shadow14l Shadow14l is offline Offline
Light Poster

Re: Count given word

  #2  
Jun 9th, 2008
  1. string = "hi"
  2.  
  3. f = open("filename.txt")
  4. contents = f.read()
  5. f.close()
  6.  
  7. print "Number of '" + string + "' in your file is:", contents.count("hi")

Replace "hi" with the word you want to count basically.

Feel free to ask anymore questions!
Last edited by Shadow14l : Jun 9th, 2008 at 3:20 pm.
Reply With Quote  
Join Date: Jun 2008
Posts: 4
Reputation: atsuko is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
atsuko atsuko is offline Offline
Newbie Poster

Re: Count given word

  #3  
Jun 9th, 2008
Thank you so much!!!
Reply With Quote  
Join Date: Aug 2005
Posts: 1,142
Reputation: Ene Uran is an unknown quantity at this point 
Rep Power: 6
Solved Threads: 66
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Veteran Poster

Re: Count given word

  #4  
Jun 9th, 2008
There is a problem with count() as shown below:
  1. string = "hi"
  2.  
  3. # test text
  4. text = "hi, I am a history buff with a hideous hidrosis history"
  5.  
  6. print "Number of '" + string + "' in your file is:", text.count("hi")
  7.  
  8. """
  9. my result -->
  10. Number of 'hi' in your file is: 5
  11. """
Last edited by Ene Uran : Jun 9th, 2008 at 4:28 pm.
drink her pretty
Reply With Quote  
Join Date: Jun 2008
Posts: 8
Reputation: Dunganb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 2
Dunganb Dunganb is offline Offline
Newbie Poster

Re: Count given word

  #5  
Jun 10th, 2008
Hi atsuko,

Here is my version of a word finder. It has some serious limitations (it cannot search for words such as "It's" or anything like that due to the punctuation), and it cannot search for multiple words at one time, but at least in my tests it could find the word I was looking for in the correct amount. Here is an example of it working...

  1. paragraph = '''This is a test sentence. We will look for hi in this sentence.
  2. If we find 'hi', we want to keep count of it. Remember, hi
  3. can be Hi or hi. Hi can also have characters before or
  4. after it ie (hi. hi, hi:). There should be a total of 10 'hi'
  5. in this sentence, not any more for words like 'this' or
  6. 'hippo' or 'hiccups' '''
  7.  
  8. word = 'hi'
  9.  
  10. for x in range(33,64):
  11. char = chr(x)
  12. paragraph = paragraph.replace(char, '')
  13.  
  14. for x in range(91,97):
  15. char = chr(x)
  16. paragraph = paragraph.replace(char, '')
  17.  
  18. listParagraph = paragraph.split()
  19. reducedList = [n for n in listParagraph if len(n) == len(word)]
  20. reducedParagraph = ' '.join(reducedList)
  21. reducedParagraph = reducedParagraph.lower()
  22.  
  23. count = reducedParagraph.count(word.lower())
  24. if count == 0:
  25. print "The word", "'" + word + "'", "does not occur in this section of text."
  26. else:
  27. print "The word", "'" + word + "'", "occurs", count, "times in this section."
  28.  
  29.  

I read an article online about tokenization, but for my current knowledge level I couldn't really do anything with it. From what I read though it is a more complicated but more exact way of finding words.
Last edited by Dunganb : Jun 10th, 2008 at 8:16 pm. Reason: Made the code a little nicer...
Reply With Quote  
Join Date: Dec 2006
Posts: 450
Reputation: woooee is on a distinguished road 
Rep Power: 2
Solved Threads: 62
woooee woooee is offline Offline
Posting Pro in Training

Re: Count given word

  #6  
Jun 10th, 2008
You could also replace the following:
reducedList = [n for n in listParagraph if len(n) == len(word)]
## replace with this
reduced_list = [n for n in list_paragraph if word == n.lower()]
if len(reduced_list):
    print "The word", "'" + word + "'", "occurs", len(reduced_list), "times in this section."
else:
    print "The word", "'" + word + "'", "does not occur in this section of text."
#
# anyway, here is another solution
#
paragraph = '''This is a test sentence.  We will look for hi in this sentence.
If we find 'hi', we want to keep count of it.  Remember, hi
can be Hi or hi.  Hi can also have characters before or
after it ie (hi. hi, hi:).  There should be a total of 10 'hi'
in this sentence, not any more for words like 'this' or
'hippo' or 'hiccups' '''

word='hi'
p_list = paragraph.split()
ctr=0
for p_word in p_list:
   p_word = p_word.lower()
   if p_word == word:
      ctr += 1
   elif not p_word.isalpha():     ## has non-alpha characters
      new_word = ""
      for chr in p_word:
         if chr.isalpha():
            new_word += chr
      if new_word == word:
         ctr += 1
print "The word '%s' was found %d times" % (word, ctr)
Last edited by woooee : Jun 10th, 2008 at 10:41 pm.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Python Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Python Forum

All times are GMT -4. The time now is 7:08 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC