2016-04-07 31 views
1

我有一個數據框df_tweets有兩列tweetsscore。 分數與價值觀1 to 5樸素貝葉斯在R,情感分析導致不能脅迫類錯誤

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 

我怎樣才能解決這個問題?

+0

也許用'as.data.frame'或直接用'as.matrix.data.frame'封裝'as.matrix'? –

+0

工作。如果您可以將其作爲答案發布,我可以接受它/或刪除我的帖子 –

回答

1

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