整理不起作用,出现此错误:LDA_Gibbs类的对象没有整理方法

时间:2018-09-12 07:55:57

标签: r tidyverse lda topic-modeling tidy

此代码显示错误:错误:LDA_Gibbs类的对象没有整洁的方法

我不明白为什么.....无论何时我尝试去做      test_lda_td2 <-tidy(test_lda2)这显示错误。 需要一些解决方案

library(NLP)
library(tm)
library(tidytext)
library(stringr)
library(tidyr)
library(topicmodels)
library(tidyverse)

mywords<- read.csv("mystop.csv",header =F,sep = ",")
mywords <- as.character(mywords)
mywords <- c(mywords, stopwords())

filelist = list.files(pattern = ".*.txt")
files <- lapply(filelist,readLines)

(docs <- VCorpus(VectorSource(files)))

docs <-tm_map(docs,content_transformer(tolower))
docs <- tm_map(docs, removePunctuation)
docs <- tm_map(docs, removeNumbers)
docs <- tm_map(docs, removeWords, mywords)

docs <- tm_map(docs, removeWords,stopwords("english"))
docs <- tm_map(docs, stripWhitespace)

toSpace <- content_transformer(function(x, pattern) { return (gsub(pattern, 
" " , x))})
docs <- tm_map(docs, toSpace, "-")
docs <- tm_map(docs, toSpace, "’")
docs <- tm_map(docs, toSpace, "‘")
docs <- tm_map(docs, toSpace, "•")
docs <- tm_map(docs, toSpace, "”")
docs <- tm_map(docs, toSpace, "“")

#...............
dtm <- DocumentTermMatrix(docs)
rownames(dtm) <- filelist
freq <- colSums(as.matrix(dtm))
length(freq)
ord <- order(freq,decreasing=TRUE)

d <- data.frame(word = names(freq),freq=freq[ord])

new_dtm<-dtm

burnin <- 4000
iter <- 2000
thin <- 500
seed <-list(2003,5,63,100001,765)
nstart <- 5
best <- TRUE
k <- 3
test_lda2 <-LDA(new_dtm,k, method="Gibbs",
control=list(nstart=nstart, seed = seed, best=best,
burnin = burnin, iter = iter, thin=thin))

test_lda_td2 <- tidy(test_lda2) <---Error

lda_top_terms2 <- test_lda_td2 %>%
group_by(topic) %>%
top_n(10, beta) %>%
ungroup() %>%
arrange(topic, -beta)

lda_top_terms2 %>%
mutate(term = reorder(term, beta)) %>%
ggplot(aes(term, beta, fill = factor(topic))) +
geom_bar(stat = "identity", show.legend = FALSE) +
facet_wrap(~ topic, scales = "free") +
coord_flip()

0 个答案:

没有答案