R tm自定义删除除hashtag之外的标点符号
我有一个twitter上的推特文集。我清理了这个语料库(删除单词、tolower、删除URL),最后还想删除标点符号 这是我的密码:R tm自定义删除除hashtag之外的标点符号,r,customization,text-processing,tm,punctuation,R,Customization,Text Processing,Tm,Punctuation,我有一个twitter上的推特文集。我清理了这个语料库(删除单词、tolower、删除URL),最后还想删除标点符号 这是我的密码: tweetCorpus <- tm_map(tweetCorpus, removePunctuation, preserve_intra_word_dashes = TRUE) tweetCorpus您可以调整现有的Remove标点符号以满足您的需要。比如说 removeMostPunctuation<- function (x, preserve_
tweetCorpus <- tm_map(tweetCorpus, removePunctuation, preserve_intra_word_dashes = TRUE)
tweetCorpus您可以调整现有的Remove标点符号以满足您的需要。比如说
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)
}
}
当您将其与tm_地图一起使用时,请确保将其包装在content_transformer()
tweetcompus我维护的qdap包具有strip
功能来处理此问题,您可以指定不剥离的字符:
library(qdap)
strip("hello #hastag @money yeah!! o.k.", char.keep="#")
这里它被应用于语料库
:
library(tm)
tweetCorpus <- Corpus(VectorSource("hello #hastag @money yeah!! o.k."))
tm_map(tweetCorpus, content_transformer(strip), char.keep="#")
这是相当神秘的,额外的步骤是使用哨兵\001、\002临时保护“#”和“-”不被删除。为了避免破坏Unicode标点符号,您是否不希望在没有这些符号的情况下简单地展开[[:punct:][]
?
library(qdap)
strip("hello #hastag @money yeah!! o.k.", char.keep="#")
library(tm)
tweetCorpus <- Corpus(VectorSource("hello #hastag @money yeah!! o.k."))
tm_map(tweetCorpus, content_transformer(strip), char.keep="#")
removeMostPunctuation <- function(text, keep = "#") {
m <- sub_holder(keep, text)
m$unhold(strip(m$output))
}
removeMostPunctuation("hello #hastag @money yeah!! o.k.")
## "hello #hastag money yeah ok"