Search and Replace

Dan Friedman dan at clearvisiontech.com
Fri Oct 17 14:49:43 EDT 2003


Hello all!

I'm having a problem with a function that Ken Ray helped me with.  I tried
to fix it myself, but I don't really understand the matchChunk function.

The goal is to pass searchAndReplaceMaleToFemale() whatText and return a
female version of whatText.  But it's not quite working correctly.  When I
pass:

he him himself his he's boy guy He Him Himself His He's Boy Guy

I get:

he her herself her she's girl gal He Him Himself His He's Boy Guy

There seems to be a problem if a replaceable word is the first word of a
sentence, and obviously, capitalization seems to be an issue.

Below is the searchAndReplaceMaleToFemale() function.  Any ideas?


function searchAndReplaceMaleToFemale whatText
  local tStart,tEnd

  put "he,him,himself,his,he's,boy,guy" into SearchWordsMale
  put "she,her,herself,her,she's,girl,gal" into SearchWordsFemale
  set the wholeMatches to true
  set the caseSensitive to false
  repeat forever
    get 
matchChunk(whatText,"[^A-Za-z0-9](boy|h(e|im|is|imself|e's)|guy)[^A-Za-z0-9]
",tStart,tEnd)
    if it is true then
      put char tStart to tEnd of whatText into tFoundChunk
      put (charToNum(toUpper(char 1 of tFoundChunk)) = charToNum(char 1 of
tFoundChunk)) into isUpper
      put itemOffset(tFoundChunk,SearchWordsMale) into tItem
      if tItem <> 0 then
        put item tItem of SearchWordsFemale into temp
        if isUpper then put toUpper(char 1 of temp) into char 1 of temp
        put temp into char tStart to tEnd of whatText
      else
        -- you decide... bail? error out? ignore?
      end if
    else
      exit repeat
    end if
  end repeat
  return whatText
end searchAndReplaceMaleToFemale



More information about the use-livecode mailing list