62 lines
1.2 KiB
Text
62 lines
1.2 KiB
Text
class SoundEx {
|
|
function : Main(args : String[]) ~ Nil {
|
|
SoundEx("Soundex")->PrintLine();
|
|
SoundEx("Example")->PrintLine();
|
|
SoundEx("Sownteks")->PrintLine();
|
|
SoundEx("Ekzampul")->PrintLine();
|
|
}
|
|
|
|
function : SoundEx(s : String) ~ String {
|
|
input := s->ToUpper()->Get(0);
|
|
code := input->ToString();
|
|
previous := GetCode(input);
|
|
|
|
for(i := 1; i < s->Size(); i += 1;) {
|
|
current := GetCode(s->ToUpper()->Get(i));
|
|
if(current->Size() > 0 & <>current->Equals(previous)) {
|
|
code += current;
|
|
};
|
|
previous := current;
|
|
};
|
|
|
|
soundex := String->New(code);
|
|
soundex += "0000";
|
|
return soundex->SubString(4);
|
|
}
|
|
|
|
function : GetCode(c : Char) ~ String {
|
|
select(c) {
|
|
label 'B': label 'F':
|
|
label 'P': label 'V': {
|
|
return "1";
|
|
}
|
|
|
|
label 'C': label 'G':
|
|
label 'J': label 'K':
|
|
label 'Q': label 'S':
|
|
label 'X': label 'Z': {
|
|
return "2";
|
|
}
|
|
|
|
label 'D': label 'T': {
|
|
return "3";
|
|
}
|
|
|
|
label 'L': {
|
|
return "4";
|
|
}
|
|
|
|
label 'M': label 'N': {
|
|
return "5";
|
|
}
|
|
|
|
label 'R': {
|
|
return "6";
|
|
}
|
|
|
|
other: {
|
|
return "";
|
|
}
|
|
};
|
|
}
|
|
}
|