-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
183 additions
and
28 deletions.
There are no files selected for viewing
34 changes: 13 additions & 21 deletions
34
packages/cspell-lib/src/SpellingDictionary/SpellingDictionaryFromTrie.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
packages/cspell-trie-lib/src/lib/distance/distance.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { editDistance, editDistanceWeighted, createWeightedMap, updatedWeightedMap } from './distance'; | ||
|
||
describe('distance', () => { | ||
test.each` | ||
wordA | wordB | expected | ||
${''} | ${''} | ${0} | ||
${'ab'} | ${'ba'} | ${100} | ||
${'bite'} | ${'bate'} | ${100} | ||
`('editDistance "$wordA" vs "$wordB"', ({ wordA, wordB, expected }) => { | ||
expect(editDistance(wordA, wordB)).toBe(expected); | ||
expect(editDistance(wordB, wordA)).toBe(expected); | ||
expect(editDistance(wordA, wordB, 200)).toBe(expected * 2); | ||
}); | ||
|
||
const weights = createWeightedMap([ | ||
{ | ||
map: 'aeiou', // cspell:disable-line | ||
replace: 50, | ||
insDel: 75, | ||
swap: 45, | ||
}, | ||
{ | ||
description: 'Vowels', | ||
map: 'aáâäãåeéêëiíîïoóôöõuúûüyÿ', // cspell:disable-line | ||
insDel: 50, | ||
replace: 25, // Replacing one vowel with another is cheap | ||
swap: 25, // Swapping vowels are cheap | ||
}, | ||
{ | ||
description: 'Vowel Accents', | ||
map: 'aáâäãå|eéêë|iíîï|oóôöõ|uúûü|yÿ', // cspell:disable-line | ||
replace: 10, // Make it cheap to add / remove an accent. | ||
}, | ||
]); | ||
|
||
updatedWeightedMap(weights, { | ||
map: 't(tt)|p(pp)|e(ee)(ea)|l(ll)|a(aa)|o(oo)(oh)(oa)(ao)(ou)|', | ||
replace: 55, | ||
}); | ||
|
||
test.each` | ||
wordA | wordB | expected | ||
${''} | ${''} | ${0} | ||
${'ab'} | ${'ba'} | ${100} | ||
${'botle' /* cspell:disable-line */} | ${'bottle'} | ${55} | ||
${'cafe'} | ${'café'} | ${10} | ||
${'tee'} | ${'tea'} | ${25} | ||
${'trie'} | ${'tree'} | ${25} | ||
`('editDistance "$wordA" vs "$wordB"', ({ wordA, wordB, expected }) => { | ||
expect(editDistanceWeighted(wordA, wordB, weights)).toBe(expected); | ||
expect(editDistanceWeighted(wordB, wordA, weights)).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { distanceAStarWeighted } from './distanceAStarWeighted'; | ||
import { levenshteinDistance } from './levenshtein'; | ||
import type { WeightedMapDef, WeightedMapTrie } from './weightedMaps'; | ||
import { addWeightedDefMapToTrie, buildWeightedMapTrie } from './weightedMaps'; | ||
|
||
export type { WeightedMapDef } from './weightedMaps'; | ||
|
||
const defaultCost = 100; | ||
|
||
/** | ||
* Calculate the edit distance between any two words. | ||
* Use the Damerau–Levenshtein distance algorithm. | ||
* @param wordA | ||
* @param wordB | ||
* @param editCost - the cost of each edit (defaults to 100) | ||
* @returns the edit distance. | ||
*/ | ||
export function editDistance(wordA: string, wordB: string, editCost = defaultCost): number { | ||
return levenshteinDistance(wordA, wordB) * editCost; | ||
} | ||
|
||
/** | ||
* Calculate the weighted edit distance between any two words. | ||
* @param wordA | ||
* @param wordB | ||
* @param weights - the weights to use | ||
* @param editCost - the cost of each edit (defaults to 100) | ||
* @returns the edit distance | ||
*/ | ||
export function editDistanceWeighted( | ||
wordA: string, | ||
wordB: string, | ||
weights: WeightedMap, | ||
editCost = defaultCost | ||
): number { | ||
return distanceAStarWeighted(wordA, wordB, weights, editCost); | ||
} | ||
|
||
/** | ||
* A Weighted map used by weighted distance calculations. | ||
*/ | ||
export type WeightedMap = WeightedMapTrie; | ||
|
||
/** | ||
* Collect Map definitions into a single weighted map. | ||
* @param defs - list of definitions | ||
* @returns A Weighted Map to be used with distance calculations. | ||
*/ | ||
export function createWeightedMap(defs: WeightedMapDef[]): WeightedMap { | ||
return buildWeightedMapTrie(defs); | ||
} | ||
|
||
/** | ||
* Update a WeightedMap with a WeightedMapDef | ||
* @param weightedMap - map to update | ||
* @param def - the definition to use | ||
*/ | ||
export function updatedWeightedMap(weightedMap: WeightedMap, def: WeightedMapDef): void { | ||
addWeightedDefMapToTrie(def, weightedMap); | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { editDistance, createWeightedMap, editDistanceWeighted } from './distance'; | ||
export type { WeightedMap, WeightedMapDef } from './distance'; |
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters