1

Is there a way to change the pre and post number of words in quanteda KWIC function? The window function is giving me an equal number of words before and after the keyword, but I need one word before and five words after the keyword.

Ali
  • 45
  • 4
  • Please consider reading [How to make a great R reproducible example?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) so that others may help you. – Cristian E. Nuno Apr 20 '18 at 02:07

1 Answers1

2

The easiest way is to do two kwic() calls, one with a window size of 1, and a second with a window size of 5. Then, you use the pre from the size 1 as the pre for the other.

library("quanteda")

# get a kwic with window size of 1
kwpre  <- kwic(data_char_sampletext, "econom*", window = 1)

# store main kwic result in one with window size of 5
kw <- kwic(data_char_sampletext, "econom*", window = 5)

# replace kw pre with the one-word window pre
kw[["pre"]] <- kwpre[["pre"]]

kw
# [text1, 162]    Irish | economy | in pursuit of a policy             
# [text1, 202] domestic | economy | ? As we are tired                  
# [text1, 268] domestic | economy | show the abject failure of         
# [text1, 486]      the | economy | . Otherwise those funds would      
# [text1, 504] domestic | economy | , stimulating demand and sustaining
Ken Benoit
  • 14,454
  • 27
  • 50
  • @Ken-Benoit To follow up on that: once I have my key words in context of let's say window = 5, how would I then go on to analyze the contexts my key words appear in? More specifically, can I define another dictionary based on which only those key-word-in-context occurences are selected that appear together with "a", "b", and "c", but also MUST NOT appear together with "d". Can we use kwic() or dictionary(list()) for this? – Dr. Fabian Habersack Jun 02 '19 at 09:03
  • Well its a new question that needs code and explanation for an answer, but also could use a clearer question and expectation of the result. Suggest you post a new question and I’ll be happy to answer. If you want to try to figure it out yourself, see `?corpus.kwic`. – Ken Benoit Jun 02 '19 at 13:55