我试图在使用stm包编写的文档项矩阵上运行结构化主题模型(使用tm包)。
我在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-compatible矩阵:
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-compatible数据指定元数据时,元数据就消失了:
docsTM <- DTM_st$documents # works fine
vocabTM <- DTM_st$vocab # works fine
metaTM <- DTM_st$meta # returns NULL
> metaTM
NULL如何将tm-generated语料库中的元数据保存在stm-compatible文档项矩阵中?欢迎有任何建议,谢谢。
发布于 2017-12-08 13:02:59
试试看quanteda包如何?
如果没有访问您的对象的能力,我不能保证这是逐字工作的,但它应该:
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 variableshttps://stackoverflow.com/questions/47652890
复制相似问题