WebAssembly port of the Hunspell spell-checking library.
Includes an easy-to-use wrapper written in TypeScript.
npm install hunspell-wasm
// Import
import { createHunspellFromFiles } from 'hunspell-wasm'
// Create instance (dictionary paths are illustrative)
const hunspell = await createHunspellFromFiles('dict/en-US.aff', 'dict/en-US.dic')
console.log(hunspell.testSpelling('Hello'))
// Output: true
console.log(hunspell.testSpelling('Hellow'))
// Output: false
console.log(hunspell.getSpellingSuggestions('Hellow'))
// Output: [ 'Hello', 'Hell ow', 'Hello w', 'Howell', 'Lowell' ]
// Dispose instance
hunspell.dispose()
You can also create the Hunspell instance using affix and dictionary file content given as strings, instead of file paths:
import { createHunspellFromStrings } from 'hunspell-wasm'
const affixes = ``
const dictionary = `
Hello
World
`
const hunspell = await createHunspellFromStrings(affixes, dictionary)
Add or remove words from dictionary:
hunspell.addWord('foobar')
hunspell.removeWord('foobar')
Add a dictionary using hunspell.addDictionaryFromFile
or hunspell.addDictionaryFromString
:
await hunspell.addDictionaryFromFile('dict/myDictionary.dic')
const dictionaryString = `
Yes
No
`
hunspell.addDictionaryFromString(dictionaryString)
Other supported methods:
hunspell.getSuffixSuggestions('run')
// Output: [ 'runs', "run's" ]
hunspell.analyzeWord('Capturing')
// Output: [ ' st:capture fl:G' ]
hunspell.stemWord('Capturing')
// Output: [ 'capture' ]
Use a dynamic import
expression in an async
function context:
MyCommonJSModule.cjs
:
async function start() {
const { createHunspellFromFiles } = await import('hunspell-wasm')
//...
}
You can find .aff
and .dic
dictionary files for many different languages in titoBouzout
's repository.
See instructions here.
- Wrap C API methods
Hunspell_stem2
,Hunspell_generate2
Hunspell is licensed under LGPL/GPL/MPL tri-license.