【问题标题】:How do you combine multiple documents into a single document with topicmodels in r?如何使用 r 中的 topicmodels 将多个文档组合成一个文档?
【发布时间】:2021-02-20 18:35:29
【问题描述】:

我目前正在尝试使用 topicmodels 包将语料库的多个文档组合成一个文档。我最初通过多个 csv 导入我的数据,每个 csv 都有多行文本。但是,当我导入每个 csv 时,csv 的每一行都被视为一个文档,每个 csv 都被视为一个语料库。我想做的是将每个 csv 的每个文档/行合并到一个文档中,然后每个 csv 将代表我的语料库中的一个文档。我不确定这是否可能——也许在最初导入然后创建文档和语料库时以某种方式将 csv 的所有行作为单个文本文件读取会更容易,但我不知道如何也这样做。下面是我用来导入 csvs 的代码:

file <- read.csv("file.csv")
fileCorp <- VCorpus(VectorSource(file$text))

csv 中的行看起来像这样(其中每个 / 代表一个换行符):'I walk' / 'the dog' / 'at the' / 'park last night'

我想将这些行中的每一行合并成一行文本,作为我语料库中的一个文档。

感谢您的帮助!

【问题讨论】:

    标签: r csv nlp corpus topicmodels


    【解决方案1】:

    您的任务可以通过以下步骤完成:

    file1 <- data.frame(text = c('I walked','the dog','at the','park last night'))
    file2 <- data.frame(text = c('He walked','the cat','at the','yesterday'))
    
    data.frame(id = c(1, 2), 
               text = c(paste(file1$text, collapse = " "),
                        paste(file2$text, collapse = " ")))
    
      id                                    text
    1  1 I walked the dog at the park last night
    2  2      He walked the cat at the yesterday
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多