1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Soundex Algorithm

Discussion in 'Snippets' started by Polymeth, Apr 16, 2017.

  1. Polymeth

    Polymeth New Member

    Apr 16, 2017
    Likes Received:

    Soundex Algorithm

    Soundex algorithm is a phonectif algorithm that encode words into a little code. The goal is for homophones to be encoded to the same representation so that they can be matched despite minor differences in spelling.
    Thanks Wikipedia lol. Using it, you can "correct" a word, or check a command entered wrong.​

    To see how the algorithm works, check the Wikipedia page out there, it will show you all the steps and all the rules. The algorithm I made handles the 'exceptions' and 'rules' you can see on the wikipedia's page.​

    Basic exemple
    1. COMMAND
    2. CMMND
    3. CMND
    4. M = 5, N = 5, D = 3
    5. C553
    Do it with a missplelled "Command", for exemple "CAMAND", and it will results C553​

    Code (Skript):
    1. function soundex(a: text) :: text:
    2.     do [set {_text} to "%convert string {_a} to uppercase%"]->[set {_first} to first character of {_text}]
    3.     replace all "a", "e", "i", "o", "u", "y" and " " with "" in {_text}
    4.     if first character of {_text} is not the first character of {_a}:
    5.         set {_text} to concatenate {_first} and {_text}
    6.     set {_text::*} to {_text} split at ""
    8.     loop {_text::*}:
    9.         set {_v} to (loop-index parsed as number + 1)
    10.         if "%loop-index%" is "1":
    11.             remove loop-value from {_text::*}
    12.         else:
    13.             if loop-value is "%{_text::%{_v}%}%":
    14.                 remove loop-value from {_text::*}
    15.             else:
    16.                 if loop-value is "B" or "F" or "P" or "V":
    17.                     add "1" to {_s::*}
    18.                 else if loop-value is "C" or "G" or "J" or "K" or "Q" or "S" or "X" or "Z":
    19.                     add "2" to {_s::*}
    20.                 else if loop-value is "D" or "T":
    21.                     add "3" to {_s::*}
    22.                 else if loop-value is "L":
    23.                     add "4" to {_s::*}
    24.                 else if loop-value is "M" or "N":
    25.                     add "5" to {_s::*}
    26.                 else if loop-value is "R":
    27.                     add "6" to {_s::*}
    28.                 else if loop-value is "H" or "W":
    29.                     add "*" to {_s::*}
    31.     loop {_s::*}:
    32.         do [set {_v} to (loop-index parsed as number + 1)]->[set {_v2} to (loop-index parsed as number + 2)]
    33.         if {_s::*} is "*":
    34.             return "%{_first}%000"
    35.         else if loop-value is "%{_s::%{_v}%}%" and "%{_s::%{_v2}%}%":
    36.             do [remove {_s::%{_v}%} from {_s::*}]->[remove {_s::%{_v2}%} from {_s::*}]
    37.         else if loop-value is "%{_s::%{_v2}%}%":
    38.             if "%{_s::%{_v}%}%" is "*":
    39.                 remove {_s::%{_v2}%} from {_s::*}
    40.         else if loop-value is "*":
    41.             remove loop-value from {_s::*}
    42.         else:
    43.             add 1 to {_t}
    44.             set {_final} to concatenate {_final} and loop-value
    45.             if size of {_s::*} is 1:
    46.                 return "%{_first}%%{_final}%00"
    47.             if size of {_s::*} is 2:
    48.                 if {_t} is 2:
    49.                     return "%{_first}%%{_final}%0"
    50.             else:
    51.                 if {_t} is 3:
    52.                     return "%{_first}%%{_final}%"


    soundex(<text>) : returns text
    Returns the Soundex code for the word you entered.​


    This exemple corrects the command /remove if you wrote it wrong
    Code (Skript):
    1. command /money <text>:[/INDENT]
    2.     trigger:
    3.         if arg 1 is not "remove":
    4.             if soundex("remove") is soundex(arg 1):
    5.                 send "The command is /money remove and not /remove %arg 1% :)"

Share This Page