title: Defining a window size in a regular expression - Python
body: <p>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. &quot;GHG | CO2 | carbon&quot; and &quot;tonnes | tonne | ton | tons&quot; 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?</p>
  <pre class="lang-py prettyprint-override"><code>match = re.compile(r&quot;climate&quot;, flags=re.I | re.X)
  match.findall(text)
  </code></pre>
  <p>We are new to doing textual analysis in Python, so all help is highly appreciated!</p>