Skip to content

Instantly share code, notes, and snippets.

@luthfianto
Last active August 18, 2018 21:50
Show Gist options
  • Save luthfianto/9668817 to your computer and use it in GitHub Desktop.
Save luthfianto/9668817 to your computer and use it in GitHub Desktop.
An implementation of Soundex algorithm in JavaScript. (Introduction to Information Retrieval)
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