Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

levenshtein mismatched lengths fixed .spec added for levenshtein.

  • Loading branch information...
commit bb8a50daf481430763455aced67226f4f9501def 1 parent 1ac29c3
@chrisumbel chrisumbel authored
View
36 lib/natural/distance/levenshtein_distance.js
@@ -1,24 +1,56 @@
/*
+Copyright (c) 2011, sidred123, Chris Umbel
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+/*
+ * contribution by sidred123
+ */
+
+/*
* Compute the Levenshtein distance between two strings.
* Algorithm based from Speech and Language Processing - Daniel Jurafsky and James H. Martin.
*/
function LevenshteinDistance (source, target, options) {
-
options = options || {};
options.insertion_cost = options.insertion_cost || 1;
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;
}
+
for (var column = 1; column <= targetLength; column++) {
- distanceMatrix[0][column] = distanceMatrix[column-1][0] + options.deletion_cost;
+ distanceMatrix[0][column] = distanceMatrix[column - 1][0] + options.deletion_cost;
}
for (var row = 1; row <= sourceLength; row++) {
View
46 spec/levenshtein_spec.js
@@ -0,0 +1,46 @@
+/*
+Copyright (c) 2011, Chris Umbel
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+var levenshteinDistance = require('lib/natural/distance/levenshtein_distance')
+
+describe('levenshtein_distance', function() {
+ it('should replace 2', function() {
+ expect(levenshteinDistance('doctor', 'doktor')).toBe(2);
+ });
+
+ it('should insert 1', function() {
+ expect(levenshteinDistance('doctor', 'docto')).toBe(1);
+ });
+
+ it('should delete 1', function() {
+ expect(levenshteinDistance('flat', 'flats')).toBe(1);
+ });
+
+ it('should combine operations', function() {
+ expect(levenshteinDistance('flad', 'flaten')).toBe(4);
+ expect(levenshteinDistance('flaten', 'flad')).toBe(4);
+ });
+
+ it('should consider perfect matches 0', function() {
+ expect(levenshteinDistance('one', 'one')).toBe(0);
+ });
+});

0 comments on commit bb8a50d

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