How to resolve the algorithm Soundex step by step in the PureBasic programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Soundex step by step in the PureBasic programming language

Table of Contents

Problem Statement

Soundex is an algorithm for creating indices for words based on their pronunciation.

The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling   (from the   soundex   Wikipedia article). There is a major issue in many of the implementations concerning the separation of two consonants that have the same soundex code! According to the official Rules [[1]]. So check for instance if Ashcraft is coded to A-261.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Soundex step by step in the PureBasic programming language

Source code in the purebasic programming language

Procedure.s getCode(c.s)
    Protected  getCode.s = ""
     
    If FindString("BFPV", c ,1)     : getCode = "1" : EndIf
    If FindString("CGJKQSXZ", c ,1) : getCode = "2" : EndIf 
    If FindString("DT", c ,1)       : getCode = "3" : EndIf
    If "L" = c                      : getCode = "4" : EndIf
    If FindString("MN", c ,1)       : getCode = "5" : EndIf
    If "R" = c                      : getCode = "6" : EndIf 
    If FindString("HW", c ,1)       : getCode = "." : EndIf
    ProcedureReturn getCode
EndProcedure
 
Procedure.s soundex(word.s)
    Protected.s previous.s = "" , code.s , current , soundex
    Protected.i i
    
    word = UCase(word)
    code = Mid(word,1,1)
    previous = getCode(Left(word, 1))
    For i = 2 To (Len(word) + 1)
        current = getCode(Mid(word, i, 1))
        If current = "." : Continue : EndIf
        If Len(current) > 0 And current <> previous
            code + current
        EndIf
        previous = current
        If Len(code) = 4
          Break
        EndIf  
    Next
    If Len(code) < 4 
        code = LSet(code, 4,"0")
    EndIf
    ProcedureReturn code
EndProcedure

OpenConsole()

PrintN (soundex("Lukasiewicz"))
PrintN("Press any key to exit"): Repeat: Until Inkey() <> ""

  

You may also check:How to resolve the algorithm Guess the number/With feedback (player) step by step in the Nim programming language
You may also check:How to resolve the algorithm Loops/N plus one half step by step in the PHP programming language
You may also check:How to resolve the algorithm Rename a file step by step in the Racket programming language
You may also check:How to resolve the algorithm Set consolidation step by step in the Quackery programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n1, continued fraction n2) step by step in the Scheme programming language