• Stars
    star
    260
  • Rank 151,377 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“ Hunspell compatible spell-checker

nspell

Travis Coverage Downloads Size

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.

Contents

Install

npm:

npm install nspell

You probably also want to install some dictionaries:

npm install dictionary-en

Use

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
}

API

NSpell(dictionary)

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.

Signatures
  • NSpell(dictionary)
  • NSpell(aff[, dic])
  • NSpell(dictionaries)
Parameters
  • dictionary (Object) β€” Object with aff (required) and dic (optional) properties
  • aff (Buffer or string) β€” Affix document to use. Must be in UTF-8 when buffer
  • dic (Buffer or string) β€” Dictionary document to use. Must be in UTF-8 when buffer
  • dictionaries (Array.<Dictionary>) β€” List of dictionary objects. The first must have an aff key, other aff keys are ignored
Returns

New instance of NSpell.

NSpell#correct(word)

Check if word is correctly spelled.

Example
spell.correct('color') // => true
spell.correct('html') // => false
spell.correct('abreviation') // => false
Parameters
  • word (string) β€” Word to check for correct spelling
Returns

boolean β€” Whether word is correctly spelled.

NSpell#suggest(word)

Suggest correctly spelled words close to word.

Example
spell.suggest('colour') // => ['color']
spell.suggest('color') // => []
spell.suggest('html') // => ['HTML']
spell.suggest('alot') // => ['allot', 'slot', 'clot', …]
Parameters
  • word (string) β€” Word to suggest spelling corrections for
Returns

Array.<string> β€” List with zero or more suggestions.

NSpell#spell(word)

Get spelling information for word.

Example
spell.spell('colour') // => {correct: false, forbidden: false, warn: false}
spell.spell('color') // => {correct: true, forbidden: false, warn: false}
Parameters
  • word (string) β€” Word to check
Returns

Object, with the following properties:

  • correct (boolean) β€” Whether word is correctly spelled
  • forbidden (boolean) β€” Whether word is actually correct, but forbidden from showing up as such (often by the users wish)
  • warn (boolean) β€” Whether word is correct, but should trigger a warning (rarely used in dictionaries)

NSpell#add(word[, model])

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.

Example
spell.correct('npm') // => false
spell.suggest('nnpm') // => ['ppm', 'bpm', …]

spell.add('npm')

spell.correct('npm') // => true
spell.suggest('nnpm') // => ['npm']
Parameters
  • word (string) β€” Word to add
  • model (string, optional) β€” Known word to model word after
Returns

NSpell β€” Operated on instance.

NSpell#remove(word)

Remove word from the known words.

Example
spell.correct('color') // => true

spell.remove('color')

spell.correct('color') // => false
Parameters
  • word (string) β€” Word to add
Returns

NSpell β€” Operated on instance.

NSpell#wordCharacters()

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.

Example
spell.wordCharacters() // => '0123456789'
Returns

string? β€” Defined word characters, if any.

NSpell#dictionary(dic)

Add an extra dictionary to the spellchecker.

Example
spell.dictionary(
  ['5', 'npm', 'nullish', 'rebase', 'SHA', 'stringification'].join('\n')
)
Parameters
  • dic (Buffer or string) β€” Dictionary document to use; must be in UTF-8 when buffer
Returns

NSpell β€” Operated on instance.

Note

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).

NSpell#personal(dic)

Add a personal dictionary.

Example
spell.personal(['foo', 'bar/color', '*baz'].join('\n'))
Parameters
  • dic (Buffer or string) β€” Dictionary document to use; must be in UTF-8 when buffer
Returns

NSpell β€” Operated on instance.

Note

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.

Dictionaries

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

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 '’0123456789Δ³.-\/

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

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 dictionary 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.

Affix options

The following affix options are known to Hunspell. The checked ones are supported by nspell.

General
  • 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
Suggestion
  • 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
Compounding
  • 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
Affix creation
  • PFX flag cross_product number
  • PFX flag stripping prefix [condition [morphological_fields…]]
  • SFX flag cross_product number
  • SFX flag stripping suffix [condition [morphological_fields…]]
Other
  • 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

License

MIT Β© Titus Wormer

More Repositories

1

franc

Natural language detection
JavaScript
3,906
star
2

dictionaries

Hunspell dictionaries in UTF-8
JavaScript
1,051
star
3

markdown-rs

CommonMark compliant markdown parser in Rust with ASTs and extensions
Rust
736
star
4

starry-night

Syntax highlighting, like GitHub
JavaScript
614
star
5

xdm

Just a *really* good MDX compiler. No runtime. With esbuild, Rollup, and webpack plugins
JavaScript
589
star
6

lowlight

Virtual syntax highlighting for virtual DOMs and non-HTML things
JavaScript
553
star
7

refractor

Lightweight, robust, elegant virtual syntax highlighting using Prism
JavaScript
535
star
8

mdxjs-rs

Compile MDX to JavaScript in Rust
Rust
387
star
9

markdown-table

Generate a markdown (GFM) table
JavaScript
249
star
10

gemoji

Info on gemoji (GitHub Emoji)
JavaScript
218
star
11

write-music

visualise sentence length
JavaScript
192
star
12

readability

visualise readability
JavaScript
185
star
13

parse-english

English (natural language) parser
JavaScript
159
star
14

server-components-mdx-demo

React server components + MDX
JavaScript
123
star
15

emphasize

ANSI syntax highlighting for the terminal
JavaScript
101
star
16

linked-list

Minimalistic linked lists
JavaScript
81
star
17

levenshtein.c

Levenshtein algorithm in C
C
79
star
18

import-meta-resolve

Resolve things like Node.js β€” ponyfill for `import.meta.resolve`
JavaScript
78
star
19

short-words

visualise lengthy words
JavaScript
65
star
20

trough

`trough` is middleware
JavaScript
61
star
21

bcp-47

Parse and stringify BCP 47 language tags
JavaScript
59
star
22

html-tag-names

List of known HTML tag names
JavaScript
58
star
23

parse-latin

Latin-script (natural language) parser
JavaScript
57
star
24

iso-3166

ISO 3166 (standard for country codes and codes for their subdivisions)
JavaScript
51
star
25

html-element-attributes

Map of HTML elements to allowed attributes
JavaScript
51
star
26

trim-lines

Remove spaces and tabs around line-breaks
JavaScript
50
star
27

common-words

visualise rare words
JavaScript
49
star
28

parse-entities

Parse HTML character references
JavaScript
46
star
29

iso-639-3

Info on ISO 639-3
JavaScript
46
star
30

levenshtein-rs

Levenshtein algorithm in Rust
Rust
42
star
31

emoticon

List of emoticons
JavaScript
40
star
32

direction

Detect directionality: left-to-right, right-to-left, or neutral
JavaScript
39
star
33

textom

DEPRECATED in favour of retext’s virtual object model
39
star
34

dictionary

Dictionary app that can work without JavaScript or internet
JavaScript
37
star
35

f-ck

🀬 Clean-up cuss words
JavaScript
37
star
36

dioscuri

A gemtext (`text/gemini`) parser with support for streaming, ASTs, and CSTs
JavaScript
34
star
37

property-information

Info on the properties and attributes of the web platform
JavaScript
33
star
38

stmr.c

Porter Stemmer algorithm in C
C
32
star
39

eslint-md

Deprecated
30
star
40

svg-tag-names

List of known SVG tag names
JavaScript
29
star
41

checkmoji

Check emoji across platforms
JavaScript
26
star
42

html-void-elements

List of known void HTML elements
JavaScript
26
star
43

npm-high-impact

The high-impact (popular) packages of npm
JavaScript
26
star
44

iso-639-2

Info on ISO 639-2
JavaScript
23
star
45

aria-attributes

List of ARIA attributes
JavaScript
21
star
46

stringify-entities

Serialize (encode) HTML character references
JavaScript
21
star
47

bcp-47-match

Match BCP 47 language tags with language ranges per RFC 4647
JavaScript
19
star
48

speakers

Speaker count for 450+ languages
JavaScript
19
star
49

svg-element-attributes

Map of SVG elements to allowed attributes
JavaScript
19
star
50

osx-learn

Add words to the OS X Spell Check dictionary
Shell
18
star
51

trigrams

Trigram files for 400+ languages
JavaScript
18
star
52

fault

Functional errors with formatted output
JavaScript
17
star
53

remark-preset-wooorm

Personal markdown (and prose) style
JavaScript
17
star
54

udhr

Universal declaration of human rights
HTML
17
star
55

bcp-47-normalize

Normalize, canonicalize, and format BCP 47 tags
JavaScript
16
star
56

happy-places

Little list of happy places
15
star
57

wooorm.github.io

πŸ› personal website
JavaScript
14
star
58

plain-text-data-to-json

Transform a simple plain-text database to JSON
JavaScript
14
star
59

parse-dutch

Dutch (natural language) parser
JavaScript
14
star
60

zwitch

Handle values based on a property
JavaScript
13
star
61

match-casing

Match the case of `value` to that of `base`
JavaScript
13
star
62

link-rel

List of valid values for `rel` on `<link>`
JavaScript
13
star
63

npm-esm-vs-cjs

Data on the share of ESM vs CJS on the public npm registry
JavaScript
13
star
64

linter-remark

Check markdown with remark in atom
13
star
65

is-badge

Check if `url` is a badge
JavaScript
13
star
66

vendors

List of vendor prefixes known to the web platform
JavaScript
12
star
67

load-plugin

Load a submodule / plugin
JavaScript
12
star
68

comma-separated-tokens

Parse and stringify comma-separated tokens
JavaScript
11
star
69

bail

Throw if given an error
JavaScript
11
star
70

space-separated-tokens

Parse and stringify space-separated tokens
JavaScript
10
star
71

trigram-utils

A few language trigram utilities
JavaScript
10
star
72

collapse-white-space

Collapse white space.
JavaScript
9
star
73

retext-language

Detect then language of text with Retext
JavaScript
9
star
74

longest-streak

Count the longest repeating streak of a substring
JavaScript
9
star
75

unherit

Clone a constructor without affecting the super-class
JavaScript
9
star
76

markdown-escapes

Legacy: list of escapable characters in markdown
JavaScript
9
star
77

state-toggle

Enter/exit a state
JavaScript
9
star
78

meta-name

List of values that can be used as `name`s on HTML `meta` elements
JavaScript
9
star
79

html-dangerous-encodings

List of dangerous HTML character encoding labels
JavaScript
8
star
80

character-entities

Map of named character references.
JavaScript
8
star
81

stmr

Porter Stemmer CLI
C
8
star
82

levenshtein

Levenshtein algorithm CLI
Shell
8
star
83

commonmark.json

CommonMark test spec in JSON
JavaScript
8
star
84

web-namespaces

Map of web namespaces
JavaScript
7
star
85

is-whitespace-character

Check if a character is a white space character
JavaScript
7
star
86

strip-skin-tone

Strip skin tone modifiers (as in Fitzpatrick scale) from emoji (πŸŽ…πŸΏ to πŸŽ…)
JavaScript
7
star
87

atom-travis

Install Atom on Travis
Shell
7
star
88

svg-event-attributes

List of SVG event handler attributes
JavaScript
7
star
89

control-pictures

Replace pictures for control character codes with actual control characters
JavaScript
7
star
90

css-declarations

Legacy utility to parse and stringify CSS declarations
JavaScript
6
star
91

html-encodings

Info on HTML character encodings.
JavaScript
6
star
92

mathml-tag-names

List of known MathML tag names
JavaScript
6
star
93

array-iterate

`Array#forEach()` but it’s possible to define where to move to next
JavaScript
6
star
94

atom-tap-test-runner

Run Atom package tests using TAP
6
star
95

ccount

Count how often a substring occurs
JavaScript
6
star
96

doctype

Info on HTML / XHTML / MathML / SVG doctypes
JavaScript
6
star
97

retext-english

Moved
6
star
98

labels

GitHub labels
6
star
99

remark-range

Deprecated
6
star
100

dead-or-alive

check if urls are dead or alive
JavaScript
6
star