Skip to content

Commit

Permalink
Repair the TypeScript for distance functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo-ter-Doest committed Mar 25, 2024
1 parent 93dae46 commit 51b2bee
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 9 deletions.
15 changes: 6 additions & 9 deletions lib/natural/distance/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ declare interface DamerauLevenshteinDistanceOptions {
/** @default 1 */
transposition_cost?: number
/** @default false */
search?: boolean
/** @default false */
restricted?: boolean
damerau?: boolean
}

declare interface SubstringDistanceResult {
Expand All @@ -66,23 +63,23 @@ export function LevenshteinDistance (
* Valid edit operations are:
* - transposition, insertion, deletion, and substitution
*/
export function DamerauLevenshteinDistance (
export function LevenshteinDistanceSearch (
source: string,
target: string,
options: DamerauLevenshteinDistanceOptions & { search: true }
options?: DamerauLevenshteinDistanceOptions
): SubstringDistanceResult

export function DamerauLevenshteinDistance (
source: string,
target: string,
options?: DamerauLevenshteinDistanceOptions & { search?: false }
options?: DamerauLevenshteinDistanceOptions
): number

export function DamerauLevenshteinDistance (
export function DamerauLevenshteinDistanceSearch (
source: string,
target: string,
options: DamerauLevenshteinDistanceOptions & { search: boolean }
): number | SubstringDistanceResult
options?: DamerauLevenshteinDistanceOptions
): SubstringDistanceResult

export function DiceCoefficient (str1: string, str2: string): number

Expand Down
53 changes: 53 additions & 0 deletions ts_spec/damerau_levenshtein_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
'use strict'

import { DamerauLevenshteinDistance, DamerauLevenshteinDistanceSearch } from 'lib/natural'

describe('DamerauLevenshtein', function () {
describe('default', function () {
it('should be 0 when given equal strings', function () {
expect(DamerauLevenshteinDistance('test', 'test')).toBe(0)
})

it('should calculate 1 for adjacent transposition', function () {
expect(DamerauLevenshteinDistance('za', 'az')).toBe(1)
expect(DamerauLevenshteinDistance('Tomato', 'oTmato')).toBe(1)
})

it('should handle custom transposition_cost', function () {
expect(DamerauLevenshteinDistance('za', 'az', { transposition_cost: 0 })).toBe(0)
})

it('should calculate 2 when there are 2 transpositions', function () {
expect(DamerauLevenshteinDistance('tomato', 'otmaot')).toBe(2)
})

it('should calculate 2 for 1 transposition and 1 insertion', function () {
expect(DamerauLevenshteinDistance('CA', 'ABC')).toBe(2)
expect(DamerauLevenshteinDistance('a cat', 'a abct')).toBe(2)
})
})

describe('options.restricted = true', function () {
const restricted = { restricted: true }
it('should calculate 0 for equal strings', function () {
expect(DamerauLevenshteinDistance('identity', 'identity', restricted)).toBe(0)
})
it('should calculate 1 for an adjacent transposition', function () {
expect(DamerauLevenshteinDistance('za', 'az', restricted)).toBe(1)
})
it('should not count transposition more than 1 char away', function () {
expect(DamerauLevenshteinDistance('CA', 'ABC', restricted)).toBe(3)
})
})

it('should combine search with Damerau', function () {
const source = 'The RainCoat BookStore'
const target = 'All the best books are here at the Rain Coats Book Store'
const result = DamerauLevenshteinDistanceSearch(source, target)
expect(result).toEqual({
substring: 'the Rain Coats Book Store',
distance: 4,
offset: 31
})
})
})

0 comments on commit 51b2bee

Please sign in to comment.