Permalink
Browse files

Tests & implementation of Jaro-Winkler distance.

  • Loading branch information...
1 parent 815276f commit 1afb214556df7d47c0f551500c4e129908ed6f60 @brianloveswords committed Dec 12, 2010
Showing with 33 additions and 0 deletions.
  1. +16 −0 distance.js
  2. +17 −0 test.js
View
@@ -65,5 +65,21 @@ var jaro = function(str1, str2){
return (1/3) * (matches/len1 + matches/len2 + (matches - transpositions)/matches);
};
+var jarowinkler = function(str1, str2, weight){
+ // do not exceed 0.25 or distance can exceed 1
+ var WEIGHT = weight ? (weight > 0.25 ? 0.25 : weight) : 0.1,
+ prefix = 0,
+ max = Math.max(str1.length, str2.length),
+ jarodist = jaro(str1, str2);
+
+ for (var i=0; i < max && prefix <= 4; i++) {
+ if (str1[i] == str2[i]) { prefix++; }
+ else { break; }
+ }
+ return jarodist + (prefix * WEIGHT * (1 - jarodist));
+};
+
exports['hamming'] = hamming;
exports['jaro'] = jaro;
+exports['jarowinkler'] = jarowinkler;
+
View
@@ -11,3 +11,20 @@ assert.equal(dist.jaro('dicksonx', 'dixon'), (1/3)*(4/5+4/8+1));
assert.equal(dist.jaro('martha', 'marhta'), (1/3)*(1+1+5/6));
assert.equal(dist.jaro('duane', 'dwayne'), (1/3)*(4/6+4/5+1));
assert.equal(dist.jaro('jones', 'johnson'), (1/3)*(4/5+4/7+1));
+
+// testing jarowinkler
+(function(){
+ var f = function(distance, prefix) {
+ return distance + (prefix * 0.1 * (1 - distance));
+ };
+
+ var test = function(str1, str2, prefix){
+ var d = dist.jaro(str1, str2);
+ assert.equal(dist.jarowinkler(str1, str2), f(d,prefix));
+ };
+
+ test('dicksonx', 'dixon', 2);
+ test('martha', 'marhta', 3);
+ test('duane', 'dwayne', 1);
+ test('jones', 'johnson', 2);
+})();

0 comments on commit 1afb214

Please sign in to comment.