问题描述
考虑下面的例子
library(quanteda)
library(tidyverse)
tibble(text = c('the dog is growing tall','the grass is growing as well')) %>%
corpus() %>% dfm()
Document-feature matrix of: 2 documents,8 features (31.2% sparse).
features
docs the dog is growing tall grass as well
text1 1 1 1 1 1 0 0 0
text2 1 0 1 1 0 1 1 1
我想在每个句子中创建 dog
和其他标记之间的交互。也就是说,创建特征 the-dog
、is-dog
、growing-dog
、tall-dog
并将它们添加到 dfm
(在我们已有的之上)。
也就是说,例如,如果句子中同时存在 the-dog
和 the
,则 dog
将等于 1(否则为零)。因此,the-dog
将是第一个句子的 1,第二个句子的 0。
请注意,我仅在句子中包含 dog
时创建交互项,因此此处不需要 dog-grass
。
如何在 quanteda
中有效地做到这一点?
解决方法
library("quanteda")
## Package version: 2.1.2
toks <- tokens(c(
"the dog is growing tall","the grass is growing as well"
))
# now keep just tokens co-occurring with "dog"
toks_dog <- tokens_select(toks,"dog",window = 1e5)
# create the dfm and label other terms as interactions with dog
dfmat_dog <- dfm(toks_dog) %>%
dfm_remove("dog")
colnames(dfmat_dog) <- paste(featnames(dfmat_dog),sep = "-")
dfmat_dog
## Document-feature matrix of: 2 documents,4 features (50.00% sparse) and 0 docvars.
## features
## docs the-dog is-dog growing-dog tall-dog
## text1 1 1 1 1
## text2 0 0 0 0
# combine with other features
print(cbind(dfm(toks),dfmat_dog),max_nfeat = -1)
## Document-feature matrix of: 2 documents,12 features (37.50% sparse) and 0 docvars.
## features
## docs the dog is growing tall grass as well the-dog is-dog growing-dog
## text1 1 1 1 1 1 0 0 0 1 1 1
## text2 1 0 1 1 0 1 1 1 0 0 0
## features
## docs tall-dog
## text1 1
## text2 0
由 reprex package (v1.0.0) 于 2021 年 3 月 18 日创建