Search and Replace

Ken Ray kray at sonsothunder.com
Sat Oct 18 00:34:15 EDT 2003


> 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?

Dan,

Here's a replacement function - it's a little "messy" (because I have to
wrap 'whatText' in spaces at the beginning and strip them at the end),
but it works:

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
  put " " & whatText & " " into whatText
  repeat forever
    get
matchChunk(whatText,"\W+((B|b)oy|(h|H)(e|im|is|imself|e's)|(g|G)uy)\W+",
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
  delete char 1 of whatText
  delete char -1 of whatText
  return whatText
end searchAndReplaceMaleToFemale

If I find a more elegant solution, I'll let you know...

Ken Ray
Sons of Thunder Software
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/ 




More information about the use-livecode mailing list