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

Naive Bayes in R, Sentiment analysis leads to cannot coerce class error

我有一个数据框 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

我该如何解决这个问题?

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