scripting challenge Contest ?

Brian Yennie briany at qldlearning.com
Thu Jul 5 15:21:37 EDT 2007


Hi Hugh,

I actually saw this thread on the Metacard list, but didn't have a  
moment to really think about it. I'm not sure I understand completely  
the "must handle all punctuation" part, but regardless it might get  
the ball rolling for me to post a simple approach and you can tell me  
the flaws =). This is only lightly tested, but maybe it's close? This  
basically tries to strip and normalize all punctuation first, and  
then search for a whole match.

function wholeFindIt sourceText, findThis
      ## remove punctuation from both source and chunk to find
      ## commas, colons, semicolons exclamations, periods, questions,  
new lines

     repeat for each char c in (";:.,!?"&return)
         replace c with space in sourceText
         replace c with space in findThis
     end repeat

      ## replace hyphens followed by spaces
      ## "I like you- are you single?" => "I like you are you single"
      ## "I like super-fast ultra-cars!" => unchanged

      replace ("-"&space) with space in sourceText
      replace ("-"&space) with space in findThis

      ## normalize multiple spaces down to one

      repeat while (offset(space&space, sourceText) > 0)
           replace (space&space) with space in sourceText
      end repeat

      repeat while (offset(space&space, findThis) > 0)
           replace (space&space) with space in findThis
      end repeat

      ## pad everything with spaces to get whole word matches

      if (char 1 of sourceText is not space) then put space before  
sourceText
      if (char -1 of sourceText is not space) then put space after  
sourceText

      if (char 1 of findThis is not space) then put space before  
findThis
      if (char -1 of findThis is not space) then put space after  
findThis

      ## search!
      return (offset(findThis, sourceText) > 0)

end wholeFindIt




> There is a wee challenge I posted to the metacard list for a   
> function that
> returns whether a whole text string is in a  variable (must handle all
> punctuation and ends of line etc), along the  lines of...
>
> put "This is a  test-piece." into sourceText (note the  punctuation  
> and
> hyphen)
> whole("a test",sourceText)   =>  FALSE
> whole("is a test-piece",sourceText)  => TRUE
> whole("is  a test-piece.",sourceText)  => TRUE
>
> There have been some good  attempts but it has yet to be solved  
> completely.
> Perhaps someone on  this list is up for it.
>
> /H




More information about the Use-livecode mailing list