Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Tests & implementation of Jaro-Winkler distance.

  • Loading branch information...
commit 1afb214556df7d47c0f551500c4e129908ed6f60 1 parent 815276f
Brian J Brennan authored December 11, 2010

Showing 2 changed files with 33 additions and 0 deletions. Show diff stats Hide diff stats

  1. 16  distance.js
  2. 17  test.js
16  distance.js
@@ -65,5 +65,21 @@ var jaro = function(str1, str2){
65 65
   return (1/3) * (matches/len1 + matches/len2 + (matches - transpositions)/matches);
66 66
 };
67 67
 
  68
+var jarowinkler = function(str1, str2, weight){
  69
+  // do not exceed 0.25 or distance can exceed 1
  70
+  var WEIGHT = weight ? (weight > 0.25 ? 0.25 : weight) : 0.1,
  71
+      prefix = 0,
  72
+      max = Math.max(str1.length, str2.length),
  73
+      jarodist = jaro(str1, str2);
  74
+  
  75
+  for (var i=0; i < max && prefix <= 4; i++) {
  76
+    if (str1[i] == str2[i]) { prefix++; }
  77
+    else { break; }
  78
+  }
  79
+  return jarodist + (prefix * WEIGHT * (1 - jarodist));
  80
+};
  81
+
68 82
 exports['hamming'] = hamming;
69 83
 exports['jaro'] = jaro;
  84
+exports['jarowinkler'] = jarowinkler;
  85
+
17  test.js
@@ -11,3 +11,20 @@ assert.equal(dist.jaro('dicksonx', 'dixon'), (1/3)*(4/5+4/8+1));
11 11
 assert.equal(dist.jaro('martha', 'marhta'), (1/3)*(1+1+5/6));
12 12
 assert.equal(dist.jaro('duane', 'dwayne'), (1/3)*(4/6+4/5+1));
13 13
 assert.equal(dist.jaro('jones', 'johnson'), (1/3)*(4/5+4/7+1));
  14
+
  15
+// testing jarowinkler
  16
+(function(){
  17
+  var f = function(distance, prefix) {
  18
+    return distance + (prefix * 0.1 * (1 - distance));
  19
+  };
  20
+  
  21
+  var test = function(str1, str2, prefix){
  22
+    var d = dist.jaro(str1, str2);
  23
+    assert.equal(dist.jarowinkler(str1, str2), f(d,prefix));
  24
+  };
  25
+  
  26
+  test('dicksonx', 'dixon', 2);
  27
+  test('martha', 'marhta', 3);
  28
+  test('duane', 'dwayne', 1);
  29
+  test('jones', 'johnson', 2);
  30
+})();

0 notes on commit 1afb214

Please sign in to comment.
Something went wrong with that request. Please try again.