Page 1 of 1

Finding words whilst ignoring apostrophes

Posted: Thu Feb 23, 2017 2:58 pm
by PoLyGLoT
Hello,

Is there any way to search for items and ignore apostrophes?

I'm attempting to do this:

Code: Select all

if "USA"  is among the items of line z of goodDemo then
add 1 to numUSA
end if
Sometimes, USA will be written as "USA" in goodDemo, and it will not detect it.

Any workaround here?
Thank you!

Re: Finding words whilst ignoring apostrophes

Posted: Thu Feb 23, 2017 3:34 pm
by Klaus
Hi PoLyGLoT,

use "truewords" instead of "items"!
Will work with USA, "USA" and 'USA', and other countries, too, of course! 8)

Code: Select all

...
if "USA"  is among the truewords of line z of goodDemo then
  add 1 to numUSA
end if
...
Tested and works! :D


Best

Klaus

Re: Finding words whilst ignoring apostrophes

Posted: Thu Feb 23, 2017 3:39 pm
by Lagi Pittas
Hi

I'm sure there is a more elegant way and shorter but this is clear and easy to change. It would only matter speed wise if you had (many?) thousands of lines I'm sure.

Code: Select all

on mouseup
  repeat with lnX = 1 to the number of lines in field "lstGoodDemo"
      put ReplaceText(line lnX of field  "lstGoodDemo",quote,empty) into lcText
      if "USA"  is among the items of lcText then
         add 1 to lnNum
      end if
   end repeat
   
   put lnNum
end mouseup 

Re: Finding words whilst ignoring apostrophes

Posted: Thu Feb 23, 2017 3:50 pm
by PoLyGLoT
Thank you both for your responses.

The "truewords" solution works, but has 1 downside (at least for my data): It only seems to work on single words. For instance, this does not catch "United States of America":

Code: Select all

else if "United States of America" is among the truewords of line z of goodDemo then
add 1 to numUSA
However, this does work:

Code: Select all

put ReplaceText(line z of goodDemo,quote,empty) into line z of goodDemo
else if "United States of America" is among the items of line z of goodDemo then
add 1 to numUSA
Perhaps truewords only works on single words? Not sure, but I appreciate both your responses and glad I figured out a way to make it work! 8)

Re: Finding words whilst ignoring apostrophes

Posted: Thu Feb 23, 2017 3:57 pm
by Klaus
Yes, as its name suggests, it will only work with WORDS.
And that was your original question. :wink:

Maybe you better use "contains" or "offset" or something like that?

Re: Finding words whilst ignoring apostrophes

Posted: Thu Feb 23, 2017 4:13 pm
by PoLyGLoT
Klaus wrote:Yes, as its name suggests, it will only work with WORDS.
And that was your original question. :wink:

Maybe you better use "contains" or "offset" or something like that?
Yes! I had assumed the solution might apply to everything. :P

Given that I will probably never have thousands and thousands of lines, I can use the replaceText solution that was offered. :mrgreen:

Re: Finding words whilst ignoring apostrophes

Posted: Thu Feb 23, 2017 5:05 pm
by Thierry
PoLyGLoT wrote:
Yes! I had assumed the solution might apply to everything. :P
Given that I will probably never have thousands and thousands of lines,
I can use the replaceText solution that was offered. :mrgreen:
What about this one?

Code: Select all

  put "\b" & "USA" & "\b" into searchForUSA
  repeat for each line T in field 1
      if matchChunk( T, searchForUSA) then add 1 to n
   end repeat
Fast and ready for long text...
Could be improved a bit but as I don't know your data, I assume it's enough.

Thierry