Page 1 of 1

soundex

Posted: Tue Sep 14, 2021 1:41 am
by mwieder
Not really sure where to post this thread so...
I had need for an implementation of the soundex algorithm for fuzzy text matching, so I wrote this.

Code: Select all

function soundex pText
   local tSoundexCode

   put char 1 of pText into tSoundexCode
   delete char 1 of pText
   replaceVowelsIn pText
   put soundexCodeOf(char 1 of pText) after tSoundexCode
   put soundexCodeOf(char 2 of pText) after tSoundexCode
   put soundexCodeOf(char 3 of pText) after tSoundexCode
   return tSoundexCode
end soundex

private command replaceVowelsIn @pText
   replace "a" with empty in pText
   replace "e" with empty in pText
   replace "i" with empty in pText
   replace "o" with empty in pText
   replace "u" with empty in pText
   replace "y" with empty in pText
end replaceVowelsIn

private function soundexCodeOf pChar
   local tCode
   
   put 0 into tCode
   switch pChar
      case "B"
      case "P"
      case "F"
      case "V"
         put 1 into tCode
         break
      case "C"
      case "S"
      case "G"
      case "J"
      case "K"
      case "Q"
      case "X"
      case "Z"
         put 2 into tCode
         break
      case "D"
      case "T"
         put 3 into tCode
         break
      case "L"
         put 4 into tCode
         break
      case "M"
      case "N"
         put 5 into tCode
         break
      case "R"
         put 6 into tCode
         break
      default
   end switch
   return tCode
end soundexCodeOf