hunspell-wasm
v0.3.0
Published
WebAssembly port of the Hunspell spell-checking library.
Downloads
19
Readme
Hunspell (WebAssembly port)
WebAssembly port of the Hunspell spell-checking library.
Includes an easy-to-use wrapper written in TypeScript.
Installation
npm install hunspell-wasm
Usage
// 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' ]
Importing from within a CommonJS module
Use a dynamic import
expression in an async
function context:
MyCommonJSModule.cjs
:
async function start() {
const { createHunspellFromFiles } = await import('hunspell-wasm')
//...
}
Dictionary files
You can find .aff
and .dic
dictionary files for many different languages in titoBouzout
's repository.
Building the WebAssembly module
See instructions here.
TODO
- Wrap C API methods
Hunspell_stem2
,Hunspell_generate2
License
Hunspell is licensed under LGPL/GPL/MPL tri-license.