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