Search code examples
rquanteda

Change the number of pre and post words of a keyword in quanteda KWIC


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.


Solution

  • 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