Skip to content
Browse files

Implemented Jaro algorithm!

  • Loading branch information...
1 parent 23f6eba commit 815276fb4590a82a1fe5c026bbc8b6d4966e9b07 @brianloveswords committed Dec 11, 2010
Showing with 50 additions and 0 deletions.
  1. +43 −0 distance.js
  2. +7 −0 test.js
View
43 distance.js
@@ -23,4 +23,47 @@ var hamming = function(str1, str2){
return dist;
};
+var jaro = function(str1, str2){
+ var len1 = str1.length,
+ len2 = str2.length,
+ max = Math.max(len1, len2),
+ frame = Math.floor(max/2 - 1),
+ match_string1 = '',
+ match_string2 = '',
+ matches = 0,
+ transpositions = 0;
+
+ var get_match_string = function(string1, string2) {
+ var result = '';
+ for (var i=0; i < string1.length; i++) {
+ var chr1 = string1[i],
+ chr2 = string2[i];
+
+ if (chr1 == chr2) {
+ result += chr1;
+ }
+
+ else {
+ for (var j=-(frame); j <= frame; j++) {
+ var pos = i + j;
+ if (pos < 0) { continue; }
+ if (chr1 == string2[pos]) {
+ result += chr1;
+ }
+ }
+ }
+ }
+ return result;
+ };
+
+ match_string1 = get_match_string(str1, str2);
+ match_string2 = get_match_string(str2, str1);
+
+ matches = Math.min(match_string1.length, match_string2.length);
+ transpositions = hamming(match_string1, match_string2)/2;
+
+ return (1/3) * (matches/len1 + matches/len2 + (matches - transpositions)/matches);
+};
+
exports['hamming'] = hamming;
+exports['jaro'] = jaro;
View
7 test.js
@@ -4,3 +4,10 @@ var dist = require("./distance");
assert.equal(dist.hamming('awesome', 'aewsome'), 2);
assert.equal(dist.hamming('a', 'ba'), 2);
assert.equal(dist.hamming('cab', 'abra'), 4);
+
+// tests shameleslly ripped from the wikipedia article
+assert.equal(dist.jaro('great', 'great'), 1);
+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));

0 comments on commit 815276f

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