【问题标题】:How to create a quanteda corpus from a data.frame with multiple columns for text?如何从具有多列文本的 data.frame 创建 quanteda 语料库?
【发布时间】:2018-02-06 18:09:31
【问题描述】:

可以说我有以下几点:

x10 = data.frame(id = c(1,2,3),vars =c('top','down','top'), 
     text1=c('this is text','so is this','and this is too.'),
     text2=c('we have more text here','and here too','and look at this, more text.'))

我想使用以下方法在 quanteda 中创建一个 dfm/corpus:

x1 = corpus(x10,docid_field='id',text_field=c(3:4),tolower=T) 

显然这会出错,因为 text_field 只占用一列。除了建立两个语料库之外,我还有更好的方法来处理这个问题吗?我可以构建 2 然后在 id 上合并吗?是这样的吗?

【问题讨论】:

  • 我们为此打开了an issue,但哪种行为更自然:连接文本,或重复 id 变量以堆叠文本列(如下面的答案)?
  • 是的,我明白你的意思。我认为更直接的方法是重复 ID 变量,因为连接可能很麻烦。例如,我们的员工调查包含 3 个开放式问题(正面经验、负面经验,还有其他什么?),将这些问题结合起来真的很奇怪。

标签: r quanteda


【解决方案1】:

首先,让我们在不考虑字符值的情况下重新创建 data.frame:

x10 = data.frame(id = c(1,2,3), vars = c('top','down','top'), 
                 text1 = c('this is text', 'so is this', 'and this is too.'),
                 text2 = c('we have more text here', 'and here too', 'and look at this, more text.'),
                 stringsAsFactors = FALSE)

那么我们有两个选择。

方法一:重塑为“长”格式并创建单个语料库

首先“融化”数据,使之只有一列,然后作为语料库导入。 (另一种选择是tidy::gather()。)

x10b <- reshape2::melt(x10, id.vars = c("id", "vars"), 
                       measure.vars = c("text1", "text2"),
                       variable.name = "doc_id", value.name = "text")

# because corpus() takes document names from row names, by default 
row.names(x10b) <- paste(x10b$doc_id, x10b$id, sep = "_")

x10b
#         id vars doc_id                         text
# text1_1  1  top  text1                 this is text
# text1_2  2 down  text1                   so is this
# text1_3  3  top  text1             and this is too.
# text2_1  1  top  text2       we have more text here
# text2_2  2 down  text2                 and here too
# text2_3  3  top  text2 and look at this, more text.

x10_corpus <- corpus(x10b)
summary(x10_corpus)
# Corpus consisting of 6 documents:
#     
#    Text Types Tokens Sentences id vars doc_id
# text1_1     3      3         1  1  top  text1
# text1_2     3      3         1  2 down  text1
# text1_3     5      5         1  3  top  text1
# text2_1     5      5         1  1  top  text2
# text2_2     3      3         1  2 down  text2
# text2_3     8      8         1  3  top  text2
# 
# Source:  /Users/kbenoit/Dropbox (Personal)/GitHub/lse-my459/assignment-2/* on x86_64 by kbenoit
# Created: Tue Feb  6 19:06:07 2018
# Notes:    

方法2:制作两个语料库对象并合并

在这里,我们分别创建两个语料库对象,并使用+ 运算符将它们组合起来。

x10_corpus2 <- 
    corpus(x10[, -which(names(x10)=="text2")], text_field = "text1") +
    corpus(x10[, -which(names(x10)=="text1")], text_field = "text2")
summary(x10_corpus2)
# Corpus consisting of 6 documents:
#     
#   Text Types Tokens Sentences id vars
#  text1     3      3         1  1  top
#  text2     3      3         1  2 down
#  text3     5      5         1  3  top
# text11     5      5         1  1  top
# text21     3      3         1  2 down
# text31     8      8         1  3  top
# 
# Source:  Combination of corpuses corpus(x10[, -which(names(x10) == "text2")], text_field = "text1") and corpus(x10[, -which(names(x10) == "text1")], text_field = "text2")
# Created: Tue Feb  6 19:14:14 2018
# Notes: 

您也可以在此阶段使用docnames(x10_corpus2) &lt;- 重新分配文档名,使其更像第一种方法。

【讨论】:

  • 啊!我考虑过建立两个语料库并合并。这些方法有好处吗?假设我必须将其扩展到 20k+ cmets,是否有更可扩展的方法?
  • 我可能会使用方法 1。20k+ 将没有任何问题。
  • 太棒了!方法 1 中的最后一个问题, rownames(x)
  • 我只是在代码中添加了注释来解释原因。这是因为 corpus() 调用将自动使用这些作为文档名。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-16
  • 2019-07-17
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多