Skip to content

Instantly share code, notes, and snippets.

@bschneidr
Last active April 17, 2019 18:46
Show Gist options
  • Save bschneidr/b436919b600ec9294763fe1663360245 to your computer and use it in GitHub Desktop.
Save bschneidr/b436919b600ec9294763fe1663360245 to your computer and use it in GitHub Desktop.
Visualize bad text with RStudio viewer
example_strings <- c("Heres the sentince", "Here's anothr")
highlight_bad_text <- function(strings, match = NA) {
bad_text <- hunspell::hunspell(text = as.character(strings),
format = "text",
dict = hunspell::dictionary("en_us"),
ignore = hunspell::en_stats)
names(bad_text) <- names(strings)
highlights <- vector(mode = 'list', length = length(strings))
names(highlights) <- names(strings)
index <- 1L
while (index <= length(highlights)) {
string <- strings[index]
pattern <- bad_text[[index]]
loc <- stringr::str_locate_all(string, pattern)
string_list <- Map(loc = loc, string = string, function(loc,
string) {
if (nrow(loc) == 0)
return(string)
for (i in rev(seq_len(nrow(loc)))) {
stringr::str_sub(string, loc[i, , drop = FALSE]) <- paste0("<span class='match'>",
stringr::str_sub(string, loc[i, , drop = FALSE]), "</span>")
}
string
})
string <- unlist(string_list)
highlights[[index]] <- string
index <- index + 1L
}
highlights
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment