我无法使用tm_map删除•和其他一些特殊字符,例如' -

时间:2017-03-22 03:29:54

标签: r gsub tm quanteda

我在第一组命令中搜索问题并能够替换。 但是,当我申请我的语料库时,它不起作用,•仍然出现。 语料库有6570个元素,2.3mb,所以它似乎是有效的。

> x <- ". R Tutorial"
> gsub("•","",x)
[1] ". R Tutorial"

> removeSpecialChars <- function(x) gsub("•","",x)
> corpus2=tm_map(corpus2, removeSpecialChars)
> print(corpus2[[6299]][1])
[1] "• R tutorial • success– october"
> ##remove special characters

1 个答案:

答案 0 :(得分:0)

对于使用语料库对象以更直接的方式工作的替代方法,这是怎么回事?

require(quanteda)
require(magrittr)

corpus3 <- corpus(c("• R Tutorial", "More of these • characters •", "Tricky •!"))

# remove the character from the tokenized corpus
tokens(corpus3)
## tokens from 3 documents.
## text1 :
## [1] "R"        "Tutorial"
## 
## text2 :
## [1] "More"       "of"         "these"      "characters"
## 
## text3 :
## [1] "Tricky" "!"  
tokens(corpus3) %>% tokens_remove("•")
## tokens from 3 documents.
## [1] "R"        "Tutorial"
## text1 :
## 
## text2 :
## [1] "More"       "of"         "these"      "characters"
## 
## text3 :
## [1]] "Tricky" "!"  

# remove the character from the corpus itself
texts(corpus3) <- gsub("•", "", texts(corpus3), fixed = TRUE)
texts(corpus3)
##         text1                        text2                        text3 
## " R Tutorial" "More of these  characters "                   "Tricky !"