Skip to content

Instantly share code, notes, and snippets.

@mcguinlu
Created December 15, 2020 17:18
Show Gist options
  • Save mcguinlu/fb812236ab77168f8c895980f6288144 to your computer and use it in GitHub Desktop.
Save mcguinlu/fb812236ab77168f8c895980f6288144 to your computer and use it in GitHub Desktop.
all_caps <- function(x) {
x_v <- stringr::str_to_lower(x) %>%
stringr::str_split(stringr::boundary()) %>%
unlist()
for (position in 1:nchar(x)) {
x_v[position] <- paste0("[",stringr::str_to_upper(x_v[position]),x_v[position],"]")
}
x_v <- paste0(x_v, collapse = "")
return(x_v)
}
fix_caps <- function(x){
unlist(lapply(x, function(x){
if (grepl("\\[",x)==TRUE) {
return(x)
}
x_v <- stringr::str_to_lower(x) %>%
stringr::str_split(stringr::boundary()) %>%
unlist()
x_v[1] <- paste0("[",stringr::str_to_upper(x_v[1]),x_v[1],"]")
x_v <- paste0(x_v, collapse = "")
return(x_v)}
))
}
fix_wildcard <- function(x) {
x <- stringr::str_replace_all(x,"\\*","([[:alpha:]])")
return(x)
}
fix_near <- function(x) {
x <- stringr::str_replace_all(x,"\\s?[Nn][Ee][Aa][Rr](\\d)\\s?","NEAR\\1") %>%
stringr::str_replace_all("NEAR(\\d)","(\\\\s+)([[:graph:]]+\\\\s+){0,\\1}")
return(x)
}
library(testthat)
expect_true(fix_near("sysNEAR2rev") == "sys(\\s+)([[:graph:]]+\\s+){0,2}rev")
expect_true(fix_near("sys Near2 rev") == "sys(\\s+)([[:graph:]]+\\s+){0,2}rev")
expect_false(fix_near("sys Near2 rev") == "sys(\\s+)([[:graph:]]+\\s+){0,2}rev")
expect_true(fix_near("sys near2 rev") == "sys(\\s+)([[:graph:]]+\\s+){0,2}rev")
expect_false(fix_near("sys near rev") == "sys(\\s+)([[:graph:]]+\\s+){0,2}rev")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment