fork(1) download
  1. import re
  2.  
  3. 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?'
  4.  
  5. def fix_punctuation(text):
  6. def sentence_case(text):
  7. # Split into sentences. Therefore, find all text that ends
  8. # with punctuation followed by white space or end of string.
  9. sentences = re.findall(r'(?:\d+\.\d+|\b[A-Z](?:\.[A-Z])*\b\.?|[^.!?])+[.!?](?:\s|\Z)', text)
  10.  
  11. # Capitalize the first letter of each sentence
  12. sentences = [x[0].upper() + x[1:] for x in sentences]
  13.  
  14. # Combine sentences
  15. return ''.join(sentences)
  16.  
  17. #add space after punctuation
  18. text = re.sub(r'(\d+\.\d+|\b[A-Z](?:\.[A-Z])*\b\.?)|([.,;:!?)])\s*', lambda x: x.group(1) or f'{x.group(2)} ', text)
  19. #capitalize sentences
  20. return sentence_case(text)
  21.  
  22. print(fix_punctuation(mystring))
  23.  
Success #stdin #stdout 0.02s 9612KB
stdin
Standard input is empty
stdout
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?