使用 tm 检查文档-术语矩阵的相应术语(vocab?英文)

Inspect the corresponding terms (vocab? in English) of document-term matrix using tm

嗨,这一定是超级基础的:

我正在使用 tm 包从语料库创建文档术语矩阵,因此矩阵的列名是语料库中术语的索引。谁能告诉我如何检查我的语料库中与矩阵中的这些索引相对应的原始单词?非常感谢!!

实际上是行名称是术语的索引。这是你想要的吗?

library(tm)
docs <- c("This is a text.", 
          "This another one.", 
          "This is some more.")

cor  <- Corpus(VectorSource(docs))
tdm  <- TermDocumentMatrix(cor, control=list(tolower=TRUE, removePunctuation=TRUE))
as.matrix(tdm)
#          Docs
# Terms     1 2 3
#   another 0 1 0
#   more    0 0 1
#   one     0 1 0
#   some    0 0 1
#   text    1 0 0
#   this    1 1 1

以后请务必包含您的数据的代表性示例。

我有点怀疑你在尝试将单词连接回原始语料库并获取语料库索引。如果是这样,您可以使用@jlhoward 的示例来执行此操作:

library(tm)
docs <- c("This is a text. And me too.", 
          "This another one.", 
          "This is some more.")

cor  <- Corpus(VectorSource(docs))
tdm  <- TermDocumentMatrix(cor, control=list(tolower=TRUE, removePunctuation=TRUE))
as.matrix(tdm)

txt <- sapply(cor, function(x) x[[1]])


setNames(lapply(rownames(tdm), function(x){
   grep(x, txt, ignore.case=TRUE)
}), rownames(tdm))

## $another
## [1] 2
## 
## $more
## [1] 3
## 
## $one
## [1] 2
## 
## $some
## [1] 3
## 
## $text
## [1] 1
## 
## $this
## [1] 1 2 3