cspell-dictionary
Advanced tools
Comparing version 6.27.0 to 6.28.0
@@ -137,2 +137,9 @@ import type { DictionaryInformation, ReplaceMap } from '@cspell/cspell-types'; | ||
mapWord(word: string): string; | ||
/** | ||
* Generates all possible word combinations by applying `repMap`. | ||
* This acts a bit like brace expansions in globs. | ||
* @param word - the word to map | ||
* @returns array of adjusted words. | ||
*/ | ||
remapWord?: (word: string) => string[]; | ||
readonly size: number; | ||
@@ -139,0 +146,0 @@ readonly isDictionaryCaseSensitive: boolean; |
@@ -14,2 +14,3 @@ import type { SuggestionCollector, SuggestionResult } from 'cspell-trie-lib'; | ||
readonly mapWord: (word: string) => string; | ||
readonly remapWord: (word: string) => string[]; | ||
readonly type = "SpellingDictionaryFromTrie"; | ||
@@ -45,3 +46,3 @@ readonly isDictionaryCaseSensitive: boolean; | ||
export declare function createSpellingDictionaryFromTrieFile(data: Iterable<string> | string, name: string, source: string, options: SpellingDictionaryOptions): SpellingDictionary; | ||
declare function outerWordForms(word: string, mapWord: (word: string) => string): Set<string>; | ||
declare function outerWordForms(word: string, mapWord: (word: string) => string[]): Set<string>; | ||
export declare const __testing__: { | ||
@@ -48,0 +49,0 @@ outerWordForms: typeof outerWordForms; |
@@ -51,2 +51,3 @@ "use strict"; | ||
this.mapWord = (0, repMap_1.createMapper)(options.repMap, options.dictionaryInformation?.ignore); | ||
this.remapWord = (0, repMap_1.createRepMapper)(options.repMap, options.dictionaryInformation?.ignore); | ||
this.isDictionaryCaseSensitive = options.caseSensitive ?? !trie.isLegacy; | ||
@@ -93,3 +94,3 @@ this.containsNoSuggestWords = options.noSuggest || false; | ||
findAnyForm(word, useCompounds, ignoreCase) { | ||
const outerForms = outerWordForms(word, this.mapWord); | ||
const outerForms = outerWordForms(word, this.remapWord ? this.remapWord : (word) => [this.mapWord(word)]); | ||
for (const form of outerForms) { | ||
@@ -198,3 +199,3 @@ const r = this._findAnyForm(form, useCompounds, ignoreCase); | ||
function outerWordForms(word, mapWord) { | ||
const forms = (0, sync_1.pipe)([word], (0, sync_1.opConcatMap)((word) => [word, word.normalize('NFC'), word.normalize('NFD')]), (0, sync_1.opConcatMap)((word) => [word, mapWord(word)])); | ||
const forms = (0, sync_1.pipe)([word], (0, sync_1.opConcatMap)((word) => [word, word.normalize('NFC'), word.normalize('NFD')]), (0, sync_1.opConcatMap)((word) => [word, ...mapWord(word)])); | ||
return new Set(forms); | ||
@@ -201,0 +202,0 @@ } |
import type { CharacterSet, ReplaceMap } from '@cspell/cspell-types'; | ||
export type ReplaceMapper = (src: string) => string; | ||
export declare function createMapper(repMap: ReplaceMap | undefined, ignoreCharset?: string): ReplaceMapper; | ||
declare function charsetToRepMap(charset: CharacterSet | undefined, replaceWith?: string): ReplaceMap | undefined; | ||
declare function charsetToRepMapRegEx(charset: CharacterSet | undefined, replaceWith?: string): ReplaceMap | undefined; | ||
declare function createMapperRegExp(repMap: ReplaceMap): RegExp; | ||
interface RepTrieNode { | ||
rep?: string[]; | ||
children?: Record<string, RepTrieNode>; | ||
} | ||
interface Edit { | ||
b: number; | ||
e: number; | ||
r: string; | ||
} | ||
export declare function createRepMapper(repMap: ReplaceMap | undefined, ignoreCharset?: string): (word: string) => string[]; | ||
declare function applyEdits(word: string, edits: Edit[]): string[]; | ||
declare function calcAllEdits(root: RepTrieNode, word: string): Edit[]; | ||
declare function createTrie(repMap: ReplaceMap | undefined, ignoreCharset?: string): RepTrieNode; | ||
export declare const __testing__: { | ||
charsetToRepMap: typeof charsetToRepMap; | ||
charsetToRepMap: typeof charsetToRepMapRegEx; | ||
createMapperRegExp: typeof createMapperRegExp; | ||
createTrie: typeof createTrie; | ||
calcAllEdits: typeof calcAllEdits; | ||
applyEdits: typeof applyEdits; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=repMap.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.__testing__ = exports.createMapper = void 0; | ||
exports.__testing__ = exports.createRepMapper = exports.createMapper = void 0; | ||
const cspell_trie_lib_1 = require("cspell-trie-lib"); | ||
const regexHelper_1 = require("./regexHelper"); | ||
const util_1 = require("./util"); | ||
function createMapper(repMap, ignoreCharset) { | ||
@@ -9,3 +11,3 @@ if (!repMap && !ignoreCharset) | ||
repMap = repMap || []; | ||
const charsetMap = charsetToRepMap(ignoreCharset); | ||
const charsetMap = charsetToRepMapRegEx(ignoreCharset); | ||
if (charsetMap) { | ||
@@ -29,3 +31,3 @@ repMap = repMap.concat(charsetMap); | ||
exports.createMapper = createMapper; | ||
function charsetToRepMap(charset, replaceWith = '') { | ||
function charsetToRepMapRegEx(charset, replaceWith = '') { | ||
if (!charset) | ||
@@ -38,2 +40,13 @@ return undefined; | ||
} | ||
function charsetToRepMap(charset, replaceWith = '') { | ||
if (!charset) | ||
return undefined; | ||
return charset | ||
.split('|') | ||
.flatMap((chars) => [...(0, cspell_trie_lib_1.expandCharacterSet)(chars)]) | ||
.map((char) => [char, replaceWith]); | ||
} | ||
function expandReplaceMap(repMap) { | ||
return repMap.flatMap(([from, replaceWith]) => from.split('|').map((w) => [w, replaceWith])); | ||
} | ||
function createMapperRegExp(repMap) { | ||
@@ -64,6 +77,86 @@ const filteredMap = repMap.filter(([match, _]) => !!match); | ||
} | ||
function createRepMapper(repMap, ignoreCharset) { | ||
if (!repMap && !ignoreCharset) | ||
return (word) => [word]; | ||
const trie = createTrie(repMap, ignoreCharset); | ||
// const root = createTrie(repMap, ignoreCharset); | ||
return (word) => { | ||
const edits = calcAllEdits(trie, word); | ||
return applyEdits(word, edits); | ||
}; | ||
} | ||
exports.createRepMapper = createRepMapper; | ||
function applyEdits(word, edits) { | ||
if (!edits.length) | ||
return [word]; | ||
// Prepare | ||
const letterEdits = []; | ||
for (let i = 0; i < word.length; ++i) { | ||
letterEdits[i] = { edits: [{ b: i, e: i + 1, r: word[i] }], suffixes: [] }; | ||
} | ||
letterEdits[word.length] = { edits: [], suffixes: [''] }; | ||
// Add edits | ||
for (const edit of edits) { | ||
const le = letterEdits[edit.b]; | ||
le.edits.push(edit); | ||
} | ||
// Apply edits in reverse | ||
for (let i = word.length - 1; i >= 0; --i) { | ||
const le = letterEdits[i]; | ||
const sfx = le.suffixes; | ||
for (const edit of le.edits) { | ||
const pfx = edit.r; | ||
const nSfx = letterEdits[edit.e].suffixes; | ||
for (const s of nSfx) { | ||
sfx.push(pfx + s); | ||
} | ||
} | ||
} | ||
const results = new Set(letterEdits[0].suffixes); | ||
return [...results]; | ||
} | ||
function calcAllEdits(root, word) { | ||
const edits = []; | ||
function walk(node, b, e) { | ||
if (node.rep) { | ||
node.rep.forEach((r) => edits.push({ b, e, r })); | ||
} | ||
if (e === word.length || !node.children) | ||
return; | ||
const n = node.children[word[e]]; | ||
if (!n) | ||
return; | ||
walk(n, b, e + 1); | ||
} | ||
for (let i = 0; i < word.length; ++i) { | ||
walk(root, i, i); | ||
} | ||
return edits; | ||
} | ||
function createTrie(repMap, ignoreCharset) { | ||
const combined = [repMap, charsetToRepMap(ignoreCharset)].filter(util_1.isDefined).flatMap((a) => a); | ||
const expanded = expandReplaceMap(combined); | ||
const trieRoot = Object.create(null); | ||
expanded.forEach(([match, replaceWith]) => addToTrie(trieRoot, match, replaceWith)); | ||
return trieRoot; | ||
} | ||
function addToTrie(node, match, replaceWith) { | ||
while (match) { | ||
const children = node.children || (node.children = Object.create(null)); | ||
const k = match[0]; | ||
const childNode = children[k] || (children[k] = Object.create(null)); | ||
node = childNode; | ||
match = match.slice(1); | ||
} | ||
const s = new Set(node.rep || []); | ||
s.add(replaceWith); | ||
node.rep = [...s]; | ||
} | ||
exports.__testing__ = { | ||
charsetToRepMap, | ||
charsetToRepMap: charsetToRepMapRegEx, | ||
createMapperRegExp, | ||
createTrie, | ||
calcAllEdits, | ||
applyEdits, | ||
}; | ||
//# sourceMappingURL=repMap.js.map |
{ | ||
"name": "cspell-dictionary", | ||
"version": "6.27.0", | ||
"version": "6.28.0", | ||
"description": "A spelling dictionary library useful for checking words and getting suggestions.", | ||
@@ -9,2 +9,3 @@ "main": "dist/index.js", | ||
"dist", | ||
"!**/*.tsbuildInfo", | ||
"!**/__mocks__", | ||
@@ -17,3 +18,2 @@ "!**/*.test.*", | ||
"build": "tsc -p .", | ||
"build-dev": "tsc -p tsconfig.dev.json", | ||
"watch": "tsc -p . -w", | ||
@@ -48,9 +48,9 @@ "clean": "shx rm -rf dist temp coverage .tsbuildinfo", | ||
"dependencies": { | ||
"@cspell/cspell-pipe": "6.27.0", | ||
"@cspell/cspell-types": "6.27.0", | ||
"cspell-trie-lib": "6.27.0", | ||
"@cspell/cspell-pipe": "6.28.0", | ||
"@cspell/cspell-types": "6.28.0", | ||
"cspell-trie-lib": "6.28.0", | ||
"fast-equals": "^4.0.3", | ||
"gensequence": "^4.0.3" | ||
"gensequence": "^5.0.2" | ||
}, | ||
"gitHead": "b0e31c7ba91ba467d5fd9c66f238bb5d899f4833" | ||
"gitHead": "1c314413e76908e5fbf61fd2555726112b177c0e" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
110767
2641
+ Added@cspell/cspell-pipe@6.28.0(transitive)
+ Added@cspell/cspell-types@6.28.0(transitive)
+ Addedcspell-trie-lib@6.28.0(transitive)
+ Addedgensequence@5.0.2(transitive)
- Removed@cspell/cspell-pipe@6.27.0(transitive)
- Removed@cspell/cspell-types@6.27.0(transitive)
- Removedcspell-trie-lib@6.27.0(transitive)
- Removedgensequence@4.0.3(transitive)
Updated@cspell/cspell-pipe@6.28.0
Updated@cspell/cspell-types@6.28.0
Updatedcspell-trie-lib@6.28.0
Updatedgensequence@^5.0.2