I have extracted keywords from a dataframe of sentences. I need to get a few words pre- and post- keyword to understand the context and be able to do some basic counts.
I have tried multiple stringr and stringi functions and grepl functions others suggested on SO for similar questions. However, not finding anything that works for my situation.
Below is what I'd like. Assume it is a dataframe or tibble with the first two fields listed. I need/want to create the rightmost column (keyword_w_context).
In the example, I'm pulling the three words that proceed the keyword. But, I would want to modify whatever solution so I can get 1, 2, n. Would also be nice if I could do post word in the same way.
Basically, wanting to do something like a mutate that creates a new variable with the context words (before/after, see below) around the keyword.
Sentence | Keyword | Keyword_w_context |
---|---|---|
The yellow lab dog is so cute. | dog | The yellow lab dog |
The fluffy black cat purrs loudly. | cat | The fluffy black cat |
Many thanks!