Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: refactor type definitions for suggestion edit maps (#2258)
- Loading branch information
Showing
15 changed files
with
492 additions
and
89 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
packages/cspell-trie-lib/src/__snapshots__/index.test.ts.snap
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,55 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Validate index.ts Track changes to the API. 1`] = ` | ||
Array [ | ||
"CASE_INSENSITIVE_PREFIX", | ||
"COMPOUND", | ||
"COMPOUND_FIX", | ||
"ChildMap", | ||
"CompoundWordsMethod", | ||
"FLAG_WORD", | ||
"FORBID", | ||
"FORBID_PREFIX", | ||
"JOIN_SEPARATOR", | ||
"NORMALIZED", | ||
"OPTIONAL_COMPOUND", | ||
"OPTIONAL_COMPOUND_FIX", | ||
"Trie", | ||
"TrieBuilder", | ||
"WORD_SEPARATOR", | ||
"buildTrie", | ||
"buildTrieFast", | ||
"consolidate", | ||
"countNodes", | ||
"countWords", | ||
"createTriFromList", | ||
"createTrieRoot", | ||
"createWeightedMap", | ||
"defaultTrieOptions", | ||
"editDistance", | ||
"editDistanceWeighted", | ||
"findNode", | ||
"has", | ||
"hintedWalker", | ||
"importTrie", | ||
"insert", | ||
"isCircular", | ||
"isDefined", | ||
"isWordTerminationNode", | ||
"iterateTrie", | ||
"iteratorTrieWords", | ||
"mergeDefaults", | ||
"mergeOptionalWithDefaults", | ||
"normalizeWord", | ||
"normalizeWordForCaseInsensitive", | ||
"normalizeWordToLowercase", | ||
"orderTrie", | ||
"parseDictionary", | ||
"parseDictionaryLines", | ||
"serializeTrie", | ||
"suggestionCollector", | ||
"trieNodeToRoot", | ||
"walk", | ||
"walker", | ||
] | ||
`; |
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 |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import * as lib from '.'; | ||
import * as lib from './index'; | ||
|
||
describe('Validate index.ts', () => { | ||
test('index', () => { | ||
expect(typeof lib.Trie).toBe('function'); | ||
}); | ||
test('Track changes to the API.', () => { | ||
expect(Object.keys(lib).sort()).toMatchSnapshot(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { editDistance, createWeightedMap, editDistanceWeighted } from './distance'; | ||
export type { WeightedMap, WeightedMapDef } from './distance'; | ||
export { createWeightedMap, editDistance, editDistanceWeighted } from './distance'; | ||
export type { WeightedMap } from './distance'; | ||
export type { SuggestionCostMapDef } from './suggestionCostsDef'; |
73 changes: 73 additions & 0 deletions
73
packages/cspell-trie-lib/src/lib/distance/suggestionCostsDef.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,73 @@ | ||
// cspell:ignore aeiouy | ||
/** | ||
* A WeightedMapDef enables setting weights for edits between related characters and substrings. | ||
* | ||
* Multiple groups can be defined using a `|`. | ||
* A multi-character substring is defined using `()`. | ||
* | ||
* For example, in some languages, some letters sound alike. | ||
* | ||
* ```ts | ||
* { | ||
* map: 'sc(sh)(sch)(ss)|t(tt)', // two groups. | ||
* replace: 50, // Make it 1/2 the cost of a normal edit to replace a `t` with `tt`. | ||
* } | ||
* ``` | ||
* | ||
* The following could be used to make inserting, removing, or replacing vowels cheaper. | ||
* ```ts | ||
* { | ||
* map: 'aeiouy', //. | ||
* insDel: 50, // Make it is cheaper to insert or delete a vowel. | ||
* replace: 45, // It is even cheaper to replace one with another. | ||
* } | ||
* ``` | ||
* | ||
* Note: the default edit distance is 100. | ||
*/ | ||
export type SuggestionCostMapDef = CostMapDefReplace | CostMapDefInsDel | CostMapDefSwap; | ||
|
||
interface CostMapDefBase { | ||
/** | ||
* The set of substrings to map, these are generally single character strings. | ||
* | ||
* Multiple sets can be defined by using a `|` to separate them. | ||
* | ||
* Example: `"eéê|aåá"` contains two different sets. | ||
* | ||
* To add a multi-character substring use `()`. | ||
* | ||
* Example: `"f(ph)(gh)"` results in the following set: `f`, `ph`, `gh`. | ||
*/ | ||
map: string; | ||
/** The cost to insert/delete one of the substrings in the map. Note: insert/delete costs are symmetrical. */ | ||
insDel?: number; | ||
/** | ||
* The cost to replace of of the substrings in the map with another substring in the map. | ||
* Example: Map['a', 'i'] | ||
* This would be the cost to substitute `a` with `i`: Like `bat` to `bit` or the reverse. | ||
*/ | ||
replace?: number; | ||
/** | ||
* The cost to swap two adjacent substrings found in the map. | ||
* Example: Map['e', 'i'] | ||
* This represents the cost to change `ei` to `ie` or the reverse. | ||
*/ | ||
swap?: number; | ||
/** | ||
* A description to describe the purpose of the map. | ||
*/ | ||
description?: string; | ||
} | ||
|
||
export interface CostMapDefReplace extends CostMapDefBase { | ||
replace: number; | ||
} | ||
|
||
export interface CostMapDefInsDel extends CostMapDefBase { | ||
insDel: number; | ||
} | ||
|
||
export interface CostMapDefSwap extends CostMapDefBase { | ||
swap: number; | ||
} |
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
Oops, something went wrong.