View Source SpellChecker (spell_checker v0.2.0)
Checks English word spelling against a bundled ~70k-word dictionary, with suggestions, autocorrect, and whole-text checking.
The dictionary is read from disk once and cached in memory (via
:persistent_term) for the life of the running node. It can be extended
with add_words/1 or replaced entirely with load_dictionary!/1, e.g. to
check a different language.
Quick start
SpellChecker.is_correct("hello")
#=> {:ok, true}
SpellChecker.correct("Helo")
#=> {:corrected, "Hello"}
SpellChecker.check_text("hello wrold")
#=> [{"wrold", ["wold", "world", "worlds", "would", "word"]}]
Summary
Functions
Adds extra words to the in-memory dictionary, on top of the bundled word
list, for the lifetime of the running node (or until
clear_custom_words/0 is called). Useful for domain jargon, names, or any
words missing from the bundled list.
Splits text into words and returns the misspelled ones together with
suggestions, as a list of {word, suggestions} tuples in the order they
first appear in text.
Removes all words previously added with add_words/1, restoring the
bundled dictionary only.
Returns the best single correction for word.
Checks whether a single word is spelled correctly.
Checks whether word is spelled correctly, returning suggestions if not.
Filters word_list down to just the correctly-spelled words.
Replaces the bundled dictionary with a custom list of words, e.g. to check
a different language or a trimmed word list. Words added via
add_words/1 are kept on top of the new dictionary.
Functions
@spec add_words([String.t()]) :: :ok
Adds extra words to the in-memory dictionary, on top of the bundled word
list, for the lifetime of the running node (or until
clear_custom_words/0 is called). Useful for domain jargon, names, or any
words missing from the bundled list.
Examples
iex> SpellChecker.add_words(["hexdocs", "genserver"])
:ok
iex> SpellChecker.is_correct("GenServer")
{:ok, true}
Splits text into words and returns the misspelled ones together with
suggestions, as a list of {word, suggestions} tuples in the order they
first appear in text.
Examples
iex> SpellChecker.check_text("hello wrold")
[{"wrold", ["wold", "world", "worlds", "would", "word"]}]
@spec clear_custom_words() :: :ok
Removes all words previously added with add_words/1, restoring the
bundled dictionary only.
Examples
iex> SpellChecker.add_words(["frobnicate"])
:ok
iex> SpellChecker.clear_custom_words()
:ok
iex> SpellChecker.is_correct("frobnicate")
{:none, false}
Returns the best single correction for word.
If word is already correct, it's returned unchanged as {:ok, word}.
Otherwise the closest match in the dictionary is returned as
{:corrected, guess}, with guess capitalized to match the input's
casing. If nothing is close enough, {:unknown, word} is returned.
Examples
iex> SpellChecker.correct("hello")
{:ok, "hello"}
iex> SpellChecker.correct("Helo")
{:corrected, "Hello"}
@spec is_correct(String.t()) :: {:ok, true} | {:none, false}
Checks whether a single word is spelled correctly.
Returns {:ok, true} if it is, {:none, false} otherwise. The check is
case-insensitive.
Examples
iex> SpellChecker.is_correct("hello")
{:ok, true}
iex> SpellChecker.is_correct("worlld")
{:none, false}
@spec is_correct!(String.t(), non_neg_integer()) :: {:ok, true} | [String.t()]
Checks whether word is spelled correctly, returning suggestions if not.
Returns {:ok, true} if word is correct, or a list of up to
suggest_count closest dictionary matches (default 5) otherwise.
Examples
iex> SpellChecker.is_correct!("hello")
{:ok, true}
iex> SpellChecker.is_correct!("worlld", 5)
["world", "worldly", "worlds", "wold", "word"]
Filters word_list down to just the correctly-spelled words.
acc is an accumulator used by the underlying recursion and should
generally be left at its default.
Examples
iex> words = ["hello", "worlld", "Programming"]
iex> SpellChecker.is_correct?(words)
["Programming", "hello"]
@spec load_dictionary!([String.t()]) :: :ok
Replaces the bundled dictionary with a custom list of words, e.g. to check
a different language or a trimmed word list. Words added via
add_words/1 are kept on top of the new dictionary.
Examples
# Use your own dictionary instead of the bundled English word list
SpellChecker.load_dictionary!(["ola", "bawo", "pele"])