【问题标题】:R: tm package, aggregate / join docsR: tm 包,聚合/加入文档
【发布时间】:2015-01-19 16:33:59
【问题描述】:

我找不到任何以前发布过的问题,所以也许你可以帮忙。

什么是基于元数据聚合 tm 语料库中数据的好方法(例如聚合不同作者的文本)?

至少有两种明显的方法可以做到:

  • tm 中的一个内置函数,允许在元数据特征上构建 DocumentTermMatrix。很遗憾,我无法发现这一点。
  • 一种基于表格中的一些外部元数据在语料库中连接文档的方法。它只会使用元数据来替换文档 ID。

因此您将拥有一个包含:DocumentId、AuthorName 的表

还有一个 tm 构建的语料库,其中包含大量文档。我知道将表格作为语料库对象的元数据引入并不难。

可以使用以下函数构建矩阵。

library(tm) # version 0.6, you seem to be using an older version
corpus  <-Corpus(DirSource("/directory-with-texts"),
 readerControl = list(language="lat"))

metadata <- data.frame(DocID, Author)

#A very crude way to enter metadata into the corpus (assumes the same sequence):
for (i in 1:length(corpus)) {
  attr(corpus[[i]], "Author") <- metadata$Author[i]
}

a_documenttermmatrix_by_DocId <-DocumentTermMatrix(corpus) 

您将如何构建一个矩阵来显示每个作者可能聚合多个文档而不是文档的频率?在这个阶段这样做会很有用,而不是在只有几个术语的后处理中。

a_documenttermmatrix_by_Author <- ?

非常感谢!

【问题讨论】:

    标签: r metadata aggregate tm word-frequency


    【解决方案1】:

    DocumentTermMatrix 实际上只是一个花哨的矩阵(来自 slam 库的 Simple Triplet Matrix),其中包含每个术语和文档的术语频率。按作者聚合来自多个文档的数据实际上只是为作者添加列。考虑将矩阵格式化为标准 R 矩阵并使用标准子集/聚合方法:

    # Format the document term matrix as a standard matrix.
    # The rownames of m become the document Id's
    # The colnames of m become the individual terms
    m <- as.matrix(dtm)
    
    # Transpose matrix to use the "by" operator.
    # Rows become individual terms
    # Columns become document ids
    # Group columns by Author
    # Aggregate column sums (word frequencies) for each author, resulting in a list.
    author.list <- by(t(m), metadata$Author, colSums)
    
    # Format the list as a matrix and do stuff with it
    author.dtm <- matrix(unlist(author.list), nrow = length(author.list), byrow = T)
    
    # Add column names (term) and row names (author)
    colnames(author.dtm) <- rownames(m)
    rownames(author.dtm) <- names(author.list)
    
    # View the resulting matrix
    View(author.dtm[1:10, 1:10])
    

    生成的矩阵将是一个标准矩阵,其中行是作者,列是单个术语。此时您应该能够进行任何您想做的分析。

    【讨论】:

      【解决方案2】:

      如果语料库文本可以在表格中找到,我有一个非常粗略的解决方法。然而,这对于“tm”格式的大型语料库没有多大帮助,但在其他情况下它可能会很方便。随意改进它,因为它非常粗糙!

      custom_term_matrix <- function(author_vector, text_vector)
      {
        author_vector <- factor(author_vector)
        temp <- data.frame(Author = levels(author_vector))
      
        for (i in 1:length(temp$Author)){
          temp$Content[i] <- paste(c(as.character(text_vector[author_vector ==
            levels(author_vector)[i]])), sep=" ", collapse="")
        }
      
        m <- list(id = "Author", content = "Content")
        myReader <- readTabular(mapping = m)
        mycorpus <- Corpus(DataframeSource(data1), readerControl = list(reader = myReader))
      
        custom_matrix <<- DocumentTermMatrix(mycorpus, control = 
          list(removePunctuation = TRUE))
      }
      

      tm 内部可能有一个函数,但我一直找不到,所以我将不胜感激!

      【讨论】:

        猜你喜欢
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        • 2013-09-18
        • 2019-05-20
        • 1970-01-01
        • 2017-02-13
        • 2012-03-27
        • 1970-01-01
        相关资源
        最近更新 更多