Last active
June 9, 2017 05:27
-
-
Save michaelbukachi/01d467bf556e2be107a2903a4bcf2361 to your computer and use it in GitHub Desktop.
A script to correct sentences
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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