From 74547073b5776205d9522f776c833df9954b2684 Mon Sep 17 00:00:00 2001 From: Ifeanyi Idiaye <72707830+Ifeanyi55@users.noreply.github.com> Date: Tue, 26 Nov 2024 17:10:10 +0100 Subject: [PATCH] Add maskWords.R (#144) --- string_manipulation/maskWords.R | 35 +++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 string_manipulation/maskWords.R diff --git a/string_manipulation/maskWords.R b/string_manipulation/maskWords.R new file mode 100644 index 0000000..24488de --- /dev/null +++ b/string_manipulation/maskWords.R @@ -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 = "$$$")