Consider the following example
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
I would like to create an interaction between dog and the other tokens in each sentence. That is, creating the features the-dog, is-dog, growing-dog, tall-dog and adding them to the dfm (on top of the ones we already have).
That is, for instance, the-dog would be equal to 1 if both the and dog are present in the sentence (and zero otherwise). So the-dog would be one for the first sentence and zero for the second one.
Notice how I only create interaction terms when dog is in the sentence, so dog-grass is not required here.
How can I do that efficiently in quanteda?