Last active
August 18, 2018 21:50
-
-
Save luthfianto/9668817 to your computer and use it in GitHub Desktop.
An implementation of Soundex algorithm in JavaScript. (Introduction to Information Retrieval)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var isAlpha = function(s) { return /^[a-zA-Z]+$/.test(s) } | |
var soundex = function(s) { | |
if(s==='') | |
return '0000' | |
const | |
head = s[0].toLowerCase(), | |
tail = s.substr(1).toLowerCase() | |
if(!isAlpha(head)) | |
return soundex(tail) | |
const | |
dict= { | |
a: '', e: '', i: '', o: '', u: '', y:'', h:'', w:'', | |
b: 1, f: 1, p: 1, v: 1, | |
c: 2, g: 2, j: 2, k: 2, q: 2, s: 2, x: 2, z: 2, | |
d: 3, t: 3, | |
l: 4, | |
m: 5, n: 5, | |
r: 6 | |
}, | |
t=tail.split(''), | |
r= head + t | |
.map(function (v, i, array) { return dict[v] }) | |
.filter(function (v, i, array) {return i===0? v !== dict[head] : v !== array[i-1]}) | |
.join('') | |
return (r.toUpperCase() + '000').slice(0, 4) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment