Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何创建与quanteda的交互?_R_Quanteda - Fatal编程技术网

R 如何创建与quanteda的交互?

R 如何创建与quanteda的交互?,r,quanteda,R,Quanteda,考虑下面的例子 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). featu

考虑下面的例子

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
和每个句子中的其他标记之间创建一个交互。也就是说,创建特征
是狗
成长中的狗
高狗
,并将它们添加到
dfm
(在我们已有的特征之上)

也就是说,例如,如果句子中同时存在
狗和
狗,那么
狗将等于1(否则为零)。因此,
第一句为1,第二句为0

请注意,我如何仅在句子中包含
dog
时创建交互术语,因此这里不需要
dog grass

如何在
quanteda
中有效地实现这一点?

库(“quanteda”)
##软件包版本:2.1.2

toks您想要什么格式的输出?按术语分类的文档,不包含其他功能?谢谢@KenBenoit。我认为dfm会很棒。因此,在我们的示例中,DFM将有以下列:
the
dog
is
growing
high
grass
as
well
dog
is dog
growing dog
high dog
。我认为这些变量可以在
tokens()
级别创建,但我不确定(
tokens\u skipgram()
如何在这里创建许多不相关的交互),因此规则是:如果句子(即quanteda文档)包含
dog
,然后将句子中的所有标记与
dog
交互。我知道,如果
cbind
调用中的两个DFM包含重叠的列名(即单词),则以后可能会出现问题。我们可以通过cbind-ed-dfm避免重复吗?是的,只需在cbind-ed-dfm上调用
dfm\u compress()
,它将合并重复的功能。