Hunspell-like spell-checker in plain-vanilla JavaScript.
nspell contains most of the essential core of Hunspell. It does not contain a tokeniser but leaves many details up to implementors. The main difference, conceptually, is that Hunspell is based on the user and their preferences, whereas nspell is based on explicitly passed in options, thus producing the same results regardless of OS, file system, or environment.
npm:
npm install nspell
You probably also want to install some dictionaries:
npm install dictionary-en
var dictionary = require('dictionary-en')
var nspell = require('nspell')
dictionary(ondictionary)
function ondictionary(err, dict) {
if (err) {
throw err
}
var spell = nspell(dict)
console.log(spell.correct('colour')) // => false
console.log(spell.suggest('colour')) // => ['color']
console.log(spell.correct('color')) // => true
console.log(spell.correct('npm')) // => false
spell.add('npm')
console.log(spell.correct('npm')) // => true
}
Create a new spell checker.
Passing an affix document is required, through any of the below mentioned
signatures.
nspell is useless without at least one dic
passed: make sure to pass one
either in the constructor or to nspell#dictionary
.
NSpell(dictionary)
NSpell(aff[, dic])
NSpell(dictionaries)
dictionary
(Object
) — Object withaff
(required) anddic
(optional) propertiesaff
(Buffer
orstring
) — Affix document to use. Must be in UTF-8 when bufferdic
(Buffer
orstring
) — Dictionary document to use. Must be in UTF-8 when bufferdictionaries
(Array.<Dictionary>
) — List ofdictionary
objects. The first must have anaff
key, otheraff
keys are ignored
New instance of NSpell
.
Check if word
is correctly spelled.
spell.correct('color') // => true
spell.correct('html') // => false
spell.correct('abreviation') // => false
word
(string
) — Word to check for correct spelling
boolean
— Whether word
is correctly spelled.
Suggest correctly spelled words close to word
.
spell.suggest('colour') // => ['color']
spell.suggest('color') // => []
spell.suggest('html') // => ['HTML']
spell.suggest('alot') // => ['allot', 'slot', 'clot', …]
word
(string
) — Word to suggest spelling corrections for
Array.<string>
— List with zero or more suggestions.
Get spelling information for word
.
spell.spell('colour') // => {correct: false, forbidden: false, warn: false}
spell.spell('color') // => {correct: true, forbidden: false, warn: false}
word
(string
) — Word to check
Object
, with the following properties:
correct
(boolean
) — Whetherword
is correctly spelledforbidden
(boolean
) — Whetherword
is actually correct, but forbidden from showing up as such (often by the users wish)warn
(boolean
) — Whetherword
is correct, but should trigger a warning (rarely used in dictionaries)
Add word
to known words.
If no model is given, the word will be marked as correct in the future, and will
show up in spelling suggestions.
If a model is given, word
will be handled the same as model
.
spell.correct('npm') // => false
spell.suggest('nnpm') // => ['ppm', 'bpm', …]
spell.add('npm')
spell.correct('npm') // => true
spell.suggest('nnpm') // => ['npm']
word
(string
) — Word to addmodel
(string
, optional) — Known word to modelword
after
NSpell
— Operated on instance.
Remove word
from the known words.
spell.correct('color') // => true
spell.remove('color')
spell.correct('color') // => false
word
(string
) — Word to add
NSpell
— Operated on instance.
Get extra word characters defined by the loaded affix file.
Most affix files don’t set these, but for example the en dictionary sets
0123456789
.
spell.wordCharacters() // => '0123456789'
string?
— Defined word characters, if any.
Add an extra dictionary to the spellchecker.
spell.dictionary(
['5', 'npm', 'nullish', 'rebase', 'SHA', 'stringification'].join('\n')
)
dic
(Buffer
orstring
) — Dictionary document to use; must be in UTF-8 when buffer
NSpell
— Operated on instance.
The given dic
must be designed to work with the already loaded affix.
It’s not possible to add dictionary files from different languages together
(use two NSpell
instances for that).
Add a personal dictionary.
spell.personal(['foo', 'bar/color', '*baz'].join('\n'))
dic
(Buffer
orstring
) — Dictionary document to use; must be in UTF-8 when buffer
NSpell
— Operated on instance.
Lines starting with a *
mark a word as forbidden, which results in them being
seen as incorrect, and prevents them from showing up in suggestions.
Splitting a line in two with a slash, adds the left side and models it after the
already known right word.
nspell supports many parts of Hunspell-style dictionaries. Essentially, the concept of a dictionary consists of one “affix” document, and one or more “dictionary” documents. The documents are tightly linked, so it’s not possible to use a Dutch affix with an English dictionary document.
Below is a short introduction, see hunspell(5) for more information.
Affix documents define the language, keyboard, flags, and much more. For example, a paraphrased Dutch affix document looks as follows:
SET UTF-8
KEY qwertyuiop|asdfghjkl|zxcvbnm|qawsedrftgyhujikolp|azsxdcfvgbhnjmk|aze|qsd|lm|wx|aqz|qws|
WORDCHARS '’0123456789ij.-\/
REP 487
REP e en
REP ji Äł
REP u oe
# …
SFX An Y 11
SFX An 0 de d
SFX An 0 fe f
SFX An 0 ge g
# …
Not every option is supported in nspell. See Affix options for a list of all options and which ones are supported.
Dictionary documents contain words and flags applying to those words. For example:
3
foo
bar/a
baz/ab
The above document contains three words, as the count on the first line shows. Further lines each start with a word. Some lines contain flags, as denoted by the slashes. What those flags do, and the size of flags, is defined by affix documents.
Personal dictionaries are not intertwined with affix document. They define new words and words to forbid. For example:
foo
bar/baz
*qux
In the above example, foo
is added as a known word; bar
is added as well,
but modelled after the existing word baz
; finally, qux
is marked as a
forbidden word.
The following affix options are known to Hunspell. The checked ones are supported by nspell.
-
SET encoding
(UTF-8 is implied) -
FLAG value
-
COMPLEXPREFIXES
-
LANG langcode
-
IGNORE characters
-
AF number_of_flag_vector_aliases
-
AF flag_vector
-
AF definitions in the affix file:
-
AF flag_vector
-
KEY characters_separated_by_vertical_line_optionally
-
TRY characters
-
NOSUGGEST flag
-
MAXCPDSUGS num
-
MAXNGRAMSUGS num
-
MAXDIFF [0-10]
-
ONLYMAXDIFF
-
NOSPLITSUGS
-
SUGSWITHDOTS
-
REP number_of_replacement_definitions
-
REP what replacement
-
MAP number_of_map_definitions
-
MAP string_of_related_chars_or_parenthesized_character_sequences
-
PHONE number_of_phone_definitions
-
PHONE what replacement
-
WARN flag
-
FORBIDWARN
-
BREAK number_of_break_definitions
-
BREAK character_or_character_sequence
-
COMPOUNDRULE number_of_compound_definitions
-
COMPOUNDRULE compound_pattern
-
COMPOUNDMIN num
-
COMPOUNDFLAG flag
-
COMPOUNDBEGIN flag
-
COMPOUNDLAST flag
-
COMPOUNDMIDDLE flag
-
ONLYINCOMPOUND flag
-
COMPOUNDPERMITFLAG flag
-
COMPOUNDFORBIDFLAG flag
-
COMPOUNDMORESUFFIXES
-
COMPOUNDROOT flag
-
COMPOUNDWORDMAX number
-
CHECKCOMPOUNDDUP
-
CHECKCOMPOUNDREP
-
CHECKCOMPOUNDCASE
-
CHECKCOMPOUNDTRIPLE
-
SIMPLIFIEDTRIPLE
-
CHECKCOMPOUNDPATTERN number_of_checkcompoundpattern_definitions
-
CHECKCOMPOUNDPATTERN endchars[/flag] beginchars[/flag] [replacement]
-
FORCEUCASE flag
-
COMPOUNDSYLLABLE max_syllable vowels
-
SYLLABLENUM flags
-
PFX flag cross_product number
-
PFX flag stripping prefix [condition [morphological_fields…]]
-
SFX flag cross_product number
-
SFX flag stripping suffix [condition [morphological_fields…]]
-
CIRCUMFIX flag
-
FORBIDDENWORD flag
-
FULLSTRIP
-
KEEPCASE flag
-
ICONV number_of_ICONV_definitions
-
ICONV pattern pattern2
-
OCONV number_of_OCONV_definitions
-
OCONV pattern pattern2
-
LEMMA_PRESENT flag
-
NEEDAFFIX flag
-
PSEUDOROOT flag
-
SUBSTANDARD flag
-
WORDCHARS characters
-
CHECKSHARPS
MIT © Titus Wormer