简体   繁体   English

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

[英]I can't remove • and some other special characters such as '- using tm_map

I search through the questions and able to replace • in my first set of command. 我搜索所有问题,并能够在第一组命令中替换掉•。 But when I apply to my corpus, it doesn't work, the • still appear. 但是,当我申请我的语料库时,它不起作用,•仍然出现。 The corpus has 6570 elements,2.3mb, so it seems to be valid. 语料库有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

How about this for an alternative that works in a more straightforward way with corpus objects? 对于以更直接的方式与语料库对象一起工作的替代方法呢?

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 !" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM