如何从具有多个文本列的数据框中创建一个quanteda语料库?

3

假设我有以下内容:

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只能接收一个列。除了构建两个语料库之外,我是否有更好的处理方法?我可以先构建两个语料库,然后按ID合并吗?这可行吗?


我们已经为此打开了一个问题(https://github.com/quanteda/quanteda/issues/1214),但哪种行为更自然:连接文本还是重复ID变量以堆叠文本列(如下面的答案)? - Ken Benoit
是的,我明白你的意思。我认为更直接的方法是重复ID变量,因为连接可能会有问题。例如,我们有3个开放式问题(积极经验、消极经验、其他问题)的员工调查,将它们合并起来确实很奇怪。 - Ted Mosby
1个回答

3

首先,让我们重新创建您的数据框,不要将字符值转换为因子:

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:    

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

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

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) <- 来重新分配文件名,使其更像第一种方法。

啊!我在考虑建立两个语料库并将它们合并。这种方法有什么好处吗?假设我必须扩展到20k+的评论,是否有更具可扩展性的方法? - Ted Mosby
1
我可能会使用方法1。20k+没有任何问题。 - Ken Benoit
太好了!最后一个问题,在方法1中,rownames(x) <- 的目的只是为了标记行,对吗?我没有错过什么重要的东西,对吗? - Ted Mosby
1
我刚刚在代码中添加了一条注释来解释原因。那是因为'corpus()'调用将自动使用这些docnames。 - Ken Benoit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接