【问题标题】:Convert topicmodels output to JSON将 topicmodels 输出转换为 JSON
【发布时间】:2017-07-27 20:03:55
【问题描述】:

我使用以下函数将 topicmodels 输出转换为 JSON 输出以在 ldavis 中使用。

topicmodels_json_ldavis <- function(fitted, corpus, doc_term){
     ## Required packages
     library(topicmodels)
     library(dplyr)
     library(stringi)
     library(tm)
     library(LDAvis)

     ## Find required quantities
     phi <- posterior(fitted)$terms %>% as.matrix
     theta <- posterior(fitted)$topics %>% as.matrix
     vocab <- colnames(phi)
     doc_length <- vector()
     for (i in 1:length(corpus)) {
          temp <- paste(corpus[[i]]$content, collapse = ' ')
          doc_length <- c(doc_length, stri_count(temp, regex = '\\S+'))
     }
     temp_frequency <- inspect(doc_term)
     freq_matrix <- data.frame(ST = colnames(temp_frequency),
                               Freq = colSums(temp_frequency))
     rm(temp_frequency)

     ## Convert to json
     json_lda <- LDAvis::createJSON(phi = phi, theta = theta,
                                    vocab = vocab,
                                    doc.length = doc_length,
                                    term.frequency = freq_matrix$Freq)

     return(json_lda)
}

但我收到以下错误

LDAvis 中的错误::createJSON(phi = phi, theta = theta, vocab = vocab, doc.length = doc_length, : doc.length 的长度不相等 到 theta 中的行数;两者都应该等于 数据中的文档。

这是我的完整代码:

data <- read.csv("textmining.csv")


corpus <- Corpus(DataframeSource(data.frame(data$reasonforleaving))) 

# Remove punctuations and numbers because they are generally uninformative.
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, removeNumbers)
# Convert all words to lowercase.
corpus <- tm_map(corpus, content_transformer(tolower))
# Remove stopwords such as "a", "the", etc.
corpus <- tm_map(corpus, removeWords, stopwords("english"))
# Use the SnowballC package to do stemming.
library(SnowballC)
corpus <- tm_map(corpus, stemDocument)


# remove extra words
toSpace <- content_transformer(function (x , pattern ) gsub(pattern, " ", x))
corpus <- tm_map(corpus, toSpace, "still")
corpus <- tm_map(corpus, toSpace, "also")

# Remove excess white spaces between words.

corpus <- tm_map(corpus, stripWhitespace)
# Inspect the first document to see what it looks like.
corpus[[1]]$content 

dtm <- DocumentTermMatrix(corpus)

# remove empty documents
library(slam)
dtm = dtm[row_sums(dtm)>0,]

# Use topicmodels package to conduct LDA analysis.

burnin <- 500
iter <- 1000
keep <- 30
k <- 5

result55 <- LDA(dtm, 5)
ldaoutput = topicmodels_json_ldavis(result55,corpus, dtm)

你知道我为什么会收到错误吗?

谢谢

【问题讨论】:

    标签: text-mining lda topic-modeling


    【解决方案1】:

    我在相同的代码中遇到了同样的问题,并找到了这个函数here

    topicmodels2LDAvis <- function(x, ...){
        post <- topicmodels::posterior(x)
        if (ncol(post[["topics"]]) < 3) stop("The model must contain > 2 topics")
        mat <- x@wordassignments
        LDAvis::createJSON(
            phi = post[["terms"]], 
            theta = post[["topics"]],
            vocab = colnames(post[["terms"]]),
            doc.length = slam::row_sums(mat, na.rm = TRUE),
            term.frequency = slam::col_sums(mat, na.rm = TRUE)
        )
    }
    

    使用起来要简单得多,只需将您的 LDA 结果作为参数:

    result55 <- LDA(dtm, 5)
    serVis(topicmodels2LDAvis(result55))
    

    【讨论】:

    • 谢谢,效果很好,而且没有额外的语料库和 dtm 参数就更简洁了。
    【解决方案2】:

    问题

    你的问题在for (i in 1:length(corpus))

     doc_length <- vector()
         for (i in 1:length(corpus)) {
              temp <- paste(corpus[[i]]$content, collapse = ' ')
              doc_length <- c(doc_length, stri_count(temp, regex = '\\S+'))
         }
    

    请记住,您已从 dtm = dtm[row_sums(dtm)&gt;0,] 的 DocumentTermMatrix 中删除了一些“空”文档, 所以这里的向量长度会太大。

    建议

    您可能希望保留一个空文档的向量,因为它不仅可以帮助您生成 JSON,还可以帮助您在空文档集和完整文档集之间来回切换。
    doc.length = colSums( as.matrix(tdm) &gt; 0 )[!empty.docs]

    我的建议假设您拥有完整的 tdm 和空文档

    【讨论】:

    • 非常感谢!这是有道理的。
    猜你喜欢
    • 2017-02-04
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2018-12-23
    • 2021-01-01
    相关资源
    最近更新 更多