Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A script to correct sentences
import re
def replace(match):
if match:
phrase = match.group(0)
symbol = match.group(1)
return symbol + ' ' + phrase[-1].upper() if symbol is '.' else phrase[-1]
def parse(string):
string = re.sub(r'([\.\,\?\:\;])\w', replace, string) # add space between any punctuation symbol and letter
string = string.capitalize() # Capitalize first letter of the sentence
string = re.sub(r'([\.])[\s]+\w', replace, string) # Capitalize any letter after a full stop
string = re.sub(r'[\s]+', ' ', string) # Fix more than one whitespace occurrence
return string
if __name__ == '__main__':
print(parse('first, solve the problem.then, write the code.'))
print(parse('this is a test... and another test.'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment