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.
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
- COMMAND
- CMMND
- CMND
- M = 5, N = 5, D = 3
- C553
Snippet
code_language.skript:
function soundex(a: text) :: text:
do [set {_text} to "%convert string {_a} to uppercase%"]->[set {_first} to first character of {_text}]
replace all "a", "e", "i", "o", "u", "y" and " " with "" in {_text}
if first character of {_text} is not the first character of {_a}:
set {_text} to concatenate {_first} and {_text}
set {_text::*} to {_text} split at ""
loop {_text::*}:
set {_v} to (loop-index parsed as number + 1)
if "%loop-index%" is "1":
remove loop-value from {_text::*}
else:
if loop-value is "%{_text::%{_v}%}%":
remove loop-value from {_text::*}
else:
if loop-value is "B" or "F" or "P" or "V":
add "1" to {_s::*}
else if loop-value is "C" or "G" or "J" or "K" or "Q" or "S" or "X" or "Z":
add "2" to {_s::*}
else if loop-value is "D" or "T":
add "3" to {_s::*}
else if loop-value is "L":
add "4" to {_s::*}
else if loop-value is "M" or "N":
add "5" to {_s::*}
else if loop-value is "R":
add "6" to {_s::*}
else if loop-value is "H" or "W":
add "*" to {_s::*}
loop {_s::*}:
do [set {_v} to (loop-index parsed as number + 1)]->[set {_v2} to (loop-index parsed as number + 2)]
if {_s::*} is "*":
return "%{_first}%000"
else if loop-value is "%{_s::%{_v}%}%" and "%{_s::%{_v2}%}%":
do [remove {_s::%{_v}%} from {_s::*}]->[remove {_s::%{_v2}%} from {_s::*}]
else if loop-value is "%{_s::%{_v2}%}%":
if "%{_s::%{_v}%}%" is "*":
remove {_s::%{_v2}%} from {_s::*}
else if loop-value is "*":
remove loop-value from {_s::*}
else:
add 1 to {_t}
set {_final} to concatenate {_final} and loop-value
if size of {_s::*} is 1:
return "%{_first}%%{_final}%00"
if size of {_s::*} is 2:
if {_t} is 2:
return "%{_first}%%{_final}%0"
else:
if {_t} is 3:
return "%{_first}%%{_final}%"
Usage
soundex(<text>) : returns text
Returns the Soundex code for the word you entered.
Exemple
This exemple corrects the command /remove if you wrote it wrong
code_language.skript:
command /money <text>:[/INDENT]
trigger:
if arg 1 is not "remove":
if soundex("remove") is soundex(arg 1):
send "The command is /money remove and not /remove %arg 1% :)"