您的问题有三种(或更多)解决方案:
首先,tm
仅将包用于删除单词。两个包都处理相同的对象,因此您可以tm
仅用于删除单词而不是RTextTools
包。即使您查看函数内部,create_matrix
它也使用tm
函数。
二是修改create_matrix
功能。例如添加一个输入参数,own_stopwords=NULL
并添加以下行:
# existing line
corpus <- Corpus(VectorSource(trainingColumn),
readerControl = list(language = language))
# after that add this new line
if(!is.null(own_stopwords)) corpus <- tm_map(corpus, removeWords,
words=as.character(own_stopwords))
第三,编写自己的函数,如下所示:
# excluder function
remove_my_stopwords<-function(own_stw, dtm){
ind<-sapply(own_stw, function(x, words){
if(any(x==words)) return(which(x==words)) else return(NA)
}, words=colnames(dtm))
return(dtm[ ,-c(na.omit(ind))])
}
让我们看看它是否有效:
# let´s test it
data(NYTimes)
data <- NYTimes[sample(1:3100, size=10,replace=FALSE),]
matrix <- create_matrix(cbind(data["Title"], data["Subject"]))
head(colnames(matrix), 5)
# [1] "109" "200th" "abc" "amid" "anniversary"
# let´s consider some "own" stopwords as words above
ostw <- head(colnames(matrix), 5)
matrix2<-remove_my_stopwords(own_stw=ostw, dtm=matrix)
# check if they are still there
sapply(ostw, function(x, words) any(x==words), words=colnames(matrix2))
#109 200th abc amid anniversary
#FALSE FALSE FALSE FALSE FALSE
高温高压