EN
TypeScript - Soundex algorithm implementation
0
points
In this short article, we would like to show how to implement the Soundex algorithm in TypeScript.
Below algorithm calculates Soundex code for indicated string.
Soundex codes can be used to:
- check words similarity (as an alternative: fuzzy comparison with bigrams),
- build fuzzy searching in databases,
- implement fuzzy map data structure (e.g. FuzzyHashMap).
Note: that algorithm works well for English words.
Example implementation:
const findStartingCode = (word: string): string => {
return word[0].toUpperCase();
};
const findLetterCode = (letter: string): string => {
switch (letter.toUpperCase()) {
case 'B':
case 'F':
case 'P':
case 'V':
return '1';
case 'C':
case 'G':
case 'J':
case 'K':
case 'Q':
case 'S':
case 'X':
case 'Z':
return '2';
case 'D':
case 'T':
return '3';
case 'L':
return '4';
case 'M':
case 'N':
return '5';
case 'R':
return '6';
default:
return null;
}
};
const calculateSoundexCode = (word: string): string => {
if (word) {
let wordCode = findStartingCode(word);
let lastCode = findLetterCode(wordCode); // wordCode contains one letter at the begining
for (let i = 1; i < word.length; ++i) {
const letterCode = findLetterCode(word[i]);
if (letterCode && letterCode != lastCode) {
wordCode += letterCode;
if (wordCode.length == 4) {
break;
}
}
lastCode = letterCode;
}
for (let i = wordCode.length; i < 4; ++i) {
wordCode += '0';
}
return wordCode;
}
return null;
};
// Usage example:
console.log(calculateSoundexCode('Robert')); // R163
console.log(calculateSoundexCode('Rupert')); // R163
console.log(calculateSoundexCode('Rubin')); // R150
console.log(calculateSoundexCode('Ashcraft')); // A226
console.log(calculateSoundexCode('Ashcroft')); // A226
console.log(calculateSoundexCode('Tymczak')); // T522
console.log(calculateSoundexCode('Pfister')); // P236
console.log(calculateSoundexCode('Honeyman')); // H555
Output:
R163
R163
R150
A226
A226
T522
P236
H555