Python: Extracting Sentences From Line - Regex Needed Based on Criteria

Viewed 230

Somewhat of a python/programming newbie here...

I am trying to come up with a regex that can handle extracting sentences from a line in a text file, and then appending them to a list. The code:

import re

txt_list = []

with open('sample.txt', 'r') as txt:
    patt = r'.*}[.!?]\s?\n?|.*}.+[.!?]\s?\n?'
    read_txt = txt.readlines()

    for line in read_txt:
        if line == "\n":
            txt_list.append("\n")
        else: 
            found = re.findall(patt, line)
            for f in found:
                txt_list.append(f)


for line in txt_list:
    if line == "\n":
        print "newline"
    else:
        print line

Printed Output as per last 5 lines of above code:

{Hello there|Hello|Howdy} Dr. Munchauson you {gentleman|fine fellow}! 
What {will|shall|should} we {eat|have} for lunch? Peas by the {thousand|hundred|1000} said Dr. Munchauson; {that|is} what he said.

newline
I am the {very last|last} sentence for this {instance|example}.

The contents of 'sample.txt':

{Hello there|Hello|Howdy} Dr. Munchauson you {gentleman|fine fellow}! What {will|shall|should} we {eat|have} for lunch? Peas by the {thousand|hundred|1000} said Dr. Munchauson; {that|is} what he said.

I am the {very last|last} sentence for this {instance|example}.

I have been playing around with the regex for a couple of hours now and I cannot seem to crack it. As it stands the regex does not match at the end of for lunch?. Therefore these 2 sentences What {will|shall|should} we {eat|have} for lunch? Peas by the {thousand|hundred|1000} said Dr. Munchauson; {that|is} what he said. are not separated; which is what I want.

A few important details for the regex:

  • Every sentence will always end in a period, exclamation mark or question mark
  • Every sentence will always contain at least 1 pair of curly brackets '{}' with some words in. Also there will be no misleading '.' after the last bracket in every sentence. Therefore Dr. will always be before the last pair of curly brackets in each sentence. This is why I have attempted to base my regex around using '}'. This way I can avoid using the exceptions approach, of creating exceptions for such grammar as Dr., Jr., approx. and so on. For each file I run this code on, I personally make sure there is no 'misleading period' after the last '}' in any sentence.

The output that I want is this:

{Hello there|Hello|Howdy} Dr. Munchauson you {gentleman|fine fellow}! 
What {will|shall|should} we {eat|have} for lunch?
Peas by the {thousand|hundred|1000} said Dr. Munchauson; {that|is} what he said.

newline
I am the {very last|last} sentence for this {instance|example}.
2 Answers
Related