diff --git a/lib/natural/distance/index.js b/lib/natural/distance/index.js index 6e8583cd0..4095fceba 100644 --- a/lib/natural/distance/index.js +++ b/lib/natural/distance/index.js @@ -25,5 +25,7 @@ THE SOFTWARE. exports.JaroWinklerDistance = require('./jaro-winkler_distance') exports.LevenshteinDistance = require('./levenshtein_distance').LevenshteinDistance exports.DamerauLevenshteinDistance = require('./levenshtein_distance').DamerauLevenshteinDistance +exports.LevenshteinDistanceSearch = require('./levenshtein_distance').LevenshteinDistanceSearch +exports.DamerauLevenshteinDistanceSearch = require('./levenshtein_distance').DamerauLevenshteinDistanceSearch exports.DiceCoefficient = require('./dice_coefficient') exports.HammingDistance = require('./hamming_distance') diff --git a/lib/natural/distance/levenshtein_distance.js b/lib/natural/distance/levenshtein_distance.js index f2ce1a6d7..1301225b0 100644 --- a/lib/natural/distance/levenshtein_distance.js +++ b/lib/natural/distance/levenshtein_distance.js @@ -94,13 +94,27 @@ function DamerauLevenshteinDistance (source, target, options) { const damLevOptions = _.extend( { transposition_cost: 1, restricted: false }, options || {}, - { damerau: true } + { damerau: true, search: false } ) return levenshteinDistance(source, target, damLevOptions) } +function DamerauLevenshteinDistanceSearch (source, target, options) { + const damLevOptions = _.extend( + { transposition_cost: 1, restricted: false }, + options || {}, + { damerau: true, search: true } + ) + return levenshteinDistance(source, target, damLevOptions) +} + +function LevenshteinDistanceSearch (source, target, options) { + const levOptions = _.extend({}, options || {}, { damerau: false, search: true }) + return levenshteinDistance(source, target, levOptions) +} + function LevenshteinDistance (source, target, options) { - const levOptions = _.extend({}, options || {}, { damerau: false }) + const levOptions = _.extend({}, options || {}, { damerau: false, search: false }) return levenshteinDistance(source, target, levOptions) } @@ -221,5 +235,7 @@ function levenshteinDistance (source, target, options) { module.exports = { LevenshteinDistance: LevenshteinDistance, - DamerauLevenshteinDistance: DamerauLevenshteinDistance + LevenshteinDistanceSearch: LevenshteinDistanceSearch, + DamerauLevenshteinDistance: DamerauLevenshteinDistance, + DamerauLevenshteinDistanceSearch: DamerauLevenshteinDistanceSearch } diff --git a/spec/levenshtein_spec.js b/spec/levenshtein_spec.js index f1c995bb1..d8bb55abd 100644 --- a/spec/levenshtein_spec.js +++ b/spec/levenshtein_spec.js @@ -24,38 +24,40 @@ THE SOFTWARE. const levenshteinDistance = require('../lib/natural/distance/levenshtein_distance').LevenshteinDistance +const levenshteinDistanceSearch = + require('../lib/natural/distance/levenshtein_distance').LevenshteinDistanceSearch describe('levenshtein_distance', function () { describe('options.search = true', function () { it('should find cheapest substring', function () { - expect(levenshteinDistance('kitten', 'sitting', { search: true })) + expect(levenshteinDistanceSearch('kitten', 'sitting')) .toEqual({ substring: 'sittin', distance: 2, offset: 0 }) }) it('should find 0 cost substring in target', function () { - expect(levenshteinDistance('doctor', 'the doctor is in', { search: true })) + expect(levenshteinDistanceSearch('doctor', 'the doctor is in')) .toEqual({ substring: 'doctor', distance: 0, offset: 4 }) }) it('should find 1 cost substring in target', function () { - expect(levenshteinDistance('doctor', 'the doktor is in', { search: true })) + expect(levenshteinDistanceSearch('doctor', 'the doktor is in')) .toEqual({ substring: 'doktor', distance: 1, offset: 4 }) }) it('should return empty substring when that is cleapest match', function () { - expect(levenshteinDistance('doctor', '000000000000', { search: true })) + expect(levenshteinDistanceSearch('doctor', '000000000000')) .toEqual({ substring: '', distance: 6, offset: 0 }) }) it('different insertion costs should work', function () { // delete 10 0's at cost 1 and insert the letters for doctor at cost -1 - expect(levenshteinDistance('0000000000', 'doctor', { search: true, insertion_cost: -1 })) + expect(levenshteinDistanceSearch('0000000000', 'doctor', { insertion_cost: -1 })) .toEqual({ substring: 'doctor', distance: 4, offset: 0 }) }) it('different deletion costs should work', function () { // delete 10 0's at cost -10 - expect(levenshteinDistance('0000000000', 'doctor', { search: true, deletion_cost: -1 })) + expect(levenshteinDistanceSearch('0000000000', 'doctor', { deletion_cost: -1 })) .toEqual({ substring: '', distance: -10, offset: 0 }) }) })