cspell-trie-lib 5.17.0 → 5.18.0-alpha.0

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.
Files changed (31) hide show
  1. package/dist/lib/distance/formatResultEx.d.ts +5 -0
  2. package/dist/lib/distance/formatResultEx.js +58 -0
  3. package/dist/lib/distance/weightedMaps.d.ts +1 -0
  4. package/dist/lib/distance/weightedMaps.js +13 -6
  5. package/dist/lib/index.d.ts +2 -1
  6. package/dist/lib/index.js +3 -1
  7. package/dist/lib/mappers/mapCosts.d.ts +6 -0
  8. package/dist/lib/mappers/mapCosts.js +28 -0
  9. package/dist/lib/mappers/mapDictionaryInfo.d.ts +4 -0
  10. package/dist/lib/mappers/mapDictionaryInfo.js +55 -0
  11. package/dist/lib/mappers/mapDictionaryInfoToWeightMap.d.ts +3 -24
  12. package/dist/lib/mappers/mapDictionaryInfoToWeightMap.js +15 -171
  13. package/dist/lib/mappers/mapHunspellInformation.d.ts +42 -0
  14. package/dist/lib/mappers/mapHunspellInformation.js +210 -0
  15. package/dist/lib/mappers/mapToSuggestionCostDef.d.ts +17 -0
  16. package/dist/lib/mappers/mapToSuggestionCostDef.js +124 -0
  17. package/dist/lib/models/DictionaryInformation.d.ts +1 -0
  18. package/dist/lib/models/locale/index.d.ts +2 -0
  19. package/dist/lib/models/locale/index.js +7 -0
  20. package/dist/lib/models/locale/knownLocales.d.ts +3 -0
  21. package/dist/lib/models/locale/knownLocales.js +537 -0
  22. package/dist/lib/models/locale/locale.d.ts +24 -0
  23. package/dist/lib/models/locale/locale.js +69 -0
  24. package/dist/lib/suggestions/suggestCollector.js +6 -1
  25. package/dist/lib/types.d.ts +50 -0
  26. package/dist/lib/types.js +12 -0
  27. package/dist/lib/utils/text.d.ts +53 -0
  28. package/dist/lib/utils/text.js +111 -0
  29. package/dist/lib/utils/util.d.ts +16 -0
  30. package/dist/lib/utils/util.js +36 -1
  31. package/package.json +5 -5
@@ -0,0 +1,5 @@
1
+ import { WeightMap } from '.';
2
+ import { ExResult } from './distanceAStarWeighted';
3
+ export declare function formatExResult(ex: ExResult | undefined): string;
4
+ export declare function formattedDistance(wordA: string, wordB: string, weightMap: WeightMap, cost?: number): string;
5
+ //# sourceMappingURL=formatResultEx.d.ts.map
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.formattedDistance = exports.formatExResult = void 0;
4
+ const distanceAStarWeighted_1 = require("./distanceAStarWeighted");
5
+ function pL(s, w) {
6
+ const strWidth = vizWidth(s);
7
+ const w0 = Math.max(0, w - strWidth);
8
+ return ' '.repeat(w0) + s;
9
+ }
10
+ function pR(s, w) {
11
+ const strWidth = vizWidth(s);
12
+ const w0 = Math.max(0, w - strWidth);
13
+ return s + ' '.repeat(w0);
14
+ }
15
+ function vizWidth(s) {
16
+ const r = s.replace(/[\u0300-\u036F\u007f-\u009f]/gu, '');
17
+ let i = 0;
18
+ for (const c of r) {
19
+ i += c.length;
20
+ }
21
+ return i;
22
+ }
23
+ function formatExResult(ex) {
24
+ if (!ex)
25
+ return '<undefined>';
26
+ const { cost, segments } = ex;
27
+ const asString = segments.map(({ a, b, c, p }) => ({
28
+ a: `<${a}>`,
29
+ b: `<${b}>`,
30
+ c: c.toString(10),
31
+ p: p.toString(10),
32
+ }));
33
+ asString.push({
34
+ a: '',
35
+ b: '',
36
+ c: ' = ' + segments.reduce((sum, { c }) => sum + c, 0).toString(10),
37
+ p: ' = ' + segments.reduce((sum, { p }) => sum + p, 0).toString(10),
38
+ });
39
+ const parts = asString.map(({ a, b, c, p }) => ({
40
+ a,
41
+ b,
42
+ c,
43
+ p,
44
+ w: Math.max(vizWidth(a), vizWidth(b), vizWidth(c), vizWidth(p)),
45
+ }));
46
+ const a = 'a: |' + parts.map(({ a, w }) => pR(a, w)).join('|') + '|';
47
+ const b = 'b: |' + parts.map(({ b, w }) => pR(b, w)).join('|') + '|';
48
+ const c = 'c: |' + parts.map(({ c, w }) => pL(c, w)).join('|') + '|';
49
+ const p = 'p: |' + parts.map(({ p, w }) => pL(p, w)).join('|') + '|';
50
+ return `<${ex.a.slice(1, -1)}> -> <${ex.b.slice(1, -1)}> (${cost})\n${[a, b, c, p].join('\n')}\n`;
51
+ }
52
+ exports.formatExResult = formatExResult;
53
+ function formattedDistance(wordA, wordB, weightMap, cost) {
54
+ const x = (0, distanceAStarWeighted_1.distanceAStarWeightedEx)(wordA, wordB, weightMap, cost);
55
+ return formatExResult(x);
56
+ }
57
+ exports.formattedDistance = formattedDistance;
58
+ //# sourceMappingURL=formatResultEx.js.map
@@ -51,6 +51,7 @@ export interface WeightMap {
51
51
  }
52
52
  export declare function createWeightMap(...defs: SuggestionCostMapDef[]): WeightMap;
53
53
  export declare function addDefToWeightMap(map: WeightMap, def: SuggestionCostMapDef, ...defs: SuggestionCostMapDef[]): WeightMap;
54
+ export declare function addDefsToWeightMap(map: WeightMap, defs: SuggestionCostMapDef[]): WeightMap;
54
55
  export declare function splitMapSubstringsIterable(map: string): Iterable<string>;
55
56
  export declare function splitMapSubstrings(map: string): string[];
56
57
  /**
@@ -1,17 +1,17 @@
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.addDefToWeightMap = exports.createWeightMap = void 0;
3
+ exports.__testing__ = exports.lookupReplaceCost = exports.prettyPrintWeightMap = exports.prettyPrintSwap = exports.prettyPrintReplace = exports.splitMapSubstrings = exports.splitMapSubstringsIterable = exports.addDefsToWeightMap = exports.addDefToWeightMap = exports.createWeightMap = void 0;
4
4
  function createWeightMap(...defs) {
5
5
  const map = _createWeightMap();
6
- _addDefToWeightMap(map, ...defs);
6
+ addDefsToWeightMap(map, defs);
7
7
  return map;
8
8
  }
9
9
  exports.createWeightMap = createWeightMap;
10
10
  function addDefToWeightMap(map, ...defs) {
11
- return _addDefToWeightMap(map, ...defs);
11
+ return addDefsToWeightMap(map, defs);
12
12
  }
13
13
  exports.addDefToWeightMap = addDefToWeightMap;
14
- function _addDefToWeightMap(map, ...defs) {
14
+ function addDefsToWeightMap(map, defs) {
15
15
  function addSet(set, def) {
16
16
  addSetToTrieCost(map.insDel, set, def.insDel, def.penalty);
17
17
  addSetToTrieTrieCost(map.replace, set, def.replace, def.penalty);
@@ -23,6 +23,7 @@ function _addDefToWeightMap(map, ...defs) {
23
23
  }
24
24
  return map;
25
25
  }
26
+ exports.addDefsToWeightMap = addDefsToWeightMap;
26
27
  function _createWeightMap() {
27
28
  return new _WeightedMap();
28
29
  }
@@ -40,12 +41,18 @@ function highest(a, b) {
40
41
  return a;
41
42
  return a >= b ? a : b;
42
43
  }
44
+ function normalize(s) {
45
+ const f = new Set([s]);
46
+ f.add(s.normalize('NFC'));
47
+ f.add(s.normalize('NFD'));
48
+ return f;
49
+ }
43
50
  function* splitMapSubstringsIterable(map) {
44
51
  let seq = '';
45
52
  let mode = 0;
46
53
  for (const char of map) {
47
54
  if (mode && char === ')') {
48
- yield seq;
55
+ yield* normalize(seq);
49
56
  mode = 0;
50
57
  continue;
51
58
  }
@@ -58,7 +65,7 @@ function* splitMapSubstringsIterable(map) {
58
65
  seq = '';
59
66
  continue;
60
67
  }
61
- yield char;
68
+ yield* normalize(char);
62
69
  }
63
70
  }
64
71
  exports.splitMapSubstringsIterable = splitMapSubstringsIterable;
@@ -1,9 +1,10 @@
1
1
  export { consolidate } from './consolidate';
2
2
  export { createWeightedMap, editDistance, editDistanceWeighted } from './distance';
3
3
  export type { WeightMap } from './distance';
4
- export type { SuggestionCostMapDef } from './models/suggestionCostsDef';
5
4
  export type { FindFullResult } from './find';
6
5
  export { ExportOptions, importTrie, serializeTrie } from './io/importExport';
6
+ export { mapDictionaryInformationToWeightMap } from './mappers/mapDictionaryInfoToWeightMap';
7
+ export type { SuggestionCostMapDef } from './models/suggestionCostsDef';
7
8
  export { parseDictionary, parseDictionaryLines } from './SimpleDictionaryParser';
8
9
  export { MaxCost, suggestionCollector, SuggestionCollector, SuggestionResult } from './suggestCollector';
9
10
  export { CASE_INSENSITIVE_PREFIX, COMPOUND, COMPOUND_FIX, defaultTrieOptions, FORBID, FORBID_PREFIX, NORMALIZED, OPTIONAL_COMPOUND, OPTIONAL_COMPOUND_FIX, Trie, } from './trie';
package/dist/lib/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.WORD_SEPARATOR = exports.walker = exports.JOIN_SEPARATOR = exports.hintedWalker = exports.CompoundWordsMethod = exports.FLAG_WORD = exports.ChildMap = exports.TrieBuilder = exports.buildTrieFast = exports.buildTrie = exports.walk = exports.trieNodeToRoot = exports.orderTrie = exports.normalizeWordToLowercase = exports.normalizeWordForCaseInsensitive = exports.normalizeWord = exports.mergeOptionalWithDefaults = exports.mergeDefaults = exports.iteratorTrieWords = exports.iterateTrie = exports.isWordTerminationNode = exports.isDefined = exports.isCircular = exports.insert = exports.has = exports.findNode = exports.createTriFromList = exports.createTrieRoot = exports.countWords = exports.countNodes = exports.Trie = exports.OPTIONAL_COMPOUND_FIX = exports.OPTIONAL_COMPOUND = exports.NORMALIZED = exports.FORBID_PREFIX = exports.FORBID = exports.defaultTrieOptions = exports.COMPOUND_FIX = exports.COMPOUND = exports.CASE_INSENSITIVE_PREFIX = exports.suggestionCollector = exports.parseDictionaryLines = exports.parseDictionary = exports.serializeTrie = exports.importTrie = exports.editDistanceWeighted = exports.editDistance = exports.createWeightedMap = exports.consolidate = void 0;
3
+ exports.WORD_SEPARATOR = exports.walker = exports.JOIN_SEPARATOR = exports.hintedWalker = exports.CompoundWordsMethod = exports.FLAG_WORD = exports.ChildMap = exports.TrieBuilder = exports.buildTrieFast = exports.buildTrie = exports.walk = exports.trieNodeToRoot = exports.orderTrie = exports.normalizeWordToLowercase = exports.normalizeWordForCaseInsensitive = exports.normalizeWord = exports.mergeOptionalWithDefaults = exports.mergeDefaults = exports.iteratorTrieWords = exports.iterateTrie = exports.isWordTerminationNode = exports.isDefined = exports.isCircular = exports.insert = exports.has = exports.findNode = exports.createTriFromList = exports.createTrieRoot = exports.countWords = exports.countNodes = exports.Trie = exports.OPTIONAL_COMPOUND_FIX = exports.OPTIONAL_COMPOUND = exports.NORMALIZED = exports.FORBID_PREFIX = exports.FORBID = exports.defaultTrieOptions = exports.COMPOUND_FIX = exports.COMPOUND = exports.CASE_INSENSITIVE_PREFIX = exports.suggestionCollector = exports.parseDictionaryLines = exports.parseDictionary = exports.mapDictionaryInformationToWeightMap = exports.serializeTrie = exports.importTrie = exports.editDistanceWeighted = exports.editDistance = exports.createWeightedMap = exports.consolidate = void 0;
4
4
  var consolidate_1 = require("./consolidate");
5
5
  Object.defineProperty(exports, "consolidate", { enumerable: true, get: function () { return consolidate_1.consolidate; } });
6
6
  var distance_1 = require("./distance");
@@ -10,6 +10,8 @@ Object.defineProperty(exports, "editDistanceWeighted", { enumerable: true, get:
10
10
  var importExport_1 = require("./io/importExport");
11
11
  Object.defineProperty(exports, "importTrie", { enumerable: true, get: function () { return importExport_1.importTrie; } });
12
12
  Object.defineProperty(exports, "serializeTrie", { enumerable: true, get: function () { return importExport_1.serializeTrie; } });
13
+ var mapDictionaryInfoToWeightMap_1 = require("./mappers/mapDictionaryInfoToWeightMap");
14
+ Object.defineProperty(exports, "mapDictionaryInformationToWeightMap", { enumerable: true, get: function () { return mapDictionaryInfoToWeightMap_1.mapDictionaryInformationToWeightMap; } });
13
15
  var SimpleDictionaryParser_1 = require("./SimpleDictionaryParser");
14
16
  Object.defineProperty(exports, "parseDictionary", { enumerable: true, get: function () { return SimpleDictionaryParser_1.parseDictionary; } });
15
17
  Object.defineProperty(exports, "parseDictionaryLines", { enumerable: true, get: function () { return SimpleDictionaryParser_1.parseDictionaryLines; } });
@@ -0,0 +1,6 @@
1
+ import { HunspellCosts, EditCosts } from '../models/DictionaryInformation';
2
+ export declare type EditCostsRequired = Required<EditCosts>;
3
+ export declare type HunspellCostsRequired = Required<HunspellCosts>;
4
+ export declare function mapHunspellCosts(costs?: HunspellCosts): HunspellCostsRequired;
5
+ export declare function mapEditCosts(costs?: EditCosts): EditCostsRequired;
6
+ //# sourceMappingURL=mapCosts.d.ts.map
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapEditCosts = exports.mapHunspellCosts = void 0;
4
+ const util_1 = require("../utils/util");
5
+ const defaultEditCosts = {
6
+ accentCosts: 1,
7
+ baseCost: 100,
8
+ capsCosts: 1,
9
+ firstLetterPenalty: 4,
10
+ nonAlphabetCosts: 110,
11
+ };
12
+ const defaultHunspellCosts = {
13
+ ...defaultEditCosts,
14
+ ioConvertCost: 30,
15
+ keyboardCost: 99,
16
+ mapCost: 25,
17
+ replaceCosts: 75,
18
+ tryCharCost: 100,
19
+ };
20
+ function mapHunspellCosts(costs = {}) {
21
+ return { ...defaultHunspellCosts, ...(0, util_1.cleanCopy)(costs) };
22
+ }
23
+ exports.mapHunspellCosts = mapHunspellCosts;
24
+ function mapEditCosts(costs = {}) {
25
+ return { ...defaultEditCosts, ...(0, util_1.cleanCopy)(costs) };
26
+ }
27
+ exports.mapEditCosts = mapEditCosts;
28
+ //# sourceMappingURL=mapCosts.js.map
@@ -0,0 +1,4 @@
1
+ import type { DictionaryInformation } from '../models/DictionaryInformation';
2
+ import type { SuggestionCostMapDef } from '../models/suggestionCostsDef';
3
+ export declare function mapDictionaryInformation(dictInfo: DictionaryInformation): SuggestionCostMapDef[];
4
+ //# sourceMappingURL=mapDictionaryInfo.d.ts.map
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mapDictionaryInformation = void 0;
4
+ const cspell_pipe_1 = require("@cspell/cspell-pipe");
5
+ const locale_1 = require("../models/locale");
6
+ const util_1 = require("../utils/util");
7
+ const mapCosts_1 = require("./mapCosts");
8
+ const mapHunspellInformation_1 = require("./mapHunspellInformation");
9
+ const mapToSuggestionCostDef_1 = require("./mapToSuggestionCostDef");
10
+ function mapDictionaryInformation(dictInfo) {
11
+ const _locale = dictInfo.locale;
12
+ const locale = _locale ? (0, locale_1.parseLocale)(_locale).filter((loc) => loc.isValid()) : undefined;
13
+ const locales = locale === null || locale === void 0 ? void 0 : locale.map((loc) => loc.locale);
14
+ const costs = (0, mapCosts_1.mapEditCosts)(dictInfo.costs);
15
+ const defsEC = dictInfo.suggestionEditCosts || [];
16
+ const defsHI = dictInfo.hunspellInformation
17
+ ? (0, mapHunspellInformation_1.hunspellInformationToSuggestionCostDef)(dictInfo.hunspellInformation, locale)
18
+ : [];
19
+ return [
20
+ ...defsEC,
21
+ ...processAlphabet(dictInfo.alphabet, locales, costs),
22
+ ...processAccents(dictInfo.accents, costs),
23
+ ...defsHI,
24
+ ];
25
+ }
26
+ exports.mapDictionaryInformation = mapDictionaryInformation;
27
+ function processAlphabet(alphabet, locale, editCost) {
28
+ const csAlphabet = toCharSets(alphabet, 'a-zA-Z', editCost.baseCost);
29
+ return [
30
+ ...(0, cspell_pipe_1.pipeSync)(csAlphabet, (0, cspell_pipe_1.opMap)((cs) => (0, mapToSuggestionCostDef_1.parseAlphabet)(cs, locale, editCost)), (0, cspell_pipe_1.opFlatten)()),
31
+ ...(0, mapToSuggestionCostDef_1.calcFirstCharacterReplaceDefs)(csAlphabet, editCost),
32
+ ];
33
+ }
34
+ function toCharSets(cs, defaultValue, cost, penalty) {
35
+ cs = cs !== null && cs !== void 0 ? cs : defaultValue;
36
+ if (!cs)
37
+ return [];
38
+ if (typeof cs === 'string') {
39
+ cs = [
40
+ {
41
+ characters: cs,
42
+ cost,
43
+ },
44
+ ];
45
+ }
46
+ if (penalty !== undefined) {
47
+ cs.forEach((cs) => (cs.penalty = penalty));
48
+ }
49
+ return cs;
50
+ }
51
+ function processAccents(accents, editCost) {
52
+ const cs = toCharSets(accents, '\u0300-\u0341', editCost.accentCosts);
53
+ return cs.map((cs) => (0, mapToSuggestionCostDef_1.parseAccents)(cs, editCost)).filter(util_1.isDefined);
54
+ }
55
+ //# sourceMappingURL=mapDictionaryInfo.js.map
@@ -1,26 +1,5 @@
1
1
  import { WeightMap } from '../distance/weightedMaps';
2
- import type { DictionaryInformation, HunspellCosts, HunspellInformation } from '../models/DictionaryInformation';
3
- import type { SuggestionCostMapDef } from '../models/suggestionCostsDef';
4
- declare type Costs = Required<HunspellCosts>;
5
- export declare function dictionaryInformationToWeightMap(dictInfo: DictionaryInformation): WeightMap;
6
- export declare function hunspellInformationToSuggestionCostDef(hunInfo: HunspellInformation): SuggestionCostMapDef[];
7
- declare function calcCosts(costs?: HunspellCosts): Costs;
8
- declare function affMap(line: string, costs: Costs): SuggestionCostMapDef | undefined;
9
- declare function affTry(line: string, costs: Costs): SuggestionCostMapDef | undefined;
10
- declare function affTryFirstCharacterReplace(line: string, costs: Costs): SuggestionCostMapDef | undefined;
11
- declare function affNoTry(line: string, costs: Costs): SuggestionCostMapDef | undefined;
12
- declare function affRepConv(line: string, costs: Costs): SuggestionCostMapDef | undefined;
13
- declare function affKey(line: string, costs: Costs): SuggestionCostMapDef | undefined;
14
- declare function split(map: string): Iterable<string>;
15
- export declare const __testing__: {
16
- affKey: typeof affKey;
17
- affMap: typeof affMap;
18
- affNoTry: typeof affNoTry;
19
- affRepConv: typeof affRepConv;
20
- affTry: typeof affTry;
21
- affTryFirstCharacterReplace: typeof affTryFirstCharacterReplace;
22
- split: typeof split;
23
- calcCosts: typeof calcCosts;
24
- };
25
- export {};
2
+ import type { DictionaryInformation } from '../models/DictionaryInformation';
3
+ export declare function mapDictionaryInformationToWeightMap(dictInfo: DictionaryInformation): WeightMap;
4
+ export declare const __testing__: {};
26
5
  //# sourceMappingURL=mapDictionaryInfoToWeightMap.d.ts.map
@@ -1,175 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.__testing__ = exports.hunspellInformationToSuggestionCostDef = exports.dictionaryInformationToWeightMap = void 0;
3
+ exports.__testing__ = exports.mapDictionaryInformationToWeightMap = void 0;
4
4
  const weightedMaps_1 = require("../distance/weightedMaps");
5
- const util_1 = require("../utils/util");
6
- function dictionaryInformationToWeightMap(dictInfo) {
7
- const defsEC = dictInfo.suggestionEditCosts || [];
8
- const defsHI = dictInfo.hunspellInformation
9
- ? hunspellInformationToSuggestionCostDef(dictInfo.hunspellInformation)
10
- : [];
11
- return (0, weightedMaps_1.createWeightMap)(...defsEC, ...defsHI);
12
- }
13
- exports.dictionaryInformationToWeightMap = dictionaryInformationToWeightMap;
14
- function hunspellInformationToSuggestionCostDef(hunInfo) {
15
- const defs = [];
16
- const costs = calcCosts(hunInfo.costs);
17
- function parseAff(aff, costs) {
18
- // cspell:ignore OCONV
19
- const regSupportedAff = /^(?:MAP|KEY|TRY|NO-TRY|ICONV|OCONV|REP)\s/;
20
- const rejectAff = /^(?:MAP|KEY|TRY|ICONV|OCONV|REP)\s+\d+$/;
21
- const lines = aff
22
- .split('\n')
23
- .map((a) => a.replace(/#.*/, ''))
24
- .map((a) => a.trim())
25
- .filter((a) => regSupportedAff.test(a))
26
- .filter((a) => !rejectAff.test(a));
27
- lines.forEach((line) => {
28
- [affMap, affNoTry, affRepConv, affTry, affTryFirstCharacterReplace, affKey]
29
- .map((fn) => fn(line, costs))
30
- .filter(util_1.isDefined)
31
- .forEach((def) => defs.push(def));
32
- });
33
- }
34
- parseAff(hunInfo.aff, costs);
35
- return defs;
36
- }
37
- exports.hunspellInformationToSuggestionCostDef = hunspellInformationToSuggestionCostDef;
38
- function calcCosts(costs = {}) {
39
- const { firstLetterPenalty = 4, ioConvertCost = 30, keyboardCost = 94, mapCost = 25, replaceCosts = 75, tryCharCost = 95, } = costs;
40
- const c = { tryCharCost, keyboardCost, mapCost, ioConvertCost, replaceCosts, firstLetterPenalty };
41
- return c;
42
- }
43
- const regExpMap = /^(?:MAP)\s+(\S+)$/;
44
- function affMap(line, costs) {
45
- const m = line.match(regExpMap);
46
- if (!m)
47
- return undefined;
48
- const map = m[1];
49
- const cost = costs.mapCost;
50
- return {
51
- map,
52
- replace: cost,
53
- swap: cost,
54
- };
55
- }
56
- const regExpTry = /^(?:TRY)\s+(\S+)$/;
57
- function affTry(line, costs) {
58
- const m = line.match(regExpTry);
59
- if (!m)
60
- return undefined;
61
- const map = m[1];
62
- const cost = costs.tryCharCost;
63
- return {
64
- map,
65
- insDel: cost,
66
- replace: cost,
67
- swap: cost,
68
- };
69
- }
70
- function affTryFirstCharacterReplace(line, costs) {
71
- const m = line.match(regExpTry);
72
- if (!m)
73
- return undefined;
74
- const map = [...split(m[1])].map((char) => `(^${char})`).join('');
75
- const cost = costs.tryCharCost;
76
- const penalty = costs.firstLetterPenalty;
77
- return {
78
- map,
79
- replace: cost - penalty,
80
- penalty,
81
- };
82
- }
83
- const regExpNoTry = /^NO-TRY\s+(\S+)$/;
84
- function affNoTry(line, costs) {
85
- const m = line.match(regExpNoTry);
86
- if (!m)
87
- return undefined;
88
- const map = m[1];
89
- return {
90
- map,
91
- insDel: Math.max(100 - costs.tryCharCost, 0),
92
- penalty: 100 + costs.tryCharCost,
93
- };
94
- }
95
- // cspell:ignore conv
96
- const regExpRepConv = /^(?:REP|(?:I|O)CONV)\s+(\S+)\s+(\S+)$/;
97
- function affRepConv(line, costs) {
98
- const m = line.match(regExpRepConv);
99
- if (!m)
100
- return undefined;
101
- const cost = line.startsWith('REP') ? costs.replaceCosts : costs.ioConvertCost;
102
- const from = m[1];
103
- let into = m[2];
104
- into = into.replace(/^0$/, '');
105
- if (from.startsWith('^') && !into.startsWith('^')) {
106
- into = '^' + into;
107
- }
108
- if (from.endsWith('$') && !into.endsWith('$')) {
109
- into = into + '$';
110
- }
111
- return {
112
- map: `(${from})(${into})`,
113
- replace: cost,
114
- };
115
- }
116
- const regExpKey = /^(?:KEY)\s+(\S+)$/;
117
- function affKey(line, costs) {
118
- const m = line.match(regExpKey);
119
- if (!m)
120
- return undefined;
121
- const kbd = m[1];
122
- const pairs = [...split(kbd)]
123
- .map(reducer((p, v) => ({ a: p.b, b: v }), { a: '|', b: '|' }))
124
- .filter((ab) => ab.a !== '|' && ab.b !== '|')
125
- .map((ab) => ab.a + ab.b);
126
- const map = pairs.join('|');
127
- const cost = costs.keyboardCost;
128
- return {
129
- map,
130
- replace: cost,
131
- swap: cost,
132
- };
133
- }
134
- function* split(map) {
135
- let seq = '';
136
- let mode = 0;
137
- for (const char of map) {
138
- if (mode && char === ')') {
139
- yield seq;
140
- mode = 0;
141
- continue;
142
- }
143
- if (mode) {
144
- seq += char;
145
- continue;
146
- }
147
- if (char === '(') {
148
- mode = 1;
149
- seq = '';
150
- continue;
151
- }
152
- yield char;
153
- }
154
- }
155
- function reducer(fn, initialVal) {
156
- let acc = initialVal;
157
- return (val, i) => (acc = fn(acc, val, i));
158
- }
159
- // function pipe<T>(v: T, ...fns: ((v: T) => T)[]): T {
160
- // for (const fn of fns) {
161
- // v = fn(v);
162
- // }
163
- // return v;
164
- // }
165
- exports.__testing__ = {
166
- affKey,
167
- affMap,
168
- affNoTry,
169
- affRepConv,
170
- affTry,
171
- affTryFirstCharacterReplace,
172
- split,
173
- calcCosts,
174
- };
5
+ const mapDictionaryInfo_1 = require("./mapDictionaryInfo");
6
+ const defaultDefs = [
7
+ {
8
+ map: '1234567890-.',
9
+ insDel: 1,
10
+ penalty: 200,
11
+ },
12
+ ];
13
+ function mapDictionaryInformationToWeightMap(dictInfo) {
14
+ const defs = (0, mapDictionaryInfo_1.mapDictionaryInformation)(dictInfo).concat(defaultDefs);
15
+ return (0, weightedMaps_1.createWeightMap)(...defs);
16
+ }
17
+ exports.mapDictionaryInformationToWeightMap = mapDictionaryInformationToWeightMap;
18
+ exports.__testing__ = {};
175
19
  //# sourceMappingURL=mapDictionaryInfoToWeightMap.js.map
@@ -0,0 +1,42 @@
1
+ import type { HunspellCosts, HunspellInformation } from '../models/DictionaryInformation';
2
+ import { Locale } from '../models/locale';
3
+ import type { SuggestionCostMapDef } from '../models/suggestionCostsDef';
4
+ import { splitMap } from './mapToSuggestionCostDef';
5
+ interface Costs extends Required<HunspellCosts> {
6
+ locale?: string[] | undefined;
7
+ }
8
+ export declare function hunspellInformationToSuggestionCostDef(hunInfo: HunspellInformation, locales: Locale[] | undefined): SuggestionCostMapDef[];
9
+ declare function calcCosts(costs?: HunspellCosts, locale?: Locale[] | undefined): Costs;
10
+ declare function affMap(line: string, costs: Costs): SuggestionCostMapDef | undefined;
11
+ declare function affTry(line: string, costs: Costs): SuggestionCostMapDef[] | undefined;
12
+ declare function affTryFirstCharacterReplace(line: string, costs: Costs): SuggestionCostMapDef | undefined;
13
+ declare function affNoTry(line: string, costs: Costs): SuggestionCostMapDef | undefined;
14
+ declare function affRepConv(line: string, costs: Costs): SuggestionCostMapDef | undefined;
15
+ declare function affKey(line: string, costs: Costs): SuggestionCostMapDef | undefined;
16
+ declare function affKeyCaps(line: string, costs: Costs): SuggestionCostMapDef | undefined;
17
+ declare function affMapCaps(line: string, costs: Costs): SuggestionCostMapDef | undefined;
18
+ declare function affTryAccents(line: string, costs: Costs): SuggestionCostMapDef[] | undefined;
19
+ declare function affMapAccents(line: string, costs: Costs): SuggestionCostMapDef[] | undefined;
20
+ /**
21
+ * Bring letters / strings together.
22
+ * - `['a', 'b'] => 'ab'`
23
+ * - `['a', 'bc'] => 'a(bc)'`
24
+ * @param letters - letters to join
25
+ */
26
+ export declare function joinLetters(letters: string[]): string;
27
+ export declare const __testing__: {
28
+ affKey: typeof affKey;
29
+ affKeyCaps: typeof affKeyCaps;
30
+ affMap: typeof affMap;
31
+ affMapAccents: typeof affMapAccents;
32
+ affMapCaps: typeof affMapCaps;
33
+ affNoTry: typeof affNoTry;
34
+ affRepConv: typeof affRepConv;
35
+ affTry: typeof affTry;
36
+ affTryAccents: typeof affTryAccents;
37
+ affTryFirstCharacterReplace: typeof affTryFirstCharacterReplace;
38
+ calcCosts: typeof calcCosts;
39
+ split: typeof splitMap;
40
+ };
41
+ export {};
42
+ //# sourceMappingURL=mapHunspellInformation.d.ts.map