Languages
[Edit]
EN

JavaScript - Soundex algorithm implementation

22 points
Created by:
Root-ssh
174740

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: 

// ONLINE-RUNNER:browser;

const findStartingCode = word => {
  	return word[0].toUpperCase();
};

const findLetterCode = letter => {
	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 => {
  	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

See also

  1. JavaScript - check words similarity (fuzzy compare with bigrams)

  2. JavaScript - calculates Levenshtein distance between strings

References

  1. Soundex - Wikipedia

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - Soundex algorithm implementation

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join