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:
xxxxxxxxxx
1
const findStartingCode = (word: string): string => {
2
return word[0].toUpperCase();
3
};
4
5
const findLetterCode = (letter: string): string => {
6
switch (letter.toUpperCase()) {
7
case 'B':
8
case 'F':
9
case 'P':
10
case 'V':
11
return '1';
12
13
case 'C':
14
case 'G':
15
case 'J':
16
case 'K':
17
case 'Q':
18
case 'S':
19
case 'X':
20
case 'Z':
21
return '2';
22
23
case 'D':
24
case 'T':
25
return '3';
26
27
case 'L':
28
return '4';
29
30
case 'M':
31
case 'N':
32
return '5';
33
34
case 'R':
35
return '6';
36
37
default:
38
return null;
39
}
40
};
41
42
const calculateSoundexCode = (word: string): string => {
43
if (word) {
44
let wordCode = findStartingCode(word);
45
let lastCode = findLetterCode(wordCode); // wordCode contains one letter at the begining
46
for (let i = 1; i < word.length; ++i) {
47
const letterCode = findLetterCode(word[i]);
48
if (letterCode && letterCode != lastCode) {
49
wordCode += letterCode;
50
if (wordCode.length == 4) {
51
break;
52
}
53
}
54
lastCode = letterCode;
55
}
56
for (let i = wordCode.length; i < 4; ++i) {
57
wordCode += '0';
58
}
59
return wordCode;
60
}
61
return null;
62
};
63
64
65
// Usage example:
66
67
console.log(calculateSoundexCode('Robert')); // R163
68
console.log(calculateSoundexCode('Rupert')); // R163
69
console.log(calculateSoundexCode('Rubin')); // R150
70
console.log(calculateSoundexCode('Ashcraft')); // A226
71
console.log(calculateSoundexCode('Ashcroft')); // A226
72
console.log(calculateSoundexCode('Tymczak')); // T522
73
console.log(calculateSoundexCode('Pfister')); // P236
74
console.log(calculateSoundexCode('Honeyman')); // H555
75
Output:
xxxxxxxxxx
1
R163
2
R163
3
R150
4
A226
5
A226
6
T522
7
P236
8
H555