cspell-trie-lib 5.18.4 → 5.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +5 -1
- package/dist/lib/distance/distanceAStarWeighted.d.ts +1 -0
- package/dist/lib/distance/distanceAStarWeighted.js +5 -2
- package/dist/lib/distance/formatResultEx.js +3 -2
- package/dist/lib/distance/weightedMaps.d.ts +12 -0
- package/dist/lib/distance/weightedMaps.js +23 -1
- package/dist/lib/io/importExport.js +5 -1
- package/dist/lib/mappers/mapDictionaryInfo.d.ts +2 -0
- package/dist/lib/mappers/mapDictionaryInfo.js +15 -1
- package/dist/lib/mappers/mapDictionaryInfoToWeightMap.js +21 -1
- package/dist/lib/suggestions/suggestCollector.js +3 -2
- package/dist/lib/suggestions/walker/index.js +5 -1
- package/dist/lib/trie-util.d.ts +2 -2
- package/dist/lib/types.d.ts +1 -0
- package/dist/lib/walker.js +5 -1
- package/package.json +8 -8
package/dist/index.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -13,17 +13,20 @@ const PairingHeap_1 = require("../utils/PairingHeap");
|
|
|
13
13
|
*/
|
|
14
14
|
function distanceAStarWeighted(wordA, wordB, map, cost = 100) {
|
|
15
15
|
const best = _distanceAStarWeightedEx(wordA, wordB, map, cost);
|
|
16
|
-
|
|
16
|
+
const penalty = map.calcAdjustment(wordB);
|
|
17
|
+
return best.c + best.p + penalty;
|
|
17
18
|
}
|
|
18
19
|
exports.distanceAStarWeighted = distanceAStarWeighted;
|
|
19
20
|
function distanceAStarWeightedEx(wordA, wordB, map, cost = 100) {
|
|
20
21
|
const best = _distanceAStarWeightedEx(wordA, wordB, map, cost);
|
|
21
22
|
const aa = '^' + wordA + '$';
|
|
22
23
|
const bb = '^' + wordB + '$';
|
|
24
|
+
const penalty = map.calcAdjustment(wordB);
|
|
23
25
|
const result = {
|
|
24
26
|
a: aa,
|
|
25
27
|
b: bb,
|
|
26
|
-
cost: best.c + best.p,
|
|
28
|
+
cost: best.c + best.p + penalty,
|
|
29
|
+
penalty,
|
|
27
30
|
segments: [],
|
|
28
31
|
};
|
|
29
32
|
const segments = result.segments;
|
|
@@ -23,7 +23,7 @@ function vizWidth(s) {
|
|
|
23
23
|
function formatExResult(ex) {
|
|
24
24
|
if (!ex)
|
|
25
25
|
return '<undefined>';
|
|
26
|
-
const { cost, segments } = ex;
|
|
26
|
+
const { cost, segments, penalty } = ex;
|
|
27
27
|
const asString = segments.map(({ a, b, c, p }) => ({
|
|
28
28
|
a: `<${a}>`,
|
|
29
29
|
b: `<${b}>`,
|
|
@@ -47,7 +47,8 @@ function formatExResult(ex) {
|
|
|
47
47
|
const b = 'b: |' + parts.map(({ b, w }) => pR(b, w)).join('|') + '|';
|
|
48
48
|
const c = 'c: |' + parts.map(({ c, w }) => pL(c, w)).join('|') + '|';
|
|
49
49
|
const p = 'p: |' + parts.map(({ p, w }) => pL(p, w)).join('|') + '|';
|
|
50
|
-
|
|
50
|
+
const penaltyMsg = penalty ? `[+${penalty}]` : '';
|
|
51
|
+
return `<${ex.a.slice(1, -1)}> -> <${ex.b.slice(1, -1)}> (${cost - penalty}${penaltyMsg})\n${[a, b, c, p].join('\n')}\n`;
|
|
51
52
|
}
|
|
52
53
|
exports.formatExResult = formatExResult;
|
|
53
54
|
function formattedDistance(wordA, wordB, weightMap, cost) {
|
|
@@ -45,12 +45,24 @@ export interface WeightMap {
|
|
|
45
45
|
readonly insDel: TrieCost;
|
|
46
46
|
readonly replace: TrieTrieCost;
|
|
47
47
|
readonly swap: TrieTrieCost;
|
|
48
|
+
readonly adjustments: Map<string, PenaltyAdjustment>;
|
|
48
49
|
calcInsDelCosts(pos: CostPosition): Iterable<CostPosition>;
|
|
49
50
|
calcSwapCosts(pos: CostPosition): Iterable<CostPosition>;
|
|
50
51
|
calcReplaceCosts(pos: CostPosition): Iterable<CostPosition>;
|
|
52
|
+
calcAdjustment(word: string): number;
|
|
53
|
+
}
|
|
54
|
+
export interface PenaltyAdjustment {
|
|
55
|
+
/** Penalty Identifier */
|
|
56
|
+
id: string;
|
|
57
|
+
/** RegExp Pattern to match */
|
|
58
|
+
regexp: RegExp;
|
|
59
|
+
/** Penalty to apply */
|
|
60
|
+
penalty: number;
|
|
51
61
|
}
|
|
52
62
|
export declare function createWeightMap(...defs: SuggestionCostMapDef[]): WeightMap;
|
|
53
63
|
export declare function addDefToWeightMap(map: WeightMap, def: SuggestionCostMapDef, ...defs: SuggestionCostMapDef[]): WeightMap;
|
|
64
|
+
export declare function addAdjustment(map: WeightMap, ...adjustments: PenaltyAdjustment[]): WeightMap;
|
|
65
|
+
export declare function addAdjustment(map: WeightMap, adjustment: PenaltyAdjustment): WeightMap;
|
|
54
66
|
export declare function addDefsToWeightMap(map: WeightMap, defs: SuggestionCostMapDef[]): WeightMap;
|
|
55
67
|
export declare function splitMapSubstringsIterable(map: string): Iterable<string>;
|
|
56
68
|
export declare function splitMapSubstrings(map: string): string[];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.__testing__ = exports.lookupReplaceCost = exports.prettyPrintWeightMap = exports.prettyPrintSwap = exports.prettyPrintReplace = exports.splitMapSubstrings = exports.splitMapSubstringsIterable = exports.addDefsToWeightMap = exports.addDefToWeightMap = exports.createWeightMap = void 0;
|
|
3
|
+
exports.__testing__ = exports.lookupReplaceCost = exports.prettyPrintWeightMap = exports.prettyPrintSwap = exports.prettyPrintReplace = exports.splitMapSubstrings = exports.splitMapSubstringsIterable = exports.addDefsToWeightMap = exports.addAdjustment = exports.addDefToWeightMap = exports.createWeightMap = void 0;
|
|
4
4
|
const suggestCollector_1 = require("../suggestions/suggestCollector");
|
|
5
5
|
const matchPossibleWordSeparators = /[+∙•・●]/g;
|
|
6
6
|
function createWeightMap(...defs) {
|
|
@@ -13,6 +13,13 @@ function addDefToWeightMap(map, ...defs) {
|
|
|
13
13
|
return addDefsToWeightMap(map, defs);
|
|
14
14
|
}
|
|
15
15
|
exports.addDefToWeightMap = addDefToWeightMap;
|
|
16
|
+
function addAdjustment(map, ...adjustments) {
|
|
17
|
+
for (const adj of adjustments) {
|
|
18
|
+
map.adjustments.set(adj.id, adj);
|
|
19
|
+
}
|
|
20
|
+
return map;
|
|
21
|
+
}
|
|
22
|
+
exports.addAdjustment = addAdjustment;
|
|
16
23
|
function addDefsToWeightMap(map, defs) {
|
|
17
24
|
function addSet(set, def) {
|
|
18
25
|
addSetToTrieCost(map.insDel, set, def.insDel, def.penalty);
|
|
@@ -181,6 +188,7 @@ class _WeightedMap {
|
|
|
181
188
|
this.insDel = {};
|
|
182
189
|
this.replace = {};
|
|
183
190
|
this.swap = {};
|
|
191
|
+
this.adjustments = new Map();
|
|
184
192
|
}
|
|
185
193
|
*calcInsDelCosts(pos) {
|
|
186
194
|
const { a, ai, b, bi, c, p } = pos;
|
|
@@ -215,6 +223,20 @@ class _WeightedMap {
|
|
|
215
223
|
}
|
|
216
224
|
}
|
|
217
225
|
}
|
|
226
|
+
calcAdjustment(word) {
|
|
227
|
+
let penalty = 0;
|
|
228
|
+
for (const adj of this.adjustments.values()) {
|
|
229
|
+
if (adj.regexp.global) {
|
|
230
|
+
for (const _m of word.matchAll(adj.regexp)) {
|
|
231
|
+
penalty += adj.penalty;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
else if (adj.regexp.test(word)) {
|
|
235
|
+
penalty += adj.penalty;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return penalty;
|
|
239
|
+
}
|
|
218
240
|
}
|
|
219
241
|
function prettyPrintInsDel(trie, pfx = '', indent = ' ') {
|
|
220
242
|
function* walk() {
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { DictionaryInformation } from '../models/DictionaryInformation';
|
|
2
2
|
import type { SuggestionCostMapDef } from '../models/suggestionCostsDef';
|
|
3
|
+
import { PenaltyAdjustment } from '../distance/weightedMaps';
|
|
3
4
|
export declare function mapDictionaryInformation(dictInfo: DictionaryInformation): SuggestionCostMapDef[];
|
|
5
|
+
export declare function mapDictionaryInformationToAdjustment(dictInfo: DictionaryInformation): PenaltyAdjustment[];
|
|
4
6
|
//# sourceMappingURL=mapDictionaryInfo.d.ts.map
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mapDictionaryInformation = void 0;
|
|
3
|
+
exports.mapDictionaryInformationToAdjustment = exports.mapDictionaryInformation = void 0;
|
|
4
4
|
const cspell_pipe_1 = require("@cspell/cspell-pipe");
|
|
5
5
|
const locale_1 = require("../models/locale");
|
|
6
6
|
const util_1 = require("../utils/util");
|
|
@@ -52,4 +52,18 @@ function processAccents(accents, editCost) {
|
|
|
52
52
|
const cs = toCharSets(accents, '\u0300-\u0341', editCost.accentCosts);
|
|
53
53
|
return cs.map((cs) => (0, mapToSuggestionCostDef_1.parseAccents)(cs, editCost)).filter(util_1.isDefined);
|
|
54
54
|
}
|
|
55
|
+
function mapDictionaryInformationToAdjustment(dictInfo) {
|
|
56
|
+
if (!dictInfo.adjustments)
|
|
57
|
+
return [];
|
|
58
|
+
return dictInfo.adjustments.map(mapAdjustment);
|
|
59
|
+
}
|
|
60
|
+
exports.mapDictionaryInformationToAdjustment = mapDictionaryInformationToAdjustment;
|
|
61
|
+
function mapAdjustment(adj) {
|
|
62
|
+
const { id, regexp, penalty } = adj;
|
|
63
|
+
return {
|
|
64
|
+
id: id,
|
|
65
|
+
regexp: new RegExp(regexp),
|
|
66
|
+
penalty,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
55
69
|
//# sourceMappingURL=mapDictionaryInfo.js.map
|
|
@@ -10,9 +10,29 @@ const defaultDefs = [
|
|
|
10
10
|
penalty: 200,
|
|
11
11
|
},
|
|
12
12
|
];
|
|
13
|
+
const defaultAdjustments = [
|
|
14
|
+
{
|
|
15
|
+
id: 'compound-case-change',
|
|
16
|
+
regexp: /\p{Ll}∙\p{Lu}/gu,
|
|
17
|
+
penalty: 1000,
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
id: 'short-compounds-1',
|
|
21
|
+
regexp: /^[^∙]{0,2}(?=∙)|∙[^∙]{0,2}(?=∙|$)/gm,
|
|
22
|
+
penalty: 100,
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
id: 'short-compounds-3',
|
|
26
|
+
regexp: /^[^∙]{3}(?=∙)|∙[^∙]{3}(?=∙|$)/gm,
|
|
27
|
+
penalty: 50,
|
|
28
|
+
},
|
|
29
|
+
];
|
|
13
30
|
function mapDictionaryInformationToWeightMap(dictInfo) {
|
|
14
31
|
const defs = (0, mapDictionaryInfo_1.mapDictionaryInformation)(dictInfo).concat(defaultDefs);
|
|
15
|
-
|
|
32
|
+
const adjustments = (0, mapDictionaryInfo_1.mapDictionaryInformationToAdjustment)(dictInfo);
|
|
33
|
+
const map = (0, weightedMaps_1.createWeightMap)(...defs);
|
|
34
|
+
(0, weightedMaps_1.addAdjustment)(map, ...defaultAdjustments, ...adjustments);
|
|
35
|
+
return map;
|
|
16
36
|
}
|
|
17
37
|
exports.mapDictionaryInformationToWeightMap = mapDictionaryInformationToWeightMap;
|
|
18
38
|
exports.__testing__ = {};
|
|
@@ -150,8 +150,9 @@ function suggestionCollector(wordToMatch, options) {
|
|
|
150
150
|
const sorted = values.sort(compSuggestionResults).map(cleanCompoundResult);
|
|
151
151
|
let i = Math.min(sorted.length, numSuggestions) - 1;
|
|
152
152
|
const limit = includeTies ? sorted.length : Math.min(sorted.length, numSuggestions);
|
|
153
|
-
const
|
|
154
|
-
|
|
153
|
+
const iCost = sorted[i].cost;
|
|
154
|
+
const maxCost = Math.min(iCost, weightMap ? changeLimit * BASE_COST - 1 : iCost);
|
|
155
|
+
for (i = 1; i < limit && sorted[i].cost <= maxCost; ++i) {
|
|
155
156
|
// loop
|
|
156
157
|
}
|
|
157
158
|
sorted.length = i;
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/dist/lib/trie-util.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Sequence } from 'gensequence';
|
|
2
2
|
import { PartialTrieOptions, TrieNode, TrieOptions, TrieRoot } from './TrieNode';
|
|
3
|
-
import type {
|
|
3
|
+
import type { PartialWithUndefined, RemoveUndefined } from './types';
|
|
4
4
|
import { YieldResult } from './walker';
|
|
5
5
|
export declare function insert(text: string, node?: TrieNode): TrieNode;
|
|
6
6
|
export declare function isWordTerminationNode(node: TrieNode): boolean;
|
|
@@ -54,5 +54,5 @@ export declare const normalizeWordToLowercase: (text: string) => string;
|
|
|
54
54
|
*/
|
|
55
55
|
export declare const normalizeWordForCaseInsensitive: (text: string) => string[];
|
|
56
56
|
export declare function isDefined<T>(t: T | undefined): t is T;
|
|
57
|
-
export declare function clean<T>(t: T):
|
|
57
|
+
export declare function clean<T>(t: T): RemoveUndefined<T>;
|
|
58
58
|
//# sourceMappingURL=trie-util.d.ts.map
|
package/dist/lib/types.d.ts
CHANGED
|
@@ -66,4 +66,5 @@ export declare type RemoveUndefined<T> = {
|
|
|
66
66
|
* Make all `undefined` optional and removes the `undefined`
|
|
67
67
|
*/
|
|
68
68
|
export declare type UndefinedToOptional<T> = RemoveUndefined<MakeOptional<T>>;
|
|
69
|
+
export declare type ArrayItem<T extends Array<unknown>> = T extends Array<infer R> ? R : never;
|
|
69
70
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/lib/walker.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cspell-trie-lib",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.19.1",
|
|
4
4
|
"description": "Trie Data Structure to support cspell.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -37,21 +37,21 @@
|
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/streetsidesoftware/cspell#readme",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@cspell/cspell-pipe": "^5.
|
|
41
|
-
"fs-extra": "^10.0.
|
|
40
|
+
"@cspell/cspell-pipe": "^5.19.1",
|
|
41
|
+
"fs-extra": "^10.0.1",
|
|
42
42
|
"gensequence": "^3.1.1"
|
|
43
43
|
},
|
|
44
44
|
"engines": {
|
|
45
45
|
"node": ">=12.13.0"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
|
-
"@cspell/cspell-types": "^5.
|
|
49
|
-
"@cspell/dict-en_us": "^2.1.
|
|
48
|
+
"@cspell/cspell-types": "^5.19.1",
|
|
49
|
+
"@cspell/dict-en_us": "^2.1.7",
|
|
50
50
|
"@cspell/dict-es-es": "^2.1.0",
|
|
51
51
|
"@types/fs-extra": "^9.0.13",
|
|
52
|
-
"@types/node": "^17.0.
|
|
53
|
-
"jest": "^27.5.
|
|
52
|
+
"@types/node": "^17.0.21",
|
|
53
|
+
"jest": "^27.5.1",
|
|
54
54
|
"rimraf": "^3.0.2"
|
|
55
55
|
},
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "622cd59a44bf1f76d538e3cf37983f387c5423e8"
|
|
57
57
|
}
|