Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #33 from sidred123/master

Bug fix for Levenshtein edit distance.
  • Loading branch information...
commit 5683e2344f4b7de46ec625ac7ffcd5f7a56fc966 2 parents eadfc30 + e4198c8
@chrisumbel chrisumbel authored
View
13 lib/natural/distance/levenshtein_distance.js
@@ -35,28 +35,23 @@ function LevenshteinDistance (source, target, options) {
options.deletion_cost = options.deletion_cost || 1;
options.substitution_cost = options.substitution_cost || 2;
- if(source.length < target.length) {
- var tmp = source;
- source = target, target = tmp;
- }
-
var sourceLength = source.length;
var targetLength = target.length;
var distanceMatrix = [[0]];
for (var row = 1; row <= sourceLength; row++) {
distanceMatrix[row] = [];
- distanceMatrix[row][0] = distanceMatrix[row-1][0] + options.insertion_cost;
+ distanceMatrix[row][0] = distanceMatrix[row-1][0] + options.deletion_cost;
}
for (var column = 1; column <= targetLength; column++) {
- distanceMatrix[0][column] = distanceMatrix[column - 1][0] + options.deletion_cost;
+ distanceMatrix[0][column] = distanceMatrix[0][column-1] + options.insertion_cost;
}
for (var row = 1; row <= sourceLength; row++) {
for (var column = 1; column <= targetLength; column++) {
- var costToInsert = distanceMatrix[row-1][column] + options.insertion_cost;
- var costToDelete = distanceMatrix[row][column-1] + options.deletion_cost;
+ var costToInsert = distanceMatrix[row][column-1] + options.insertion_cost;
+ var costToDelete = distanceMatrix[row-1][column] + options.deletion_cost;
var sourceElement = source[row-1];
var targetElement = target[column-1];
View
22 spec/levenshtein_spec.js
@@ -31,11 +31,11 @@ describe('levenshtein_distance', function() {
expect(levenshteinDistance('doctor', 'doktor', {substitution_cost: 1})).toBe(1);
});
- it('should insert 1', function() {
+ it('should delete 1', function() {
expect(levenshteinDistance('doctor', 'docto')).toBe(1);
});
- it('should delete 1', function() {
+ it('should insert 1', function() {
expect(levenshteinDistance('flat', 'flats')).toBe(1);
});
@@ -46,5 +46,21 @@ describe('levenshtein_distance', function() {
it('should consider perfect matches 0', function() {
expect(levenshteinDistance('one', 'one')).toBe(0);
- });
+ });
+
+ it('different deletion cost should work', function() {
+ expect(levenshteinDistance('ones', 'one', {deletion_cost: 3})).toBe(3);
+ });
+
+ it('different insertion cost should work', function() {
+ expect(levenshteinDistance('one', 'ones', {deletion_cost: 3, insertion_cost: 5})).toBe(5);
+ });
+
+ it('delete all characters with -ve cost', function() {
+ expect(levenshteinDistance('delete', '', {deletion_cost: -1})).toBe(-6);
+ });
+
+ it('insert all characters', function() {
+ expect(levenshteinDistance('', 'insert')).toBe(6);
+ });
});
Please sign in to comment.
Something went wrong with that request. Please try again.