tm自定义removePunctuation(井号除外) [英] tm custom removePunctuation except hashtag
本文介绍了tm自定义removePunctuation(井号除外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有来自Twitter的推文集.我清理了这个语料库(removeWords,tolower,删除URls),最后也想删除标点符号.
I have a Corpus of tweets from twitter. I clean this corpus (removeWords, tolower, delete URls) and finally also want to remove punctuation.
这是我的代码:
tweetCorpus <- tm_map(tweetCorpus, removePunctuation, preserve_intra_word_dashes = TRUE)
现在的问题是,通过这样做,我还松了了井号(#).有没有办法使用tm_map删除标点符号,但保留主题标签?
The problem now is, that by doing so I also loose the hashtag (#). Is there a way to remove punctuation with tm_map but remain the hashtag?
推荐答案
您可以调整现有的removePunctuation来满足您的需求.例如
You could adapt the existing removePunctuation to suit your needs. For example
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()
and when you use it with tm_map, but sure to wrap it in content_transformer()
tweetCorpus <- tm_map(tweetCorpus, content_transformer(removeMostPunctuation),
preserve_intra_word_dashes = TRUE)
这篇关于tm自定义removePunctuation(井号除外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文