spell.replacer provides fast probabilistic spelling correction for character vectors in R. This package automatically replaces misspelled words based on their string distance from a frequency-ordered word list derived from the Corpus of Contemporary American English (COCA).
The package is inspired by Peter Norvig's classic probabilistic spell checker, but adapted for R with some key differences in implementation and optimization for speed over perfect accuracy.
⚠️ American English bias: Since the word list is based on COCA (Corpus of Contemporary American English), the package will convert spellings to American English variants (e.g., "colour" → "color", "realise" → "realize").
⚠️ Speed vs. Accuracy trade-off: This package prioritizes speed over perfect accuracy. For applications requiring maximum accuracy, consider more sophisticated spell checkers.
install.packages("spell.replacer")
# install.packages("devtools")
# devtools::install_github("browndw/spell.replacer")
install.packages("spell.replacer", repos = c('https://browndw.r-universe.dev', 'https://cloud.r-project.org'))
library(spell.replacer)
# Basic spelling correction
text <- "The quik brown fox jumps over the lasy dog."
corrected <- spell_replace(text)
print(corrected)
#> "The quick brown fox jumps over the lazy dog."
# Correct individual words
correct("recieve", coca_list)
#> "receive"
# More examples
examples <- c(
"This is a tesst of the speling correcter.",
"The algorithim should work wel on diferent sentances."
)
spell_replace(examples)
spell_replace(text,
word_list = coca_list, # Default word list from COCA
ignore_names = TRUE, # Ignore potential proper names
threshold = 0.12, # String distance threshold
ignore_punct = FALSE) # Consider punctuation in distance
# More conservative correction (higher accuracy, fewer corrections)
conservative <- spell_replace(text, threshold = 0.08)
# More aggressive correction (lower accuracy, more corrections)
aggressive <- spell_replace(text, threshold = 0.20)
# Ignore punctuation when calculating string distance
spell_replace("can't", ignore_punct = TRUE)
# Use your own word list (must be frequency-ordered)
custom_words <- c("hello", "world", "custom", "words")
spell_replace("helo wrld", word_list = custom_words)
The spell correction algorithm:
hunspell
This approach builds on Peter Norvig's probabilistic spell checker but differs in:
This package is inspired by Peter Norvig's "How to Write a Spelling Corrector", which demonstrates a simple but effective probabilistic approach to spell checking. Key similarities and differences:
The default word list (coca_list) contains the 100,000 most frequent words from the Corpus of Contemporary American English (COCA), sorted by frequency. COCA is a large, balanced corpus of American English containing over one billion words from 1990-2019.
The package is optimized for speed:
Typical performance: ~1000 words corrected per second (varies by text complexity and hardware).
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests on GitHub.
This package is licensed under the MIT License. See LICENSE for details.
If you use this package in your research, please cite:
citation("spell.replacer")