Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions lib/natural/distance/levenshtein_distance.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,16 @@ function levenshteinDistance (source, target, options) {

distanceMatrix[row][column] = { cost: minCostParent.cost, parentCell: minCostParent.coordinates }

if (isUnrestrictedDamerau) {
lastRowMap[sourceElement] = row
if (sourceElement === targetElement) {
lastColMatch = column
}
if (isUnrestrictedDamerau && sourceElement === targetElement) {
lastColMatch = column
}
}
// da[c]: last row in which source character c occurred. Update after the
// inner loop so the transposition lookup only references earlier rows; the
// per-column update gave wrong distances for repeated characters (issue #757).
if (isUnrestrictedDamerau) {
lastRowMap[source[row - 1]] = row
}
}

if (!options.search) {
Expand Down
5 changes: 5 additions & 0 deletions spec/damerau_levenshtein_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ describe('DamerauLevenshtein', function () {
expect(DamerauLevenshteinDistance('CA', 'ABC')).toBe(2)
expect(DamerauLevenshteinDistance('a cat', 'a abct')).toBe(2)
})

it('should stay symmetric for strings with repeated characters (issue #757)', function () {
expect(DamerauLevenshteinDistance('0,1,10,11', '0,11,110,111')).toBe(3)
expect(DamerauLevenshteinDistance('0,11,110,111', '0,1,10,11')).toBe(3)
})
})

describe('options.restricted = true', function () {
Expand Down