2017-03-22 25 views
0

我搜索问题并能够在第一组命令中替换。 但是,当我适用于我的语料库,它不起作用,仍然出现。 语料库有6570个元素,2.3mb,所以它似乎是有效的。我无法删除•和一些其他特殊字符,如' - 使用tm_map

> 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 
+0

您第一次调用'gsub()'并没有说明这一点,因为'''从'x'中缺少。但是,除此之外,我测试了它并且工作。我不知道你的实际问题是什么。 –

+0

我的问题是当它只是第一次调用时,gsub功能起作用。但是当我在第二次调用tm_map和语料库时将其应用于我的代码中时,它不能删除• – Etalo

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