R中的朴素贝叶斯,情感分析导致无法强迫类错误

时间:2016-04-07 17:21:35

标签: r sentiment-analysis

我的数据框df_tweets有两列tweetsscore。 分数是值1 to 5

之间的因子
getMatrix <- function(chrVect){
 testsource <- VectorSource(chrVect)
 testcorpus <- Corpus(testsource)
 testcorpus <- tm_map(testcorpus,stripWhitespace)
 testcorpus <- tm_map(testcorpus, removeWords, stopwords('french'))
 testcorpus <- tm_map(testcorpus, removeWords, stopwords('english'))
 testcorpus <- tm_map(testcorpus, content_transformer(tolower))
 testcorpus <- tm_map(testcorpus, removePunctuation)
 testcorpus <- tm_map(testcorpus, removeNumbers)
 testcorpus <- tm_map(testcorpus, PlainTextDocument)

 return(DocumentTermMatrix(testcorpus))
}

op =getMatrix(df_tweets$text)
classifier <-naiveBayes(as.matrix(op), as.factor(df_tweets$avg_score))

当我使用预测功能时,我收到错误

myPrediction<- predict(classifier,op)

Error in as.data.frame.default(newdata) : 
cannot coerce class "c("DocumentTermMatrix", "simple_triplet_matrix")" to a data.frame

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我相信你可以用as.matrixas.data.frame直接包裹as.matrix.data.frame