Forked from netojoaobatista/String.prototype.soundex.js
Created
October 17, 2012 21:18
-
-
Save emersonsoares/3908250 to your computer and use it in GitHub Desktop.
Implementação do algorítimo SoundEX em Javascript
This file contains hidden or 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
/** | |
* 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/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment