title: Python find when two words are N words apart
body: <p>I have a dictionary of names (where the key is the name and the value is a list of aliases associated to that name), and a body of text. I want to find and count the number of times a pair of names are N words apart in the body of text.</p>
  <p>For example :</p>
  <pre class="lang-py prettyprint-override"><code>found_names = {
      'jack' : ['ripper', 'drought'],
      'jim' : ['carrey', 'gaffigan'],
      'chris' : ['hemsworth', 'evans', 'pratt', 'brown'],
      'james' : ['bond']
  }
  </code></pre>
  <p>text = <code>'Hello Jack, my name is Jim. My friend Evans has seen you with Bond. Evans says they call you ripper'</code></p>
  <p>N = 6</p>
  <p>We would get some sort of result like :</p>
  <pre class="lang-py prettyprint-override"><code>[
      ('Jack', 'Jim', 1),  # [jack] my name is [jim] - 4 words after Jack
      ('Jim', 'Chris', 1), # [jim]. my friend [evans] - 3 words after Jim, evans refers to an alias of Chris hence Chris being in the result
      ('Chris', 'James', 2), # [evans] has seen you with [bond]. -- first occurence, [bond]. [evans] -- second occurence
      ('Chris', 'Jack', 1), # [evans] says they call you [ripper] - 5 words apart and ripper is an alias for Jack
  ]
  </code></pre>
  <p>Note, there is no need to reference the same name twice during the period, so the result doesn't have (Chris, Chris). Also the occurrences of the two names have to be at most 6 words apart, not exactly 6 words apart.</p>
  <p>Is there a way to do this efficiently? I was thinking of finding the position of each name in the names list in the body of text and storing it as a dictionary where the key is the name and the values is a list of indicies corresponding to the position of the name in the body of text.</p>
  <p>I'm not sure what to do after that, can anyone help. The code I have so far :</p>
  <pre class="lang-py prettyprint-override"><code>N = 16
  t = text.split(' ')
  pos_dct = {} # key is going to be the name, value is going to. be the list of positions
  name_lst = [[k] + v for k,v in found_names.items()]
  for i,w in enumerate(t):
      if w in name_lst:
          if w in pos_dct:
              pos_dct[w].append(i)
          else:
              pos_dct[w] = [i]
  </code></pre>