【问题标题】:DataframeSource reading from text file with multiline documentsDataframeSource 从带有多行文档的文本文件中读取
【发布时间】:2019-12-13 05:40:42
【问题描述】:

您好,我正在尝试使用 R 中的 tm 包从 CSV 文档文件创建语料库。通过阅读文档,我了解到 DataframeSource 需要两列:1)唯一标识符和 2)文本。每行都应该是一个新文档。

doc_id text
1      Document 1
2      Document 2

我有一个 sourcedata 文件,其中包含许多跨多行的文档。

doc_id text
1      Document 1
1      Document 1 continued
2      Document 2
2      Document 2 continued

有没有一种快速而整洁的方法来读取与同一文档具有相同标识符的所有内容?谢谢!

【问题讨论】:

    标签: r tm


    【解决方案1】:

    您能否利用group_bypaste 将字符放入单个字段中?一种使用 dplyr 的方法:

    library(tidyverse)
    
    data <- data.frame(doc_id = as.character(c(1,1,2,2)), text_old = c('text 1', 'text 1 
            cont.','text 2', 'text 2 cont.'), stringsAsFactors = FALSE)
    
    x <- data %>% 
      group_by(doc_id) %>% 
      mutate(text = paste(text_old, collapse = ",")) %>% 
      select(doc_id, text) %>% 
      unique() %>% 
      as.data.frame() %>% 
      DataframeSource()
    
    x <- Corpus(x)
    
    inspect(x)
    
    <<SimpleCorpus>>
    Metadata:  corpus specific: 1, document level (indexed): 0
    Content:  documents: 2
    
                      1                   2 
     text 1,text 1 cont. text 2,text 2 cont. 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 2014-12-19
      • 2011-10-24
      • 2022-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多