How to resolve the algorithm Soundex step by step in the F# programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Soundex step by step in the F# 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 F# programming language

Source code in the fsharp programming language

module Soundex

let soundex (s : string) = 
    let code c = 
        match c with
        | 'B' | 'F' | 'P' | 'V' -> Some('1')
        | 'C' | 'G' | 'J' | 'K' | 'Q' | 'S' | 'X' | 'Z' -> Some('2')
        | 'D' | 'T' -> Some('3')
        | 'L' -> Some('4')
        | 'M' | 'N' -> Some('5')
        | 'R' -> Some('6')
        | _ -> None

    let rec p l = 
        match l with
        | [] -> []
        | x :: y :: tail when (code x) = (code y) -> (p (y :: tail))
        | x :: 'W' :: y :: tail when (code x) = (code y) -> (p (y :: tail))
        | x :: 'H' :: y :: tail when (code x) = (code y) -> (p (y :: tail))
        | x :: tail -> (code x) :: (p tail)

    let chars = 
        match (p (s.ToUpper() |> List.ofSeq)) with
        | [] -> ""
        | head :: tail -> new string((s.[0] :: (tail |> List.filter (fun x -> x.IsSome) |> List.map (fun x -> x.Value))) |> List.toArray)
    chars.PadRight(4, '0').Substring(0, 4)

let test (input, se) = 
    printfn "%12s\t%s\t%s" input se (soundex input)

let testCases = [|
    ("Ashcraft", "A261"); ("Ashcroft", "A261"); ("Burroughs", "B620"); ("Burrows", "B620");
    ("Ekzampul", "E251"); ("Example", "E251"); ("Ellery", "E460"); ("Euler", "E460"); 
    ("Ghosh", "G200"); ("Gauss", "G200"); ("Gutierrez", "G362"); ("Heilbronn", "H416"); 
    ("Hilbert", "H416"); ("Jackson", "J250"); ("Kant", "K530"); ("Knuth", "K530");
    ("Lee", "L000"); ("Lukasiewicz", "L222"); ("Lissajous", "L222"); ("Ladd", "L300");
    ("Lloyd", "L300"); ("Moses", "M220"); ("O'Hara", "O600"); ("Pfister", "P236");
    ("Rubin", "R150"); ("Robert", "R163"); ("Rupert", "R163"); ("Soundex", "S532");
    ("Sownteks", "S532"); ("Tymczak", "T522"); ("VanDeusen", "V532"); ("Washington", "W252");
    ("Wheaton", "W350");
    |]

[<EntryPoint>]
let main args = 
    testCases |> Array.sortBy (fun (_, x) -> x) |> Array.iter test
    System.Console.ReadLine() |> ignore

    0


  

You may also check:How to resolve the algorithm Polyspiral step by step in the SVG programming language
You may also check:How to resolve the algorithm Array length step by step in the RPL programming language
You may also check:How to resolve the algorithm Search a list of records step by step in the Rust programming language
You may also check:How to resolve the algorithm JSON step by step in the Ruby programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the 360 Assembly programming language