I work with a very messy text when sentences are not capitalised and punctuations are separated properly. I need to add spaces when missing after punctuations [.,:;)!?] but not in decimal numbers or abbreviations.
This is an example:
mystring = 'this is my first sentence with (brackets)in it. this is the second?What about this sentence with D.D.T. in it?or this with 4.5?'
This is where I've got so far.
def fix_punctuation(text):
def sentence_case(text):
# Split into sentences. Therefore, find all text that ends
# with punctuation followed by white space or end of string.
sentences = re.findall('[^.!?]+[.!?](?:\s|\Z)', text)
# Capitalize the first letter of each sentence
sentences = [x[0].upper() + x[1:] for x in sentences]
# Combine sentences
return ''.join(sentences)
#add space after punctuation
text = re.sub('([.,;:!?)])', r'\1 ', text)
#capitalize sentences
text = sentence_case(text)
return text
Which gives me this output:
'This is my first sentence with (brackets) in it. this is the second? What about this sentence with D. D. T. in it? Or this with 4. 5? '
I tried methods suggested here and here but they didn't work with my case. Regex makes my brain hurt so I will really appreciate your help.