Page 1 of 1
Display number for found word
Posted: Mon Feb 28, 2011 9:28 am
by urbaud
I'm trying to figure out how to display the number of the word found. For example, if I have the sentence: The quick brown fox jumped over the lazy dog, and I use the Find command to locate the word 'over', how do I then have the handler automatically display the number of the word in another field? The sentence has 9 words in it, so the word 'over' is word number 6. What is the code to display the number 6 in another field.
Code: Select all
on mouseUp
ask "Type a word to find"
put it into tWord
find word tWord in fld "t1"
get it
put it into tFound --found word
put tFound into fld "t2" --found word displayed in field "t2"
put cr & the number of words in fld "t1" after fld "t2" --number of words in "t1"
put cr & ...... ?????..... after fld "t2" --number of the found word
end mouseUp
Thanks in advance for any help.
Re: Display number for found word
Posted: Mon Feb 28, 2011 9:55 am
by BvG
put the foundChunk
or
put word 2 of the foundChunk
Re: Display number for found word
Posted: Mon Feb 28, 2011 9:03 pm
by urbaud
Hi BvG,
Thanks very much for the help. Is there a way to just show the number of the word in the string? As you know, 'foundChunk' identifies the character numbers of the word, ie. char 28 to 33 for example and 'word 2 of the foundChunk' gives the character number for the first character of the word, ie. 28. If there are 9 words in the string and I find the 5th word, I'd like to display the number 5, as the fifth word.
Actually, my true wish is to display the following, for example:
The quick brown fox jumped over the lazy dog.
1 2 3 4 5 6 7 8 9
Note: the numbers 1 to 9 should correspond to the 9 words above them. This reply field won't display the numbers directly under each word, or I don't know how to make it do that, so just imagine the numbers under each word.
Any thoughts? Thanks in advance.
Re: Display number for found word
Posted: Mon Feb 28, 2011 9:23 pm
by Dixie
Urbaud...
Make a fld and a button... put 'The quick brown fox jumped over the lazy dog' into fld 1. Put the following into the button
Code: Select all
on mouseUp
ask "Enter the word to find.."
if it is not empty then
put wordOffset (it, fld 1)
end if
end mouseUp
The number of the word's position will be placed in the message box. ... look at 'wordOffset' in the dictionary..
be well
Dixie
Re: Display number for found word
Posted: Mon Feb 28, 2011 9:33 pm
by mwieder
Actually, you'll have a problem with the word "the", since it appears in the string twice (word positions 1 and 7). You'll have to decide what you want to do with that situation.
Re: Display number for found word
Posted: Mon Feb 28, 2011 9:45 pm
by BvG
Oh I am sorry, you asked for words and i gave you chars... useless

Re: Display number for found word
Posted: Mon Feb 28, 2011 10:10 pm
by Dixie
mweider...
be well
Dixie
Re: Display number for found word
Posted: Mon Feb 28, 2011 11:51 pm
by urbaud
Hi Dixie, Mwieder and BVG,
Dixie, you came through again! Thanks so much for the help. I don't know anything about wordOffset, lineOffset or itemOffset. I will have to start to explore what they are all about.
Mwieder, any suggestions about what to do with "the", double words in a string and displaying their respective word numbers? And BvG, thanks again for the help. Actually, I don't know anything about FoundChunk either, so you gave me some info to explore. So, thanks to all of you for your quick response, help and suggestions.
Re: Display number for found word
Posted: Tue Mar 01, 2011 2:12 am
by mwieder
No suggestions, it depends on what you want to do.
If you're going through in sequence, note that the ...offset functions allow you to specify a third parameter for words to skip. So when you find a word, move on to the next one that way. That would give you the numeric sequence you specified. But not knowing what you want to do with that sequence, that may not give you the answer you're looking for.
As Dixie said,
Code: Select all
on mouseUp
ask "Enter the word to find.."
if it is not empty then
put wordOffset (it, fld 1)
end if
end mouseUp
will get you one answer, with "the" giving you 1. And that may be what you're looking for.
But if you want 7 for the second occurrence of "the" then try
Code: Select all
on mouseUp
local tOffset
local tFound
ask "Enter the word to find.."
if it is not empty then
repeat
put wordoffset (it, fld 1, tOffset) into tFound
if tFound is 0 then
exit repeat
else
add tFound to tOffset
put tOffset & space after field 2
end if
end repeat
end if
end mouseUp
Re: Display number for found word
Posted: Thu Mar 03, 2011 5:21 pm
by urbaud
Hi mwieder,
Thanks so much for the code. Now I have to sit down and figure out how it works. Again, thanks for helping.
Re: Display number for found word
Posted: Thu Mar 03, 2011 6:34 pm
by mwieder
NP. The key to this is that there's an optional third parameter to the <...>offset functions that specifies how many chunks to skip before searching. In this case you're looking at word chunks, so we start by skipping zero words to find the first occurrence. Once we've found an instance of the word we're looking for we save its offset somewhere, then make a new skip parameter and continue searching. Since I don't know if this is what you're after, I just took the result of all the above and stored it in a second field, and you can take it from there.
Re: Display number for found word
Posted: Sat Mar 05, 2011 1:46 am
by urbaud
Hi mwieder,
Thanks for the note on the 3rd parameter of wordoffset. What I find a bit confusing is line 11. If we use the sentence: the quick brown fox jumped over the lazy dog, and search for say, jumped, the wordoffset function returns the number 5, which is then put into tFound. However, when tFound is added to tOffset it only adds 1 on each iteration of the repeat loop, as far as I can determine. I've been trying to write code within your code to show me how the tOffset and tFound variables change values with each loop of the repeat loop, but with little success. I'd like to see the values change as wordoffset cycles through the sentence; to see the values change dynamically. I was thinking that if I could see the values change it would help me understand, but up to this point I haven't been successful.
I have been placing the following code: put "tOffset: " & tOffset & tab & "tFound: " & tFound & cr after fld "l1" between various lines to see the changing values, but I find I'm still not grasping your code. And I should be, as it is not particularly complicated code. If you have any ideas, I'm all ears.
Thanks, Dan
Code: Select all
1. on mouseUp
2. local tOffset
3. local tFound
4. ask "Enter the word to find.."
5. if it is not empty then
6. repeat
7. put wordoffset (it, fld "t1", tOffset) into tFound
8. if tFound is 0 then
9. exit repeat
10. else
11. add tFound to tOffset
12. put tOffset & space after field "t2"
13. end if
14. end repeat
15. end if
16. end mouseUp
Re: Display number for found word
Posted: Sat Mar 05, 2011 3:05 am
by mwieder
Ah. Ok. I can see where that might be confusing. Let's take a look at the section
Code: Select all
8. if tFound is 0 then
9. exit repeat
10. else
11. add tFound to tOffset
12. put tOffset & space after field "t2"
13. end if
First of all, if the word is not found (or there are no more occurrences of it) then the wordOffset function will return zero. That's line 8, and in that case we've found all the instances of the word in the string and we're done. So line 9 exits and we go on to do something significant with our results in hand.
If we got something other than a zero then we've encountered a new instance of the word. Parameter 3 of the wordOffset function is an offset from the starting point of our search (think of it as the number of words to ignore before we start searching). The result of the wordOffset function is relative to the offset parameter. So in our case if we say
put wordoffset("lazy", "the quick brown fox jumped over the lazy dog the other day")
or
put wordoffset("lazy", "the quick brown fox jumped over the lazy dog the other day", 0)
we'll get an answer of 8. But if we say
put wordoffset("lazy", "the quick brown fox jumped over the lazy dog the other day", 3)
we'll get an answer of 4 (the fourthword after word 3). So we need to add the result (tFound) to the offset in order to get the absolute offset from the beginning of the string and determine that we got a hit on the seventh word. If we didn't add it we'd get
put wordoffset("the", "the quick brown fox jumped over the lazy dog the other day". 0) = 1
then we put 1 into the offset and get
put wordoffset("the", "the quick brown fox jumped over the lazy dog the other day". 1) = 6
and if we don't add the offset and just use the result we get
put wordoffset("the", "the quick brown fox jumped over the lazy dog the other day". 6) = 1
and we'll keep going around and never get the tenth instance.
Hope this makes sense - it's been a long day/week/whatever. I need me some weekend.
Re: Display number for found word
Posted: Sun Mar 06, 2011 5:05 pm
by Dixie
Dan...
Another way to find multiple occurrences of the same word in your string might be just to run through the whole lot...

So, assuming that the string is in 'fld 1' ... 'The quick brown fox jumped over the lazy dog the other day' ... then say, using 'the' as the word to find would return 1,7,10
Code: Select all
on mouseUp
ask "The word to find ?"
if it is empty then exit mouseUp
put fld 1 into theWords
put theFoundWords (it, theWords)
end mouseUp
function theFoundWords theWord, theWords
put 1 into count
repeat for (the number of words of theWords) times
if theWord= word count of theWords then put count & comma after theWordPositions
add 1 to count
end repeat
if theWordPositions is empty then
put 0 into theWordPositions
else
delete the last char of theWordPositions
end if
return theWordPositions
end theFoundWords
be well,
Dixie
Re: Display number for found word
Posted: Wed Mar 09, 2011 8:43 am
by urbaud
Hi Dixie,
Thank you so much for the code. You and everyone else have been so helpful. I will now study your code to try to understand it as I am doing with mweider's code. I'm fairly new to Rev and am trying to learn it by posing problems for myself to solve. Thank you for your help, I truly appreciate it.
Dan