Skip to content

Commit

Permalink
dice_coefficient supports single character strings
Browse files Browse the repository at this point in the history
  • Loading branch information
psnider committed Mar 24, 2015
1 parent df64740 commit f07739a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
34 changes: 22 additions & 12 deletions lib/natural/distance/dice_coefficient.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,32 @@ var sanitize = function (str) {

// Compare two strings, and spit out a number from 0-1
var compare = function (str1, str2) {
var pairs1 = wordLetterPairs(sanitize(str1));
var pairs2 = wordLetterPairs(sanitize(str2));
var sanitized_str1 = sanitize(str1);
var sanitized_str2 = sanitize(str2);
var pairs1 = wordLetterPairs(sanitized_str1);
var pairs2 = wordLetterPairs(sanitized_str2);
var intersection = 0, union = pairs1.length + pairs2.length;
var i, j, pair1, pair2;
for (i = 0; i < pairs1.length; i++) {
pair1 = pairs1[i];
for (j = 0; j < pairs2.length; j++) {
pair2 = pairs2[j];
if (pair1 == pair2) {
intersection ++;
delete pairs2[j];
break;
if (union === 0) {
if (sanitized_str1 === sanitized_str2) {
return 1;
} else {
return 0;
}
} else {
var i, j, pair1, pair2;
for (i = 0; i < pairs1.length; i++) {
pair1 = pairs1[i];
for (j = 0; j < pairs2.length; j++) {
pair2 = pairs2[j];
if (pair1 == pair2) {
intersection ++;
delete pairs2[j];
break;
}
}
}
return 2 * intersection / union;
}
return 2 * intersection / union;
};

module.exports = compare;
5 changes: 5 additions & 0 deletions spec/dice_coefficient_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ describe('dice', function () {
expect(dice('john', 'john')).toBe(1);
});

it('should match single character words', function () {
expect(dice('a', 'a')).toBe(1);
expect(dice('a', 'b')).toBe(0);
});

it('should handle total mis-matches', function () {
expect(dice('john', 'matt')).toBe(0);
});
Expand Down

0 comments on commit f07739a

Please sign in to comment.