cspell-trie-lib 6.28.0 → 6.29.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 (129) hide show
  1. package/package.json +6 -4
  2. package/dist/esm/index.d.ts +0 -2
  3. package/dist/esm/index.js +0 -1
  4. package/dist/esm/lib/SimpleDictionaryParser.d.ts +0 -73
  5. package/dist/esm/lib/SimpleDictionaryParser.js +0 -191
  6. package/dist/esm/lib/TrieBuilder.d.ts +0 -46
  7. package/dist/esm/lib/TrieBuilder.js +0 -214
  8. package/dist/esm/lib/TrieNode.d.ts +0 -18
  9. package/dist/esm/lib/TrieNode.js +0 -3
  10. package/dist/esm/lib/compoundWalker.d.ts +0 -29
  11. package/dist/esm/lib/compoundWalker.js +0 -56
  12. package/dist/esm/lib/consolidate.d.ts +0 -7
  13. package/dist/esm/lib/consolidate.js +0 -90
  14. package/dist/esm/lib/constants.d.ts +0 -9
  15. package/dist/esm/lib/constants.js +0 -11
  16. package/dist/esm/lib/convertToTrieRefNodes.d.ts +0 -8
  17. package/dist/esm/lib/convertToTrieRefNodes.js +0 -83
  18. package/dist/esm/lib/distance/distance.d.ts +0 -35
  19. package/dist/esm/lib/distance/distance.js +0 -42
  20. package/dist/esm/lib/distance/distanceAStar.d.ts +0 -7
  21. package/dist/esm/lib/distance/distanceAStar.js +0 -53
  22. package/dist/esm/lib/distance/distanceAStarWeighted.d.ts +0 -21
  23. package/dist/esm/lib/distance/distanceAStarWeighted.js +0 -136
  24. package/dist/esm/lib/distance/formatResultEx.d.ts +0 -5
  25. package/dist/esm/lib/distance/formatResultEx.js +0 -53
  26. package/dist/esm/lib/distance/index.d.ts +0 -3
  27. package/dist/esm/lib/distance/index.js +0 -1
  28. package/dist/esm/lib/distance/levenshtein.d.ts +0 -9
  29. package/dist/esm/lib/distance/levenshtein.js +0 -43
  30. package/dist/esm/lib/distance/weightedMaps.d.ts +0 -98
  31. package/dist/esm/lib/distance/weightedMaps.js +0 -289
  32. package/dist/esm/lib/find.d.ts +0 -71
  33. package/dist/esm/lib/find.js +0 -282
  34. package/dist/esm/lib/flatten.d.ts +0 -9
  35. package/dist/esm/lib/flatten.js +0 -108
  36. package/dist/esm/lib/index.d.ts +0 -23
  37. package/dist/esm/lib/index.js +0 -16
  38. package/dist/esm/lib/io/importExport.d.ts +0 -17
  39. package/dist/esm/lib/io/importExport.js +0 -59
  40. package/dist/esm/lib/io/importExportV1.d.ts +0 -16
  41. package/dist/esm/lib/io/importExportV1.js +0 -121
  42. package/dist/esm/lib/io/importExportV2.d.ts +0 -16
  43. package/dist/esm/lib/io/importExportV2.js +0 -168
  44. package/dist/esm/lib/io/importExportV3.d.ts +0 -23
  45. package/dist/esm/lib/io/importExportV3.js +0 -281
  46. package/dist/esm/lib/io/importExportV4.d.ts +0 -52
  47. package/dist/esm/lib/io/importExportV4.js +0 -439
  48. package/dist/esm/lib/mappers/joinLetters.d.ts +0 -8
  49. package/dist/esm/lib/mappers/joinLetters.js +0 -10
  50. package/dist/esm/lib/mappers/mapCosts.d.ts +0 -6
  51. package/dist/esm/lib/mappers/mapCosts.js +0 -22
  52. package/dist/esm/lib/mappers/mapDictionaryInfo.d.ts +0 -6
  53. package/dist/esm/lib/mappers/mapDictionaryInfo.js +0 -63
  54. package/dist/esm/lib/mappers/mapDictionaryInfoToWeightMap.d.ts +0 -5
  55. package/dist/esm/lib/mappers/mapDictionaryInfoToWeightMap.js +0 -34
  56. package/dist/esm/lib/mappers/mapHunspellInformation.d.ts +0 -35
  57. package/dist/esm/lib/mappers/mapHunspellInformation.js +0 -195
  58. package/dist/esm/lib/mappers/mapToSuggestionCostDef.d.ts +0 -17
  59. package/dist/esm/lib/mappers/mapToSuggestionCostDef.js +0 -112
  60. package/dist/esm/lib/models/DictionaryInformation.d.ts +0 -8
  61. package/dist/esm/lib/models/DictionaryInformation.js +0 -1
  62. package/dist/esm/lib/models/locale/index.d.ts +0 -2
  63. package/dist/esm/lib/models/locale/index.js +0 -1
  64. package/dist/esm/lib/models/locale/knownLocales.d.ts +0 -3
  65. package/dist/esm/lib/models/locale/knownLocales.js +0 -533
  66. package/dist/esm/lib/models/locale/locale.d.ts +0 -24
  67. package/dist/esm/lib/models/locale/locale.js +0 -60
  68. package/dist/esm/lib/models/suggestionCostsDef.d.ts +0 -2
  69. package/dist/esm/lib/models/suggestionCostsDef.js +0 -1
  70. package/dist/esm/lib/suggest.d.ts +0 -5
  71. package/dist/esm/lib/suggest.js +0 -4
  72. package/dist/esm/lib/suggestCollector.d.ts +0 -6
  73. package/dist/esm/lib/suggestCollector.js +0 -1
  74. package/dist/esm/lib/suggestions/constants.d.ts +0 -2
  75. package/dist/esm/lib/suggestions/constants.js +0 -1
  76. package/dist/esm/lib/suggestions/genSuggestionsOptions.d.ts +0 -58
  77. package/dist/esm/lib/suggestions/genSuggestionsOptions.js +0 -44
  78. package/dist/esm/lib/suggestions/orthography.d.ts +0 -13
  79. package/dist/esm/lib/suggestions/orthography.js +0 -65
  80. package/dist/esm/lib/suggestions/suggest.d.ts +0 -7
  81. package/dist/esm/lib/suggestions/suggest.js +0 -195
  82. package/dist/esm/lib/suggestions/suggestAStar.d.ts +0 -7
  83. package/dist/esm/lib/suggestions/suggestAStar.js +0 -415
  84. package/dist/esm/lib/suggestions/suggestCollector.d.ts +0 -118
  85. package/dist/esm/lib/suggestions/suggestCollector.js +0 -205
  86. package/dist/esm/lib/trie-util.d.ts +0 -27
  87. package/dist/esm/lib/trie-util.js +0 -152
  88. package/dist/esm/lib/trie.d.ts +0 -103
  89. package/dist/esm/lib/trie.js +0 -211
  90. package/dist/esm/lib/trieRef.d.ts +0 -6
  91. package/dist/esm/lib/trieRef.js +0 -1
  92. package/dist/esm/lib/types.d.ts +0 -70
  93. package/dist/esm/lib/types.js +0 -13
  94. package/dist/esm/lib/utils/PairingHeap.d.ts +0 -42
  95. package/dist/esm/lib/utils/PairingHeap.js +0 -85
  96. package/dist/esm/lib/utils/autoCacheMap.d.ts +0 -11
  97. package/dist/esm/lib/utils/autoCacheMap.js +0 -28
  98. package/dist/esm/lib/utils/bufferLines.d.ts +0 -3
  99. package/dist/esm/lib/utils/bufferLines.js +0 -26
  100. package/dist/esm/lib/utils/clean.d.ts +0 -3
  101. package/dist/esm/lib/utils/clean.js +0 -9
  102. package/dist/esm/lib/utils/isDefined.d.ts +0 -2
  103. package/dist/esm/lib/utils/isDefined.js +0 -3
  104. package/dist/esm/lib/utils/memorizeLastCall.d.ts +0 -2
  105. package/dist/esm/lib/utils/memorizeLastCall.js +0 -13
  106. package/dist/esm/lib/utils/memorizer.d.ts +0 -2
  107. package/dist/esm/lib/utils/memorizer.js +0 -37
  108. package/dist/esm/lib/utils/mergeDefaults.d.ts +0 -10
  109. package/dist/esm/lib/utils/mergeDefaults.js +0 -19
  110. package/dist/esm/lib/utils/mergeOptionalWithDefaults.d.ts +0 -3
  111. package/dist/esm/lib/utils/mergeOptionalWithDefaults.js +0 -5
  112. package/dist/esm/lib/utils/normalizeWord.d.ts +0 -20
  113. package/dist/esm/lib/utils/normalizeWord.js +0 -22
  114. package/dist/esm/lib/utils/secondChanceCache.d.ts +0 -16
  115. package/dist/esm/lib/utils/secondChanceCache.js +0 -55
  116. package/dist/esm/lib/utils/text.d.ts +0 -53
  117. package/dist/esm/lib/utils/text.js +0 -101
  118. package/dist/esm/lib/utils/timer.d.ts +0 -15
  119. package/dist/esm/lib/utils/timer.js +0 -22
  120. package/dist/esm/lib/utils/util.d.ts +0 -38
  121. package/dist/esm/lib/utils/util.js +0 -71
  122. package/dist/esm/lib/walker/hintedWalker.d.ts +0 -23
  123. package/dist/esm/lib/walker/hintedWalker.js +0 -103
  124. package/dist/esm/lib/walker/index.d.ts +0 -5
  125. package/dist/esm/lib/walker/index.js +0 -3
  126. package/dist/esm/lib/walker/walker.d.ts +0 -9
  127. package/dist/esm/lib/walker/walker.js +0 -38
  128. package/dist/esm/lib/walker/walkerTypes.d.ts +0 -24
  129. package/dist/esm/lib/walker/walkerTypes.js +0 -17
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cspell-trie-lib",
3
- "version": "6.28.0",
3
+ "version": "6.29.0",
4
4
  "description": "Trie Data Structure to support cspell.",
5
5
  "type": "commonjs",
6
6
  "main": "dist/cjs/index.js",
@@ -14,6 +14,8 @@
14
14
  },
15
15
  "files": [
16
16
  "dist",
17
+ "!dist/esm/**/*.js",
18
+ "!dist/esm/**/*.ts",
17
19
  "!**/*.tsbuildInfo",
18
20
  "!**/__mocks__",
19
21
  "!**/test/**",
@@ -48,8 +50,8 @@
48
50
  },
49
51
  "homepage": "https://github.com/streetsidesoftware/cspell#readme",
50
52
  "dependencies": {
51
- "@cspell/cspell-pipe": "6.28.0",
52
- "@cspell/cspell-types": "6.28.0",
53
+ "@cspell/cspell-pipe": "6.29.0",
54
+ "@cspell/cspell-types": "6.29.0",
53
55
  "gensequence": "^5.0.2"
54
56
  },
55
57
  "engines": {
@@ -59,5 +61,5 @@
59
61
  "@cspell/dict-en_us": "^3.0.0",
60
62
  "@cspell/dict-es-es": "^2.2.4"
61
63
  },
62
- "gitHead": "1c314413e76908e5fbf61fd2555726112b177c0e"
64
+ "gitHead": "2eabb1c47c12c2a42eb95d30329be6f544ee2ffc"
63
65
  }
@@ -1,2 +0,0 @@
1
- export * from './lib/index.js';
2
- //# sourceMappingURL=index.d.ts.map
package/dist/esm/index.js DELETED
@@ -1 +0,0 @@
1
- export * from './lib/index.js';
@@ -1,73 +0,0 @@
1
- import { type Operator } from '@cspell/cspell-pipe/sync';
2
- import type { Trie } from './trie.js';
3
- export interface ParseDictionaryOptions {
4
- compoundCharacter: string;
5
- optionalCompoundCharacter: string;
6
- forbiddenPrefix: string;
7
- caseInsensitivePrefix: string;
8
- /**
9
- * Start of a single-line comment.
10
- * @default "#"
11
- */
12
- commentCharacter: string;
13
- /**
14
- * If word starts with prefix, do not strip case or accents.
15
- * @default false;
16
- */
17
- keepExactPrefix: string;
18
- /**
19
- * Tell the parser to automatically create case / accent insensitive forms.
20
- * @default true
21
- */
22
- stripCaseAndAccents: boolean;
23
- /**
24
- * Tell the parser to keep non-case/accent version in both forms.
25
- * @default false
26
- */
27
- stripCaseAndAccentsKeepDuplicate: boolean;
28
- /**
29
- * Tell the parser to keep non-case/accent version in both forms.
30
- * @default false
31
- */
32
- stripCaseAndAccentsOnForbidden: boolean;
33
- /**
34
- * Tell the parser to split into words along spaces.
35
- * @default false
36
- */
37
- split: boolean;
38
- /**
39
- * When splitting tells the parser to output both the split and non-split versions of the line.
40
- * @default false
41
- */
42
- splitKeepBoth: boolean;
43
- /**
44
- * Specify the separator for splitting words.
45
- */
46
- splitSeparator: RegExp | string;
47
- }
48
- export declare const defaultParseDictionaryOptions: ParseDictionaryOptions;
49
- export declare const cSpellToolDirective = "cspell-dictionary:";
50
- export declare const setOfCSpellDirectiveFlags: string[];
51
- /**
52
- * Normalizes a dictionary words based upon prefix / suffixes.
53
- * Case insensitive versions are also generated.
54
- * @param options - defines prefixes used when parsing lines.
55
- * @returns words that have been normalized.
56
- */
57
- export declare function createDictionaryLineParserMapper(options?: Partial<ParseDictionaryOptions>): Operator<string>;
58
- /**
59
- * Normalizes a dictionary words based upon prefix / suffixes.
60
- * Case insensitive versions are also generated.
61
- * @param lines - one word per line
62
- * @param _options - defines prefixes used when parsing lines.
63
- * @returns words that have been normalized.
64
- */
65
- export declare function parseDictionaryLines(lines: Iterable<string> | string, options?: Partial<ParseDictionaryOptions>): Iterable<string>;
66
- export declare function parseLinesToDictionary(lines: Iterable<string>, options?: Partial<ParseDictionaryOptions>): Trie;
67
- export declare function parseDictionary(text: string, options?: Partial<ParseDictionaryOptions>): Trie;
68
- declare function splitLine(line: string, regExp: RegExp | string): string[];
69
- export declare const __testing__: {
70
- splitLine: typeof splitLine;
71
- };
72
- export {};
73
- //# sourceMappingURL=SimpleDictionaryParser.d.ts.map
@@ -1,191 +0,0 @@
1
- import { opCombine as opPipe, opConcatMap, opFilter, opMap } from '@cspell/cspell-pipe/sync';
2
- import { CASE_INSENSITIVE_PREFIX, COMPOUND_FIX, FORBID_PREFIX, IDENTITY_PREFIX, LINE_COMMENT, OPTIONAL_COMPOUND_FIX, } from './constants.js';
3
- import { buildTrieFast } from './TrieBuilder.js';
4
- import { normalizeWord, normalizeWordForCaseInsensitive } from './utils/normalizeWord.js';
5
- const RegExpSplit = /[\s,;]/g;
6
- const _defaultOptions = {
7
- commentCharacter: LINE_COMMENT,
8
- optionalCompoundCharacter: OPTIONAL_COMPOUND_FIX,
9
- compoundCharacter: COMPOUND_FIX,
10
- forbiddenPrefix: FORBID_PREFIX,
11
- caseInsensitivePrefix: CASE_INSENSITIVE_PREFIX,
12
- keepExactPrefix: IDENTITY_PREFIX,
13
- stripCaseAndAccents: true,
14
- stripCaseAndAccentsKeepDuplicate: false,
15
- stripCaseAndAccentsOnForbidden: false,
16
- split: false,
17
- splitKeepBoth: false,
18
- splitSeparator: RegExpSplit,
19
- };
20
- export const defaultParseDictionaryOptions = Object.freeze(_defaultOptions);
21
- export const cSpellToolDirective = 'cspell-dictionary:';
22
- export const setOfCSpellDirectiveFlags = ['no-split', 'split', 'generate-alternatives', 'no-generate-alternatives'];
23
- /**
24
- * Normalizes a dictionary words based upon prefix / suffixes.
25
- * Case insensitive versions are also generated.
26
- * @param options - defines prefixes used when parsing lines.
27
- * @returns words that have been normalized.
28
- */
29
- export function createDictionaryLineParserMapper(options) {
30
- const _options = options || _defaultOptions;
31
- const { commentCharacter = _defaultOptions.commentCharacter, optionalCompoundCharacter: optionalCompound = _defaultOptions.optionalCompoundCharacter, compoundCharacter: compound = _defaultOptions.compoundCharacter, caseInsensitivePrefix: ignoreCase = _defaultOptions.caseInsensitivePrefix, forbiddenPrefix: forbidden = _defaultOptions.forbiddenPrefix, keepExactPrefix: keepCase = _defaultOptions.keepExactPrefix, splitSeparator = _defaultOptions.splitSeparator, splitKeepBoth = _defaultOptions.splitKeepBoth, stripCaseAndAccentsKeepDuplicate = _defaultOptions.stripCaseAndAccentsKeepDuplicate, stripCaseAndAccentsOnForbidden = _defaultOptions.stripCaseAndAccentsOnForbidden, } = _options;
32
- let { stripCaseAndAccents = _defaultOptions.stripCaseAndAccents, split = _defaultOptions.split } = _options;
33
- // console.log('options: %o', options);
34
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
- function isString(line) {
36
- return typeof line === 'string';
37
- }
38
- function trim(line) {
39
- return line.trim();
40
- }
41
- function removeComments(line) {
42
- const idx = line.indexOf(commentCharacter);
43
- if (idx < 0)
44
- return line;
45
- const idxDirective = line.indexOf(cSpellToolDirective, idx);
46
- if (idxDirective >= 0) {
47
- const flags = line
48
- .slice(idxDirective)
49
- .split(/[\s,;]/g)
50
- .map((s) => s.trim())
51
- .filter((a) => !!a);
52
- for (const flag of flags) {
53
- switch (flag) {
54
- case 'split':
55
- split = true;
56
- break;
57
- case 'no-split':
58
- split = false;
59
- break;
60
- case 'no-generate-alternatives':
61
- stripCaseAndAccents = false;
62
- break;
63
- case 'generate-alternatives':
64
- stripCaseAndAccents = true;
65
- break;
66
- }
67
- }
68
- }
69
- return line.slice(0, idx).trim();
70
- }
71
- function filterEmptyLines(line) {
72
- return !!line;
73
- }
74
- function* mapOptionalPrefix(line) {
75
- if (line[0] === optionalCompound) {
76
- const t = line.slice(1);
77
- yield t;
78
- yield compound + t;
79
- }
80
- else {
81
- yield line;
82
- }
83
- }
84
- function* mapOptionalSuffix(line) {
85
- if (line.slice(-1) === optionalCompound) {
86
- const t = line.slice(0, -1);
87
- yield t;
88
- yield t + compound;
89
- }
90
- else {
91
- yield line;
92
- }
93
- }
94
- const doNotNormalizePrefix = Object.create(null);
95
- [ignoreCase, keepCase, '"'].forEach((prefix) => (doNotNormalizePrefix[prefix] = true));
96
- if (!stripCaseAndAccentsOnForbidden) {
97
- doNotNormalizePrefix[forbidden] = true;
98
- }
99
- function removeDoublePrefix(w) {
100
- return w.startsWith(ignoreCase + ignoreCase) ? w.slice(1) : w;
101
- }
102
- function stripKeepCasePrefixAndQuotes(word) {
103
- word = word.replace(/"(.*?)"/g, '$1');
104
- return word[0] === keepCase ? word.slice(1) : word;
105
- }
106
- function _normalize(word) {
107
- return normalizeWord(stripKeepCasePrefixAndQuotes(word));
108
- }
109
- function* mapNormalize(word) {
110
- const nWord = _normalize(word);
111
- const forms = new Set();
112
- forms.add(nWord);
113
- if (stripCaseAndAccents && !(word[0] in doNotNormalizePrefix)) {
114
- for (const n of normalizeWordForCaseInsensitive(nWord)) {
115
- (stripCaseAndAccentsKeepDuplicate || n !== nWord) && forms.add(ignoreCase + n);
116
- }
117
- }
118
- yield* forms;
119
- }
120
- function* splitWords(lines) {
121
- for (const line of lines) {
122
- if (split) {
123
- const lineEscaped = line.indexOf('"') >= 0
124
- ? line.replace(/".*?"/g, (quoted) => ' ' + quoted.replace(/(\s)/g, '\\$1') + ' ')
125
- : line;
126
- const words = splitLine(lineEscaped, splitSeparator);
127
- yield* words.map((escaped) => escaped.replace(/\\/g, ''));
128
- if (!splitKeepBoth)
129
- continue;
130
- }
131
- yield line;
132
- }
133
- }
134
- function* splitLines(paragraphs) {
135
- for (const paragraph of paragraphs) {
136
- yield* paragraph.split('\n');
137
- }
138
- }
139
- const processLines = opPipe(opFilter(isString), splitLines, opMap(removeComments), splitWords, opMap(trim), opFilter(filterEmptyLines), opConcatMap(mapOptionalPrefix), opConcatMap(mapOptionalSuffix), opConcatMap(mapNormalize), opMap(removeDoublePrefix));
140
- return processLines;
141
- }
142
- /**
143
- * Normalizes a dictionary words based upon prefix / suffixes.
144
- * Case insensitive versions are also generated.
145
- * @param lines - one word per line
146
- * @param _options - defines prefixes used when parsing lines.
147
- * @returns words that have been normalized.
148
- */
149
- export function parseDictionaryLines(lines, options) {
150
- return createDictionaryLineParserMapper(options)(typeof lines === 'string' ? [lines] : lines);
151
- }
152
- export function parseLinesToDictionary(lines, options) {
153
- const _options = mergeOptions(_defaultOptions, options);
154
- const dictLines = parseDictionaryLines(lines, _options);
155
- return buildTrieFast([...new Set(dictLines)].sort(), {
156
- compoundCharacter: _options.compoundCharacter,
157
- forbiddenWordPrefix: _options.forbiddenPrefix,
158
- stripCaseAndAccentsPrefix: _options.caseInsensitivePrefix,
159
- });
160
- }
161
- export function parseDictionary(text, options) {
162
- return parseLinesToDictionary(text.split('\n'), options);
163
- }
164
- // function escapeRegEx(s: string) {
165
- // return s.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
166
- // }
167
- function mergeOptions(base, ...partials) {
168
- const opt = { ...base };
169
- for (const p of partials) {
170
- if (!p)
171
- continue;
172
- Object.assign(opt, p);
173
- }
174
- return opt;
175
- }
176
- const RegExpToEncode = /\\([\s,;])/g;
177
- const RegExpDecode = /<<(%[\da-f]{2})>>/gi;
178
- function encodeLine(line) {
179
- return line.replace(RegExpToEncode, (_, v) => '<<' + encodeURIComponent(v) + '>>');
180
- }
181
- function decodeLine(line) {
182
- return line.replace(RegExpDecode, (_, v) => '\\' + decodeURIComponent(v));
183
- }
184
- function splitLine(line, regExp) {
185
- return encodeLine(line)
186
- .split(regExp)
187
- .map((line) => decodeLine(line));
188
- }
189
- export const __testing__ = {
190
- splitLine,
191
- };
@@ -1,46 +0,0 @@
1
- import type { PartialTrieOptions, TrieOptions } from './trie.js';
2
- import { Trie } from './trie.js';
3
- /**
4
- * Builds an optimized Trie from a Iterable<string>. It attempts to reduce the size of the trie
5
- * by finding common endings.
6
- * @param words Iterable set of words -- no processing is done on the words, they are inserted as is.
7
- * @param trieOptions options for the Trie
8
- */
9
- export declare function buildTrie(words: Iterable<string>, trieOptions?: PartialTrieOptions): Trie;
10
- /**
11
- * Builds a Trie from a Iterable<string>. NO attempt a reducing the size of the Trie is done.
12
- * @param words Iterable set of words -- no processing is done on the words, they are inserted as is.
13
- * @param trieOptions options for the Trie
14
- */
15
- export declare function buildTrieFast(words: Iterable<string>, trieOptions?: PartialTrieOptions): Trie;
16
- export declare class TrieBuilder {
17
- private count;
18
- private readonly signatures;
19
- private readonly cached;
20
- private readonly transforms;
21
- private _eow;
22
- /** position 0 of lastPath is always the root */
23
- private lastPath;
24
- private tails;
25
- trieOptions: TrieOptions;
26
- constructor(words?: Iterable<string>, trieOptions?: PartialTrieOptions);
27
- private set _root(value);
28
- private get _root();
29
- private signature;
30
- private _canBeCached;
31
- private tryCacheFrozen;
32
- private freeze;
33
- private tryToCache;
34
- private storeTransform;
35
- private addChild;
36
- private buildTail;
37
- private _insert;
38
- insertWord(word: string): void;
39
- insert(words: Iterable<string>): void;
40
- /**
41
- * Resets the builder
42
- */
43
- reset(): void;
44
- build(consolidateSuffixes?: boolean): Trie;
45
- }
46
- //# sourceMappingURL=TrieBuilder.d.ts.map
@@ -1,214 +0,0 @@
1
- import { consolidate } from './consolidate.js';
2
- import { Trie } from './trie.js';
3
- import { createTrieRoot, createTriFromList, trieNodeToRoot } from './trie-util.js';
4
- import { mergeOptionalWithDefaults } from './utils/mergeOptionalWithDefaults.js';
5
- import { SecondChanceCache } from './utils/secondChanceCache.js';
6
- /**
7
- * Builds an optimized Trie from a Iterable<string>. It attempts to reduce the size of the trie
8
- * by finding common endings.
9
- * @param words Iterable set of words -- no processing is done on the words, they are inserted as is.
10
- * @param trieOptions options for the Trie
11
- */
12
- export function buildTrie(words, trieOptions) {
13
- return new TrieBuilder(words, trieOptions).build();
14
- }
15
- /**
16
- * Builds a Trie from a Iterable<string>. NO attempt a reducing the size of the Trie is done.
17
- * @param words Iterable set of words -- no processing is done on the words, they are inserted as is.
18
- * @param trieOptions options for the Trie
19
- */
20
- export function buildTrieFast(words, trieOptions) {
21
- const root = createTriFromList(words, trieOptions);
22
- return new Trie(root, undefined);
23
- }
24
- // cspell:words sigs
25
- const MAX_NUM_SIGS = 100000;
26
- const MAX_TRANSFORMS = 1000000;
27
- const MAX_CACHE_SIZE = 1000000;
28
- export class TrieBuilder {
29
- constructor(words, trieOptions) {
30
- this.count = 0;
31
- this.signatures = new SecondChanceCache(MAX_NUM_SIGS);
32
- this.cached = new SecondChanceCache(MAX_CACHE_SIZE);
33
- this.transforms = new SecondChanceCache(MAX_TRANSFORMS);
34
- this._eow = Object.freeze({ f: 1 });
35
- /** position 0 of lastPath is always the root */
36
- this.lastPath = [{ s: '', n: { f: undefined, c: undefined } }];
37
- this.tails = new Map([['', this._eow]]);
38
- this._canBeCached(this._eow); // this line is just for coverage reasons
39
- this.signatures.set(this.signature(this._eow), this._eow);
40
- this.cached.set(this._eow, this.count++);
41
- this.trieOptions = Object.freeze(mergeOptionalWithDefaults(trieOptions));
42
- if (words) {
43
- this.insert(words);
44
- }
45
- }
46
- set _root(n) {
47
- this.lastPath[0].n = n;
48
- }
49
- get _root() {
50
- return trieNodeToRoot(this.lastPath[0].n, this.trieOptions);
51
- }
52
- signature(n) {
53
- const isWord = n.f ? '*' : '';
54
- const ref = n.c ? JSON.stringify([...n.c.entries()].map(([k, n]) => [k, this.cached.get(n)])) : '';
55
- return isWord + ref;
56
- }
57
- _canBeCached(n) {
58
- if (!n.c)
59
- return true;
60
- for (const v of n.c) {
61
- if (!this.cached.has(v[1]))
62
- return false;
63
- }
64
- return true;
65
- }
66
- tryCacheFrozen(n) {
67
- if (this.cached.has(n)) {
68
- return n;
69
- }
70
- this.cached.set(n, this.count++);
71
- return n;
72
- }
73
- freeze(n) {
74
- if (Object.isFrozen(n))
75
- return n;
76
- // istanbul ignore else
77
- if (n.c) {
78
- const c = [...n.c]
79
- .sort((a, b) => (a[0] < b[0] ? -1 : 1))
80
- .map(([k, n]) => [k, this.freeze(n)]);
81
- n.c = new Map(c);
82
- Object.freeze(n.c);
83
- }
84
- return Object.freeze(n);
85
- }
86
- tryToCache(n) {
87
- if (!this._canBeCached(n)) {
88
- return n;
89
- }
90
- const sig = this.signature(n);
91
- const ref = this.signatures.get(sig);
92
- if (ref !== undefined) {
93
- return this.tryCacheFrozen(ref);
94
- }
95
- this.signatures.set(sig, this.freeze(n));
96
- return n;
97
- }
98
- storeTransform(src, s, result) {
99
- if (!Object.isFrozen(result) || !Object.isFrozen(src))
100
- return;
101
- const t = this.transforms.get(src) ?? new Map();
102
- t.set(s, result);
103
- this.transforms.set(src, t);
104
- }
105
- addChild(node, head, child) {
106
- if (node.c?.get(head) !== child) {
107
- if (!node.c || Object.isFrozen(node)) {
108
- node = { ...node, c: new Map(node.c ?? []) };
109
- }
110
- node.c?.set(head, child);
111
- }
112
- return Object.isFrozen(child) ? this.tryToCache(node) : node;
113
- }
114
- buildTail(s) {
115
- const v = this.tails.get(s);
116
- if (v)
117
- return v;
118
- const head = s[0];
119
- const tail = s.slice(1);
120
- const t = this.tails.get(tail);
121
- const c = t || this.buildTail(tail);
122
- const n = this.addChild({ f: undefined, c: undefined }, head, c);
123
- if (!t) {
124
- return n;
125
- }
126
- const cachedNode = this.tryCacheFrozen(Object.freeze(n));
127
- this.tails.set(s, cachedNode);
128
- return cachedNode;
129
- }
130
- _insert(node, s, d) {
131
- const orig = node;
132
- if (Object.isFrozen(node)) {
133
- const n = this.transforms.get(node)?.get(s);
134
- if (n) {
135
- return this.tryCacheFrozen(n);
136
- }
137
- }
138
- if (!s) {
139
- if (!node.c) {
140
- return this._eow;
141
- }
142
- else {
143
- node = copyIfFrozen(node);
144
- node.f = this._eow.f;
145
- return node;
146
- }
147
- }
148
- const head = s[0];
149
- const tail = s.slice(1);
150
- const cNode = node.c?.get(head);
151
- const child = cNode ? this._insert(cNode, tail, d + 1) : this.buildTail(tail);
152
- node = this.addChild(node, head, child);
153
- this.storeTransform(orig, s, node);
154
- this.lastPath[d] = { s: head, n: child };
155
- return node;
156
- }
157
- insertWord(word) {
158
- let d = 1;
159
- for (const s of word.split('')) {
160
- const p = this.lastPath[d];
161
- if (p?.s !== s)
162
- break;
163
- d++;
164
- }
165
- // remove the remaining part of the path because it doesn't match this word.
166
- if (word.length < d) {
167
- d = word.length;
168
- }
169
- this.lastPath.length = d;
170
- d -= 1;
171
- const { n } = this.lastPath[d];
172
- const tail = word.slice(d);
173
- this.lastPath[d].n = this._insert(n, tail, d + 1);
174
- while (d > 0) {
175
- const { s, n } = this.lastPath[d];
176
- d -= 1;
177
- const parent = this.lastPath[d];
178
- const pn = parent.n;
179
- parent.n = this.addChild(pn, s, n);
180
- if (pn === parent.n)
181
- break;
182
- const tail = word.slice(d);
183
- this.storeTransform(pn, tail, parent.n);
184
- }
185
- }
186
- insert(words) {
187
- for (const w of words) {
188
- w && this.insertWord(w);
189
- }
190
- }
191
- /**
192
- * Resets the builder
193
- */
194
- reset() {
195
- this._root = createTrieRoot(this.trieOptions);
196
- this.cached.clear();
197
- this.signatures.clear();
198
- this.signatures.set(this.signature(this._eow), this._eow);
199
- this.count = 0;
200
- this.cached.set(this._eow, this.count++);
201
- }
202
- build(consolidateSuffixes = false) {
203
- const root = this._root;
204
- // Reset the builder to prevent updating the trie in the background.
205
- this.reset();
206
- return new Trie(consolidateSuffixes ? consolidate(root) : root);
207
- }
208
- }
209
- function copyIfFrozen(n) {
210
- if (!Object.isFrozen(n))
211
- return n;
212
- const c = n.c ? new Map(n.c) : undefined;
213
- return { f: n.f, c };
214
- }
@@ -1,18 +0,0 @@
1
- import type { PartialWithUndefined } from './types.js';
2
- export declare const FLAG_WORD = 1;
3
- export declare class ChildMap extends Map<string, TrieNode> {
4
- }
5
- export interface TrieNode {
6
- f?: number | undefined;
7
- c?: ChildMap | undefined;
8
- }
9
- export interface TrieOptions {
10
- compoundCharacter: string;
11
- stripCaseAndAccentsPrefix: string;
12
- forbiddenWordPrefix: string;
13
- }
14
- export type PartialTrieOptions = PartialWithUndefined<TrieOptions> | undefined;
15
- export interface TrieRoot extends TrieOptions {
16
- c: ChildMap;
17
- }
18
- //# sourceMappingURL=TrieNode.d.ts.map
@@ -1,3 +0,0 @@
1
- export const FLAG_WORD = 1;
2
- export class ChildMap extends Map {
3
- }
@@ -1,29 +0,0 @@
1
- import type { Trie } from './trie.js';
2
- import type { TrieNode } from './TrieNode.js';
3
- export interface WalkItem {
4
- /** prefix so far */
5
- s: string;
6
- n: TrieNode;
7
- /** compound depth */
8
- d: number;
9
- /** true iff compound edge */
10
- c: boolean;
11
- }
12
- export type WalkNext = boolean;
13
- /**
14
- *
15
- * Depth first walk of a compound trie.
16
- * If there are compounds, this becomes an infinite iterator.
17
- * Use i.next(false) to prevent the walker from going deeper into the trie.
18
- *
19
- * @param trie the compound Trie to walk
20
- */
21
- export declare function compoundWalker(trie: Trie, caseSensitive?: boolean): Generator<WalkItem, void, WalkNext>;
22
- /**
23
- *
24
- * @param trie Trie to walk
25
- * @param maxDepth Max compound depth
26
- * @param caseSensitive case sensitive search.
27
- */
28
- export declare function compoundWords(trie: Trie, maxDepth: number, caseSensitive?: boolean): Generator<string, void, unknown>;
29
- //# sourceMappingURL=compoundWalker.d.ts.map
@@ -1,56 +0,0 @@
1
- /**
2
- *
3
- * Depth first walk of a compound trie.
4
- * If there are compounds, this becomes an infinite iterator.
5
- * Use i.next(false) to prevent the walker from going deeper into the trie.
6
- *
7
- * @param trie the compound Trie to walk
8
- */
9
- export function* compoundWalker(trie, caseSensitive = true) {
10
- const { compoundCharacter: cc, forbiddenWordPrefix: forbidden, stripCaseAndAccentsPrefix } = trie.options;
11
- const blockNode = new Set([cc, forbidden, stripCaseAndAccentsPrefix]);
12
- const root = (!caseSensitive && trie.root.c?.get(stripCaseAndAccentsPrefix)) || trie.root;
13
- function* walk(n, s, c, d) {
14
- const deeper = yield { n, s, c, d };
15
- if (deeper !== false && n.c) {
16
- for (const [k, cn] of n.c) {
17
- if (blockNode.has(k))
18
- continue;
19
- yield* walk(cn, s + k, false, d);
20
- }
21
- if (n.c.has(cc)) {
22
- const compoundNodes = root.c?.get(cc);
23
- if (compoundNodes) {
24
- yield* walk(compoundNodes, s, true, d + 1);
25
- }
26
- }
27
- }
28
- }
29
- // Make sure we do not walk forbidden and compound only words from the root.
30
- for (const n of root.c || []) {
31
- if (!blockNode.has(n[0])) {
32
- yield* walk(n[1], n[0], false, 0);
33
- }
34
- }
35
- }
36
- /**
37
- *
38
- * @param trie Trie to walk
39
- * @param maxDepth Max compound depth
40
- * @param caseSensitive case sensitive search.
41
- */
42
- export function* compoundWords(trie, maxDepth, caseSensitive = true) {
43
- const stream = compoundWalker(trie, caseSensitive);
44
- let item = stream.next();
45
- while (!item.done) {
46
- const { n, s, d } = item.value;
47
- if (d >= maxDepth) {
48
- item = stream.next(false);
49
- continue;
50
- }
51
- if (n.f) {
52
- yield s;
53
- }
54
- item = stream.next();
55
- }
56
- }
@@ -1,7 +0,0 @@
1
- import type { TrieRoot } from './TrieNode.js';
2
- /**
3
- * Consolidate to DAWG
4
- * @param root the root of the Trie tree
5
- */
6
- export declare function consolidate(root: TrieRoot): TrieRoot;
7
- //# sourceMappingURL=consolidate.d.ts.map