R/spell_lookup.R

Defines functions spell_lookup

Documented in spell_lookup

#' Look up a title in a data set of titles given a permutation and/or misspelling
#'
#' @param data Data frame of any kind
#' @param column Character of the name of the variable of interest
#' @param input Character of the full title of the variable of interest, in any permutation or misspelling
#' @return The proper title of interest from the full data set
#' @examples
#' spell_lookup(movies, "title", "tyo 2 story")
#' @export
spell_lookup <- function(data, column, input){
  tolower(input)
  bad <- hunspell(input)
  correct <- lapply(str_split(input, " "), hunspell_check)
  suggest <- lapply(hunspell_suggest(bad[[1]]), '[[', 1)
  correct_list <- as.list(str_split(input, " ")[[1]])[correct[[1]]]
  perm_corrected_strings <- permn(unlist(lapply(append(suggest, correct_list), str_c)))
  corrected_strings <- unlist(lapply(perm_corrected_strings, str_c, collapse = " "))
  lowercase_column <- lapply(tolower(iconv(data[[column]])), removePunctuation)
  for (i in 1:length(corrected_strings)) {
    if (length(data[grepl(corrected_strings[[i]], lowercase_column),][[column]] %in% movies) != 0) {
      print(data[grepl(corrected_strings[[i]], lowercase_column),][[column]])
      break
    }
  }
}
hnasif/movies documentation built on May 17, 2019, 4:36 p.m.