我正在尝试在使用tm
软件包制备的文档矩阵上运行结构性主题模型(使用stm
软件包)。
我在tm
软件包中构建了一个语料库,其中包含以下元数据:
library(tm)
myReader2 <- readTabular(mapping=list(content="text", id="id", sentiment = "sentiment"))
text_corpus2 <- VCorpus(DataframeSource(bin_stm_df), readerControl = list(reader = myReader2))
meta(text_corpus2[[1]])
id : 11
sentiment: negative
language : en
执行了一些文本清洁并保存结果为clean_corpus2
(元数据仍然存在),我将其更改为文档矩阵,然后将其读为stm
-兼容矩阵:
library(stm)
chat_DTM2 <- DocumentTermMatrix(clean_corpus2, control = list(wordLengths = c(3, Inf)))
DTM2 <- removeSparseTerms(chat_DTM2 , 0.990)
DTM_st <-readCorpus(DTM2, type = "slam")
到目前为止,一切都很好。但是,当我尝试使用 stm
-兼容数据指定元数据时,元数据消失了:
docsTM <- DTM_st$documents # works fine
vocabTM <- DTM_st$vocab # works fine
metaTM <- DTM_st$meta # returns NULL
> metaTM
NULL
如何将元数据从tm
生成的语料库中保持在stm
-兼容的文档 - 学期矩阵中?欢迎任何建议,谢谢。
如何尝试 Quanteda package?
没有访问您的对象的能力,我无法逐字保证这项工作,但应该:
library("quanteda")
# creates the corpus with document variables except for the "text"
text_corpus3 <- corpus(bin_stm_df, text_field = "text")
# convert to document-feature matrix - cleaning options can be added
# see ?tokens
chat_DTM3 <- dfm(text_corpus3)
# similar to tm::removeSparseTerms()
DTM3 <- dfm_trim(chat_DTM3, sparsity = 0.990)
# convert to STM format
DTM_st <- convert(DTM3, to = "stm")
# then it's all there
docsTM <- DTM_st$documents
vocabTM <- DTM_st$vocab
metaTM <- DTM_st$meta # should return the data.frame of document variables