use*_*594 6 r text-mining stop-words tm
使用tm包我可以这样做:
c0 <- Corpus(VectorSource(text))
c0 <- tm_map(c0, removeWords, c(stopwords("english"),mystopwords))
Run Code Online (Sandbox Code Playgroud)
mystopwords 是我要删除的附加停用词的向量.
但我找不到使用RTextTools包的等效方法.例如:
dtm <- create_matrix(text,language="english",
removePunctuation=T,
stripWhitespace=T,
toLower=T,
removeStopwords=T, #no clear way to specify a custom list here!
stemWords=T)
Run Code Online (Sandbox Code Playgroud)
是否有可能做到这一点?我真的很喜欢这个RTextTools界面,很遗憾必须回到原点tm.
小智 3
您的问题有三种(或可能更多)解决方案:
\n\n首先,该tm包仅用于删除单词。两个包处理相同的对象,因此您可以tm仅用于删除单词而不是RTextTools包。即使你查看函数内部,create_matrix它也使用tm函数。
二、修改create_matrix功能。例如,添加一个输入参数,例如own_stopwords=NULL并添加以下行:
# existing line\ncorpus <- Corpus(VectorSource(trainingColumn), \n readerControl = list(language = language))\n# after that add this new line\nif(!is.null(own_stopwords)) corpus <- tm_map(corpus, removeWords, \n words=as.character(own_stopwords))\nRun Code Online (Sandbox Code Playgroud)\n\n第三,编写自己的函数,如下所示:
\n\n# excluder function\nremove_my_stopwords<-function(own_stw, dtm){\n ind<-sapply(own_stw, function(x, words){\n if(any(x==words)) return(which(x==words)) else return(NA)\n }, words=colnames(dtm))\n return(dtm[ ,-c(na.omit(ind))]) \n}\nRun Code Online (Sandbox Code Playgroud)\n\n让\xc2\xb4s 看看是否有效:
\n\n# let\xc2\xb4s test it\ndata(NYTimes)\ndata <- NYTimes[sample(1:3100, size=10,replace=FALSE),]\nmatrix <- create_matrix(cbind(data["Title"], data["Subject"]))\n\nhead(colnames(matrix), 5)\n# [1] "109" "200th" "abc" "amid" "anniversary"\n\n\n# let\xc2\xb4s consider some "own" stopwords as words above\nostw <- head(colnames(matrix), 5)\n\nmatrix2<-remove_my_stopwords(own_stw=ostw, dtm=matrix)\n\n# check if they are still there\nsapply(ostw, function(x, words) any(x==words), words=colnames(matrix2))\n#109 200th abc amid anniversary \n#FALSE FALSE FALSE FALSE FALSE \nRun Code Online (Sandbox Code Playgroud)\n\n华泰
\n