如何解决如何使用R中的seededlda包来保留主题的用户标识
我一直在尝试对 MOOC 中的一组讨论论坛帖子进行主题建模。我尝试过基本的LDA来创建主题,主题没有意义。所以现在我正在研究播种我的主题以创建更好的主题。我找到了 seededlda 包,它需要一个 dfm 作为输入以及一个种子术语字典。它运作良好!我的问题是弄清楚每个文档或论坛帖子是如何分类的。
我的原始数据将“userid”作为变量,将“post”作为我用于 LDA 的文档。到目前为止,我的代码看起来像这样。
text <- introduction_posts$post
dfmt <- dfm(text,remove_number = TRUE) %>%
dfm_remove(stopwords('en'),min_nchar = 2)
#install.packages("seededlda")
library(seededlda)
slda <- textmodel_seededlda(dfmt,seeded_dict,valuetype = c("glob","regex","fixed"),case_insensitive = FALSE,residual = TRUE,weight = 0.01,max_iter = 2000,alpha = NULL,beta = NULL,verbose = quanteda_options("verbose")
)
terms <- terms(slda)
如何确定哪些字词属于哪个用户?
当我使用topicmodeling包下的LDA函数时,我使用了这样定义的文档术语矩阵
posts_dtm <- CreateDtm(doc_vec = introduction_posts$post,# character vector of documents
doc_names = introduction_posts$userid_bycourse,# document names
ngram_window = c(1,2),# minimum and maximum n-gram length
stopword_vec = c(stopwords::stopwords("en"),# stopwords from tm
stopwords::stopwords(source = "smart"))
它在文件进行时命名。最后,我能够很好地看到哪些主题属于哪些参与者。但是我似乎无法使用 seededlda 包使用的 dfm 来做到这一点。
任何帮助将不胜感激。
解决方法
在我看来,更多的是关于如何使用 quanteda 而非 seeddlda 构建 dfm。
dat <- data.frame(user = c("user1","user2","user3","user4","user5"),post = c("a f","b dd","e g","g a","f b"))
dat
# user post
# 1 user1 a f
# 2 user2 b dd
# 3 user3 e g
# 4 user4 g a
# 5 user5 f b
corp <- corpus(dat,docid_field = "user",text_field = "post")
dfmt <- dfm(corp)
dfmt
# Document-feature matrix of: 5 documents,6 features (66.7% sparse).
# features
# docs a f b dd e g
# user1 1 1 0 0 0 0
# user2 0 0 1 1 0 0
# user3 0 0 0 0 1 1
# user4 1 0 0 0 0 1
# user5 0 1 1 0 0 0
对于 seeddlda,它的 topics()
不返回带有文档名称的向量,但您可以给出名称。
topic <- topics(slda)
names(topic) <- docnames(dfmt)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。