您可以调整现有的删除标点符号以满足您的需求。例如
removeMostPunctuation<-
function (x, preserve_intra_word_dashes = FALSE)
{
rmpunct <- function(x) {
x <- gsub("#", "\002", x)
x <- gsub("[[:punct:]]+", "", x)
gsub("\002", "#", x, fixed = TRUE)
}
if (preserve_intra_word_dashes) {
x <- gsub("(\\w)-(\\w)", "\\1\001\\2", x)
x <- rmpunct(x)
gsub("\001", "-", x, fixed = TRUE)
} else {
rmpunct(x)
}
}
这会给你
removeMostPunctuation("hello #hastag @money yeah!! o.k.")
# [1] "hello #hastag money yeah ok"
当你将它与 tm_map 一起使用时,但一定要将它包装在content_transformer()
tweetCorpus <- tm_map(tweetCorpus, content_transformer(removeMostPunctuation),
preserve_intra_word_dashes = TRUE)