Question: loading a dictionary with cspell-lib?
Hello,
I am using cspell-lib in my node application to check for typos. Specifically, the suggestionsForWord method.
I'd like to add multilingual support and scientific terms, however I'm unsure how to do this? The readme in the dictionary I want to use gives these installation instructions:
npm install -g @cspell/dict-scientific-terms-us
cspell link add @cspell/dict-scientific-terms-us
However as I'm not using the cspell cli, I don't think this applies? I've read through the source code in cspell-lib but don't see how.
Any advice appreciated, thanks in advance.
@simon-lang,
Good question. I moved this to the CSpell repo because it has more to do with cspell-lib than with the dictionaries themselves.
There are two parts to the question:
- How should the dictionaries be installed?
- How to load the dictionaries?
Q: How should the dictionaries be installed? A: Just like a normal dependency. See: cspell-bundled-dicts/package.json
Q: How to load the dictionaries?
A: It needs to be imported and resolved in the CSpellSettings.
Something like this:
import { resolveConfigFileImports, suggestionsForWord } from 'cspell';
const rawSettings = {
import: ['@cspell/dict-scientific-terms-us'],
dictionaries: ['scientific-terms-us']
};
const settings = await resolveConfigFileImports({ url: new URL(import.meta.url), settings: rawSettings });
const results = await suggestionsForWord('aachen', undefined, settings);
Note: I have not tried this code, but it should give you the right idea.