Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 4th, 2005, 1:06 AM   #1
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile Find a word in a text file

I need to read in a text file and find out how many times a particular word appears in the text. Any ideas?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Mar 4th, 2005, 9:17 AM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
sure, make two arrays, one that stores words, one that stores numbers, make them the same size ex.
1 - A 1 - 1
2 - B 2 - 3
.. ..
n - Z n - 5
so now you go through every word in the file, and if it's already in the first array, increment the index at the second array, if it doens't exist yet, add it to the array.
good luck
Dizz
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 4th, 2005, 10:49 AM   #3
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Unhappy

How do I separate my text from the file into words?
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Mar 4th, 2005, 12:38 PM   #4
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
using python? no idea, but it's a common thing, search google
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Mar 4th, 2005, 3:56 PM   #5
Fred
Programmer
 
Fred's Avatar
 
Join Date: Feb 2005
Posts: 67
Rep Power: 4 Fred is on a distinguished road
I can't give you an exact code, but you can check each letter for a space, and the slice the text from space to space and save it in a list or dictionary...
How I do not exactely know, but something like this might work:
list = []

for x text:
   if x == ' ':
      list.append[:#till the space](There should be some way, to figure out which index it has...)
      text.del[:space+1] (so that the counting restarts from zero in the next run of the loop)

Then you should have all the words in a list...
As soon as I get home, and have acces to a python, I will try and figure out a way... If you are faster then me, be sure to post it...
Hope this helped a bit
Fred is offline   Reply With Quote
Old Mar 4th, 2005, 5:23 PM   #6
Fred
Programmer
 
Fred's Avatar
 
Join Date: Feb 2005
Posts: 67
Rep Power: 4 Fred is on a distinguished road
Here the promised code:
import string
text = 'h h h'
l = []
for x in text:
    if x == ' ':
        space = text.index(' ')
        part = text[:space]
        l.append(part)
        part1 = text[:space + 1]
        string.join(string.split(text, part1), '') #NO space between '' !!!!

print l

It has one problem though:
It does not add the last word of the string to the list. As soon as I come up with a solution to that one, I will edit my post or post new...
hope this helps

Last edited by Fred; Mar 4th, 2005 at 5:26 PM.
Fred is offline   Reply With Quote
Old Mar 4th, 2005, 5:48 PM   #7
Fred
Programmer
 
Fred's Avatar
 
Join Date: Feb 2005
Posts: 67
Rep Power: 4 Fred is on a distinguished road
Well...
As always, when I am doing something, there is a better way:
text.split()

That does the job, and includes the last word...
All that work for nothing :mad:
Fred is offline   Reply With Quote
Old Mar 5th, 2005, 8:20 AM   #8
al1986
Newbie
 
Join Date: Feb 2005
Posts: 24
Rep Power: 0 al1986 is on a distinguished road
Couldn't you just use the "count" method of the string type?
e.g.

s = "h h h"
s.count("h")

(returns 3)
al1986 is offline   Reply With Quote
Old Mar 5th, 2005, 10:43 AM   #9
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

Quote:
Originally Posted by Fred
Here the promised code:
import string
text = 'h h h'
l = []
for x in text:
    if x == ' ':
        space = text.index(' ')
        part = text[:space]
        l.append(part)
        part1 = text[:space + 1]
        string.join(string.split(text, part1), '') #NO space between '' !!!!

print l


It has one problem though:
It does not add the last word of the string to the list. As soon as I come up with a solution to that one, I will edit my post or post new...
hope this helps
text = text + ' ' # append a space
will make it work
thanks
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Mar 5th, 2005, 10:48 AM   #10
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

Quote:
Originally Posted by Fred
Well...
As always, when I am doing something, there is a better way:
text.split()

That does the job, and includes the last word...
All that work for nothing :mad:
wordlist = str1.split()
works really great!!

Now I just sort the list and find the duplicate words.
Or does sombody have a smarter solution?

thanks again!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:05 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC