title: How to split sentence at sentence boundaries using iterators in python
body: <p>Hi I have to parse a string such that I split it at the punctuation and write each sentence on a separate line. There are some cases where the punctuation is not a sentence boundary and therefore I don't split it there (for debugging purposes I print out a message when these cases occur).</p>
  
  <p>The following is my code (below):</p>
  
  <ul>
  <li>line is my string that I am reading in </li>
  <li>punctuation list is a predefined list (not that important)</li>
  <li>sentence_boundary is the Boolean that I am trying to use to know when to split the sentence</li>
  <li>I use <em>i</em>, <em>prev</em> and <em>c</em> to check the <em>current</em>, <em>next</em> and <em>next,next</em> characters</li>
  </ul>
  
  <p>Since I worked backwards, the code finds all the conditions that are <strong>NOT</strong> sentence boundaries. It checks for multiple cases and uses iterators to check the next characters. Because I am using iterators, I decided to use recursion to pass in a smaller string everytime so I could iteratively search the whole string. The functionality is working. </p>
  
  <p>However, the goal is to split the string at the points where the punctuation <strong>IS</strong> actually a sentence boundary (i.e. when the other cases are not met). Because of my recursive function I've gotten myself into a bit of a problem where I can't keep track of the index of the list that I am at and therefore don't know where to split the sentence. I was thinking of using a helper function somehow but I don't know how to keep track of the index. </p>
  
  <p>Any help would be appreciated in helping me to modify this code. I know my approach is a bit backwards (rather than looking for where to split the sentence I am looking for where not to split it), but I would like to still work off of this code if possible.</p>
  
  <pre><code>def parse(line): #function
  
  sentence_boundary = True
  
  if (len(line) == 3):
      return
  
  t = iter(line)
  i = next(t)
  prev = next(t)
  c = next(t)
  
  # periods followed by a digit with no intervening whitespace are not sentence boundaries
  if i == "." and (prev.isdigit()):
      print("This is a digit")
      sentence_boundary = False
  
  # periods followed by certain kinds of punctuation are probably not sentence boundaries
  for j in punctuation_list:
      if i == "." and (prev == j):
          print("Found a punctuation")
          sentence_boundary = False
  
  
  # periods followed by a whitespace followed by a lower case letter are not sentence boundaries
  if (i == "." and prev == " " and c.islower()):
      print("This is a lower letter")
      sentence_boundary = False
  
  # periods internal to a sequence of letters with no adjacent whitespace are not sentence boundaries
  if i == '.' and prev.islower() and c.islower():
      print("This is a period within a sentence")
      sentence_boundary = False
  
  # periods followed by a whitespace and then an uppercase letter, but preceded by any of a short list of titles are not sentence boundaries
  if c == '.' and prev.islower() and i.isupper():
      print("This is a title")
      sentence_boundary = False
  
  index = line.index(i)
  
  parse(line[index+1:])
  
  
  if __name__ == "__main__":
      parse(line)
  </code></pre>