Hunspell Spell Checking and Morphological Analysis

The hunspell function is a high-level wrapper for finding spelling errors within a text document. It takes a character vector with text (plain, latex or man format), parses out the words and returns a list with incorrect words for each line. It effectively combines of hunspell_parse with hunspell_check in a single step. Other functions in the package operate on individual words, see details.

hunspell(text, format = c("text", "man", "latex"), dict = "en_US", ignore = en_stats)
hunspell_parse(text, format = c("text", "man", "latex"), dict = "en_US")
hunspell_check(words, dict = "en_US")
hunspell_suggest(words, dict = "en_US")
hunspell_analyze(words, dict = "en_US")
hunspell_stem(words, dict = "en_US")
hunspell_info(dict = "en_US")
character vector with arbitrary input text
input format; supported parsers are text, latex or man
dictionary language, see details
character vector with additional approved words added to the dictionary
character vector with individual words to spell check

Hunspell uses a special dictionary format that defines which stems and affixes are valid in a given language. The hunspell_analyze function shows how a word breaks down into a valid stem plus affix. The hunspell_stem function is similar but only returns valid stems for a given word. Stemming can be used to summarize text (e.g in a wordcloud). The hunspell_check function takes a vector of individual words and tests each one for correctness. Finally hunspell_suggest is used to suggest correct alternatives for each (incorrect) input word.

Because spell checking is usually done on a document, the package includes some parsers to extract words from various common formats. With hunspell_parse we can parse plain-text, latex and man format. R also has a few built-in parsers such as RdTextFilter and SweaveTeXFilter, see also ?aspell.

The package searches for dictionaries in the working directory as well as in the standard system locations. Additional search paths can be specified by setting the DICPATH environment variable. A US English dictionary (en_US) is included with the package; other dictionaries need to be installed by the system. Most operating systems already include compatible dictionaries with names such as hunspell-en-gb or myspell-en-gb.

To manually install dictionaries, download the .aff and .dic file from an OpenOffice mirror or bundle and copy them to ~/Library/Spelling or a custom directory specified in DICPATH. Alternatively you can pass the entire path to the .dic file as the dict parameter.

Note that hunspell uses iconv to convert input text to the encoding used by the dictionary. This will fail if text contains characters which are unsupported by that particular encoding. For this reason UTF-8 dictionaries are preferable over legacy 8bit dictionaries Several UTF8 dictionaries are available from Github.

  • dicpath
  • en_stats
  • hunspell
  • hunspell_analyze
  • hunspell_check
  • hunspell_find
  • hunspell_info
  • hunspell_parse
  • hunspell_stem
  • hunspell_suggest
# Check individual words
words <- c("beer", "wiskey", "wine")
correct <- hunspell_check(words)

# Find suggestions for incorrect words

# Extract incorrect from a piece of text
bad <- hunspell("spell checkers are not neccessairy for langauge ninja's")

# Stemming
words <- c("love", "loving", "lovingly", "loved", "lover", "lovely", "love")

# Check an entire latex document
download.file("", "1406.4806v1.tar.gz",  mode = "wb")
text <- readLines("content.tex", warn = FALSE)
bad_words <- hunspell(text, format = "latex")

# Summarize text by stems (e.g. for wordcloud)
allwords <- hunspell_parse(text, format = "latex")
stems <- unlist(hunspell_stem(unlist(allwords)))
words <- head(sort(table(stems), decreasing = TRUE), 200)
Documentation reproduced from package hunspell, version 1.2, License: MIT + file LICENSE

Community examples

Looks like there are no examples yet.