获取潜在语义分析 (lsa) 对象并对 R 中的新数据进行评分



我正在使用R中的textmineR运行潜在语义分析(LSA(。我希望得到的是按主题矩阵列出的文档,其中包含按文档进行主题分数,我可以通过从我的 lsa 对象(下图(调用 theta 来完成。但是,我在获取我创建的 lsa 对象并使用它来对新数据集(即文档术语矩阵,dtm(进行评分时遇到了挑战,以便我可以在新数据上应用我预先存在的主题结构。在下面的示例中,我创建了两个主题,然后当我尝试使用相同的确切的 dtm 时(为了此示例而假装它是一个新文件(,我收到以下错误:

"Error in predict.lsa_topic_model(model, dtm_m) : newdata must be a matrix of class dgCMatrix or a numeric vector"

我需要使用 lsa 对象对新文本进行评分。有没有我缺少的简单解决方案?我没有运气将矩阵强制为"dgCMatrix"。我实际上也不知道如何使用 lsa 等其他软件包来做到这一点。对这种方法的任何帮助将不胜感激。

file = as.data.frame(matrix( c('case1', 'this is some SAMPLE TEXT!',
'case2',  'and this is the 2nd version of that text...', 
'case3', 'more stuff to talk about'), 
        nrow=3,              
        ncol=2,              
        byrow = TRUE))
names(file) [1] <- 'doc_id'
names(file) [2] <- 'text'
library(tm)
wordCorpus <- Corpus(DataframeSource(file))
cleaner <- function (wordCorpus) {
  wordCorpus <- tm_map(wordCorpus, removeNumbers)
  wordCorpus <- tm_map(wordCorpus, content_transformer(tolower))
  wordCorpus <- tm_map(wordCorpus, removePunctuation)
  return (wordCorpus)
}
wordCorpus <- cleaner (wordCorpus)
tokenizer <- function(x) 
  NGramTokenizer(x, Weka_control(min = 1, max = 2))
dtm  <- DocumentTermMatrix (wordCorpus, control = list (tokenize=tokenizer, weighting = weightTfIdf))
dtm_m <- as.matrix(dtm)
library(textmineR)
model <- FitLsaModel(dtm = dtm_m,  k = 2)
#this is what I want to get, but ideally also 
#be able to save the "model" object and use to create this in a new sample`
values <- as.data.frame (model$theta)
values
#pretending my original dataset is a new sample and using predict
values_other <- predict (model, dtm_m)

对于这样的工作流程,您可以非常安全地完全跳过使用tm,而直接使用 textmineRCreateDtm函数。

请参阅LSA示例作为textmineR 的小插图的一部分,其中显示了这个确切的工作流程。 https://cran.r-project.org/web/packages/textmineR/vignettes/c_topic_modeling.html

最新更新