【问题标题】:Topics proportion over time using Mallet LDA使用 Mallet LDA 随时间变化的主题比例
【发布时间】:2018-02-14 22:39:03
【问题描述】:

我想知道如何通过我的语料库中的 130 个 .txt 文件(每月数据)中的句子来训练槌 LDA。由于我在文档级别估计时面临的问题是,主题比例加班图非常奇怪。例如,随着时间的推移,比例仍然没有变化,在某些主题中比例没有变化。

这是我使用的编码脚本。

dir <- "C:/Users/Dell/desktop/MPSCLEANED"
setwd(dir)
require(mallet)

documents <- mallet.read.dir(dir) 
mallet.instances <- mallet.import(documents$id, documents$text, 
"C:/Users/Dell/desktop/stopwords.txt", token.regexp = "\\p{L}
[\\p{L}\\p{P}]+\\p{L}")

# Before moving on, I just wonder how can I estimate LDA by sentences from 
all documents in my corpus. 

n.topics <- 15
topic.model  <- MalletLDA(n.topics, alpha.sum=3.33, beta=0.2)
topic.model$model$setRandomSeed(19820L)
topic.model$setOptimizeInterval(50L)
topic.model$loadDocuments(mallet.instances)
vocabulary <- topic.model$getVocabulary()
word.freqs <- mallet.word.freqs(topic.model)
topic.model$setAlphaOptimization(1,1000)
topic.model$train(1000)
topic.model$maximize(20)

doc.topics <- mallet.doc.topics(topic.model, smoothed=T, normalized=T)
topic.words <- mallet.topic.words(topic.model, smoothed=T, normalized=T)

topic.docs <- t(doc.topics)
topic.docs <- topic.docs / rowSums(topic.docs)


topics.labels <- rep("", n.topics)
for (topic in 1:n.topics) topics.labels[topic] <- 
paste(mallet.top.words(topic.model, topic.words[topic,], num.top.words=5) 
$words, collapse=" ")

topics.labels

#Topics over time
options(java.parameters="-Xmx2g")
library("dfrtopics")
library("dplyr")
library("ggplot2")
library("lubridate")
library("stringr")
library("mallet")

m <- mallet_model(doc_topics = doc.topics, doc_ids = documents$id, vocab = 
vocabulary, topic_words = topic.words, model = topic.model)

pd <- data.frame(date = list.files(path = "C:/Users/Dell/Desktop/MPS"))

pd <- data.frame(date = lapply(pd, function(x) {gsub(".txt", "", x)}))

meta <- data.frame(id = documents$id, pubdate = as.Date(pd$date, "%Y%m%d"))

metadata(m) <- meta

# Visualize topics over time
theme_update(strip.text=element_text(size=7), 
axis.text=element_text(size=7))
topic_series(m) %>%
plot_series(labels=topic_labels(m, 2))

【问题讨论】:

    标签: r lda mallet


    【解决方案1】:

    130 个文档不太适合估计主题模型。文档可以细分成更小的片段吗?

    【讨论】:

    • 完全正确。其实我也很想这么做!但问题是我不知道该怎么做,因为这是我第一次做 NLP。您对在训练 MALLET LDA 之前将这些文档标记为句子的方式有什么建议吗?
    猜你喜欢
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 2020-06-09
    • 2021-10-05
    • 2017-11-28
    • 2017-10-29
    • 2012-12-17
    相关资源
    最近更新 更多