我正在编写主题建模的代码。我收到此错误。
install.packages("tm")
install.packages("topicmodels")
library(tm)
library(topicmodels)
docs <- Corpus(VectorSource(c(
"This is the first document about topic modeling.",
"Topic modeling is a popular technique in text analysis.",
"LDA is a common algorithm for topic modeling.",
"Text mining is an interesting field in data science."
)))
docs <- tm_map(docs, content_transformer(tolower)) # Convert text to lowercase
docs <- tm_map(docs, removePunctuation) # Remove punctuation
docs <- tm_map(docs, removeNumbers) # Remove numbers
docs <- tm_map(docs, removeWords, stopwords("english")) # Remove common English stopwords
docs <- tm_map(docs, stripWhitespace)
dtm <- DocumentTermMatrix(docs)
num_topics <- 3
lda_model <- LDA(dtm, k = num_topics)
topic_labels <- c("Topic 1: Introduction to Topic Modeling",
"Topic 2: Techniques in Text Analysis",
"Topic 3: LDA and Data Science")
terms(lda_model, 10)
topics <- topics(lda_model)
document_topics <- as.data.frame(topics)
colnames(document_topics) <- topic_labels
print(document_topics)
Error in names(x) <- value :
'names' attribute [3] must be the same length as the vector [1]
> traceback()
1: `colnames<-`(`*tmp*`, value = c("Topic 1: Introduction to Topic Modeling",
"Topic 2: Techniques in Text Analysis", "Topic 3: LDA and Data Science"
))
有人可以告诉我吗? 谢谢
我正在尝试打印主题名称,而不仅仅是数字。
最好的猜测是您正在尝试这样做:
(document_topics <-
data.frame(topics = factor(topics,
levels = seq(topic_labels),
labels = topic_labels
)))