Skip to content

Commit 7454707

Browse files
authored
Add maskWords.R (#144)
1 parent b46d3ac commit 7454707

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

string_manipulation/maskWords.R

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
maskWords <- function(text, mask) {
2+
text_split <- c(unlist(strsplit(text, split = " ")))
3+
4+
post_n <- c()
5+
for (i in text_split) {
6+
post_n <- c(
7+
post_n,
8+
if (i %in% c(
9+
"birds",
10+
"BIRDS",
11+
"Birds",
12+
"market",
13+
"Market",
14+
"MARKET",
15+
"street",
16+
"STREET",
17+
"Street"
18+
)) {
19+
tolower(i)
20+
} else {
21+
i
22+
}
23+
)
24+
}
25+
26+
clean_text <- gsub("\\b(birds|street|market)\\b", mask, post_n)
27+
28+
clean_text <- gsub("\n", "", clean_text)
29+
30+
return(paste(clean_text, collapse = " "))
31+
}
32+
33+
post <- "The lady bought groceries from the market, but some of them spilled on the street, and the birds helped themselves."
34+
35+
maskWords(text = post,mask = "$$$")

0 commit comments

Comments
 (0)