从 R 中的 LDA 模型中获取术语权重

Getting term weights out of an LDA model in R

我想知道是否有人知道使用 topicmodels 包从 R 中构建的主题模型中提取术语权重/概率的方法。

按照下面的示例 link 我创建了一个主题模型,如下所示:

Gibbs = LDA(JSS_dtm, k = 4, 
            method = "Gibbs",
            control = list(seed = 1, burnin = 1000, thin = 100, iter = 1000))

然后我们可以使用 topics(Gibbs,1) 获取主题,使用 terms(Gibbs,10) 获取术语,甚至使用 Gibbs@gamma 获取主题概率,但在查看 str(Gibbs) 之后,似乎有无法获得每个主题中的术语概率。这将很有用,因为主题 1 可能是 50% 的术语 A 和 50% 的术语 B,而主题 2 可能是 90% 的术语 C 和 10% 的术语 D。我知道 MALLET 和 Python 等工具NLTK 模块提供此功能,但我也希望 R 中可能存在类似的解决方案。

如果有人知道如何实现,请告诉我们。

非常感谢!

编辑:

为了其他人的利益,我想我会分享我目前的解决方法。如果我知道术语概率,我就能将它们可视化并让观众更好地理解每个主题的含义,但如果没有概率,我只是按每个主题分解我的数据并为每个主题创建一个词云使用二进制权重的主题。虽然这些值不是概率,但它们表明了每个主题的重点。

见以下代码:

JSS_text   <- sapply(1:length(JSS_papers[,"description"]), function(x) unlist(JSS_papers[x,"description"]))
jss_df     <- data.frame(text=JSS_text,topic=topics(Gibbs, 1))
jss_dec_df <- data.frame()

for(i in unique(topics(Gibbs, 1))){
  jss_dec_df <- rbind(jss_dec_df,data.frame(topic = i, 
                                            text = paste(jss_df[jss_df$topic==i,"text"],collapse=" ")))
}

corpus <- Corpus(VectorSource(jss_dec_df$text))
JSS_dtm <- TermDocumentMatrix(corpus,control = list(stemming = TRUE, 
                                                    stopwords = TRUE, 
                                                    minWordLength = 3,
                                                    removeNumbers = TRUE, 
                                                    removePunctuation = TRUE,
                                                    function(x)weightSMART(x,spec="bnc")))

(JSS_dtm  = removeSparseTerms(JSS_dtm,0.1)) # not the sparsity parameter 

library(wordcloud)
comparison.cloud(as.matrix(JSS_dtm),random.order=F,max.words=100, 
                 scale=c(6,0.6),colours=4,title.size=2)

想出来了——要获得术语权重,请使用 posterior(lda_object)$terms。结果比我想象的要容易得多!