Skip to content

Instantly share code, notes, and snippets.

@michaelbukachi
Last active June 9, 2017 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbukachi/01d467bf556e2be107a2903a4bcf2361 to your computer and use it in GitHub Desktop.
Save michaelbukachi/01d467bf556e2be107a2903a4bcf2361 to your computer and use it in GitHub Desktop.
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