Skip to content

Commit

Permalink
Tests & implementation of Jaro-Winkler distance.
Browse files Browse the repository at this point in the history
  • Loading branch information
brianloveswords committed Dec 12, 2010
1 parent 815276f commit 1afb214
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions distance.js
Expand Up @@ -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;

17 changes: 17 additions & 0 deletions test.js
Expand Up @@ -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.