We are trying to locate two words within a limited window size in a text file, e.g., 5-10 words, using regular expression. We have imported the text using PyPDF2 and then tokenized. We are able to locate single words in the text (see code below), however, we want to see if e.g. "GHG | CO2 | carbon" and "tonnes | tonne | ton | tons" appear together in the text within a window size of 5 or 10 words. Is there a function in nltk we have to import? And do you guys have a suggestion for how we can reformulate the code below to check if the words above appear within a window?
match = re.compile(r"climate", flags=re.I | re.X)
match.findall(text)
We are new to doing textual analysis in Python, so all help is highly appreciated!