Skip to content

Commit

Permalink
Add maskWords.R (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifeanyi55 authored Nov 26, 2024
1 parent b46d3ac commit 7454707
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions string_manipulation/maskWords.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
maskWords <- function(text, mask) {
text_split <- c(unlist(strsplit(text, split = " ")))

post_n <- c()
for (i in text_split) {
post_n <- c(
post_n,
if (i %in% c(
"birds",
"BIRDS",
"Birds",
"market",
"Market",
"MARKET",
"street",
"STREET",
"Street"
)) {
tolower(i)
} else {
i
}
)
}

clean_text <- gsub("\\b(birds|street|market)\\b", mask, post_n)

clean_text <- gsub("\n", "", clean_text)

return(paste(clean_text, collapse = " "))
}

post <- "The lady bought groceries from the market, but some of them spilled on the street, and the birds helped themselves."

maskWords(text = post,mask = "$$$")

0 comments on commit 7454707

Please sign in to comment.