R/spell_correct.R

Defines functions spell_correct

Documented in spell_correct

#' spell_correct
#'
#' @param terms 
#'
#' @return
#' @export
#'
#' @examples
spell_correct <- function(terms) {
   # Figure out what's ok already.
   spelled_correctly <- hunspell::hunspell_check(terms)
   
   # Get suggestions for the bad ones.
   suggestions <- hunspell::hunspell_suggest(terms[!spelled_correctly])
   
   # Pull out the first suggestion, defaulting to NA if there aren't any.
   replacements <- purrr::map_chr(suggestions, 1, .default = NA)
   
   # Replace the misspellings with the replacements. Use the term if there isn't
   # a replacement.
   terms[!spelled_correctly] <- dplyr::coalesce(
      replacements,
      terms[!spelled_correctly]
   )
   
   return(terms)
}
mengliuveronica/tm101 documentation built on June 9, 2022, 5:37 p.m.