paragrim343
paragrim343

Reputation: 3

Using variable input for str_extract_all in R

I am pretty green when it comes to R and coding in general. I've been working on a CS project recently for a linguistics course through which I'm finding the words that surround various natural landscape words in The Lord of the Rings. For instance, I'm interested in finding the descriptive words used around words like "stream", "mountain", etc.

Anyhow, to extract all of these words from the text, I've been working off of this post. When running this command by itself, it works:

stringr::str_extract_all(text, "([^\\s]+\\s){4}stream(\\s[^\\s]+){6}")

where "stream" is the specific word I'm going after. The numbers before and after specify how many words before and after I want to extract along with it.

However, I'm interested in combining this (and some other things) into a single function, where all you need to plug in the text you want to search, and the word you want to get context for. However, as far as I've tinkered, I can't get anything other than a specific word to work in the above code. Would there be a way to, in the context of writing a function in R, include the above code, but with a variable input, for instance

stringr::str_extract_all(text, "([^\\s]+\\s){4}WORD(\\s[^\\s]+){6}")

where WORD is whatever you specify in the overall function:

function(text,WORD)
I apologize for the generally apparent newb-ness of this post. I am very new to all of this but would greatly appreciate any help you could offer.

Upvotes: 0

Views: 121

Answers (1)

Serkan
Serkan

Reputation: 1945

This is what you are looking for, if I understood you correctly,

my_fun <- function(input_text, word) {
    
    
    stringr::str_extract_all(
        string = input_text,
        pattern = paste("([^\\s]+\\s){4}", word,  "(\\s[^\\s]+){6}", sep = "")
    )
    
    
    
}

May the light of Eärendil ever shine upon you!

Upvotes: 0

Related Questions