Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function normalizeWordsToTrie(words: Sequence): Trie.TrieNode {
const trie = Trie.buildTrie(normalizeWords(words));
return trie.root;
}
export async function createSpellingDictionaryTrie(
data: IterableLike,
name: string,
source: string,
options?: SpellingDictionaryOptions
): Promise {
const trieNode = importTrie(data);
const trie = new Trie(trieNode);
return new SpellingDictionaryFromTrie(trie, name, options, source);
}
.action(async (filename, options) => {
const {
output: outputFile,
} = options;
notify('Reading Trie', !!outputFile);
const pOutputStream = createWriteStream(outputFile);
const lines = await fileToLines(filename);
const root = Trie.importTrie(lines);
const words = Trie.iteratorTrieWords(root);
const outputStream = await pOutputStream;
return new Promise((resolve) => {
iterableToStream(words.map(a => a + '\n')).pipe(outputStream).on('finish', () => resolve());
});
});
notify('Create Trie', !!outputFile);
const pOutputStream = createWriteStream(outputFile);
notify(`Generating...`, !!outputFile);
const lines = await fileToLines(filename);
const toLower = lowerCase ? (a: string) => a.toLowerCase() : (a: string) => a;
const wordsRx = lines
.map(toLower)
.map(a => a.trim())
.filter(a => !!a);
notify('Processing Trie');
const root = wordsRx.reduce((node: Trie.TrieNode, word: string) => Trie.insert(word, node), {} as Trie.TrieNode);
notify('Export Trie');
const serialStream = iterableToStream(Trie.serializeTrie(root, (base - 0) || 32));
const outputStream = await pOutputStream;
return new Promise((resolve) => {
serialStream.pipe(outputStream).on('finish', () => resolve());
});
});
export async function compileTrie(words: Sequence, destFilename: string, options: CompileTrieOptions): Promise {
log('Reading Words into Trie');
const base = options.base ?? 32;
const version = options.trie3 ? 3 : 1;
const destDir = path.dirname(destFilename);
const pDir = mkdirp(destDir);
const root = normalizeWordsToTrie(words);
log('Reduce duplicate word endings');
const trie = consolidate(root);
log(`Writing to file ${path.basename(destFilename)}`);
await pDir;
await writeSeqToFile(Trie.serializeTrie(trie, { base, comment: 'Built by cspell-tools.', version }), destFilename);
log(`Done writing to file ${path.basename(destFilename)}`);
}
public suggest(
word: string,
numSuggestions?: number,
compoundMethod: CompoundWordsMethod = CompoundWordsMethod.SEPARATE_WORDS,
numChanges?: number
): SuggestionResult[] {
word = this.mapWord(word);
const wordLc = word.toLowerCase();
compoundMethod = this.options.useCompounds ? CompoundWordsMethod.JOIN_WORDS : compoundMethod;
const numSugs = numSuggestions || defaultSuggestions;
const suggestions = this.trie.suggestWithCost(word, numSugs, compoundMethod, numChanges);
if (word === wordLc) {
return suggestions;
}
return mergeSuggestions(
numSugs,
suggestions,
this.trie.suggestWithCost(wordLc, numSugs, compoundMethod, numChanges)
);
}
public genSuggestions(
collector: SuggestionCollector,
compoundMethod: CompoundWordsMethod = CompoundWordsMethod.SEPARATE_WORDS
): void {
compoundMethod = this.options.useCompounds ? CompoundWordsMethod.JOIN_WORDS : compoundMethod;
this.trie.genSuggestions(collector, compoundMethod);
}
}
export async function createSpellingDictionaryTrie(
data: IterableLike,
name: string,
source: string,
options?: SpellingDictionaryOptions
): Promise {
const trieNode = importTrie(data);
const trie = new Trie(trieNode);
return new SpellingDictionaryFromTrie(trie, name, options, source);
}
{ w: lc, p: true },
{ w: lc_na, p: true },
];
};
const words = genSequence(wordList)
.filter(word => typeof word === 'string')
.map(word => word.trim())
.filter(w => !!w)
.concatMap(mapCase)
.reduce((s, w) => {
if (!s.has(w.w)) {
s.add(w.p ? PREFIX_NO_CASE + w.w : w.w);
}
return s;
}, new Set());
const trie = Trie.create(words);
return new SpellingDictionaryFromTrie(trie, name, options, source, words.size);
}
function compileSimpleWordListSeq(words: Sequence): Sequence {
return words.map(a => a.toLowerCase());
}
export function normalizeWordsToTrie(words: Sequence): Trie.TrieNode {
const trie = Trie.buildTrie(normalizeWords(words));
return trie.root;
}
export interface CompileTrieOptions {
base?: number;
trie3?: boolean;
}
export const consolidate = Trie.consolidate;
export async function compileTrie(words: Sequence, destFilename: string, options: CompileTrieOptions): Promise {
log('Reading Words into Trie');
const base = options.base ?? 32;
const version = options.trie3 ? 3 : 1;
const destDir = path.dirname(destFilename);
const pDir = mkdirp(destDir);
const root = normalizeWordsToTrie(words);
log('Reduce duplicate word endings');
const trie = consolidate(root);
log(`Writing to file ${path.basename(destFilename)}`);
await pDir;
await writeSeqToFile(Trie.serializeTrie(trie, { base, comment: 'Built by cspell-tools.', version }), destFilename);
log(`Done writing to file ${path.basename(destFilename)}`);
}