soundex

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

soundex

Post by mwieder » Tue Sep 14, 2021 1:41 am

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

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”