Skip to content

Instantly share code, notes, and snippets.

@netojoaobatista
Created March 22, 2012 18:00
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save netojoaobatista/2161104 to your computer and use it in GitHub Desktop.
Save netojoaobatista/2161104 to your computer and use it in GitHub Desktop.
Implementação do algorítimo SoundEX em Javascript
/**
* Implementação do algorítimo SoundEx em Javascript.
* @author João Batista Neto
*
* SoundEX é um algorítimo fonético para indexação de nomes pelo som segundo
* sua pronúncia. O algorítimo foi desenvolvido por Robert C. Russell e
* Margaret K. Odell e patenteado em 1918 e 1922.
* {@link http://en.wikipedia.org/wiki/Soundex}
*
* @return {String}
*/
String.prototype.soundex = function() {
var string = this.toUpperCase().replace(/[^A-Z]/g,"");
string = [
string.substr(0,1),
string.substr(1)
.replace(/A|E|H|I|O|U|W|Y/g,0)
.replace(/B|F|P|V/g,1)
.replace(/C|G|J|K|Q|S|X|Z/g,2)
.replace(/D|T/g,3)
.replace(/L/g,4)
.replace(/M|N/g,5)
.replace(/R/g,6)
.replace(/1{2}|2{2}|3{2}|4{2}|5{2}|6{2}/g,"")
.replace(/0/g,"")
].join("").substr(0,4);
return string+
(string.length==4?"":(new Array(5-string.length)).join("0"));
};
var product = "Laptop Toshiba";
var query = "lapitopi tochiba"; //¬¬
product === query; //false
product.soundex()===query.soundex(); //true \o/
@suissa
Copy link

suissa commented Mar 25, 2012

Nossa simples e útil!

@felquis
Copy link

felquis commented Mar 25, 2012

Muito bom, parece ótimo para identificar palavrões, e muito mais.

@romuloctba
Copy link

lindo <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment