Created
June 29, 2018 18:33
-
-
Save himlohiya/e2f3d4e25584e0c27f0d2132284d6218 to your computer and use it in GitHub Desktop.
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
def expand_contractions(text, contraction_mapping=CONTRACTION_MAP): | |
contractions_pattern = re.compile('({})'.format('|'.join(contraction_mapping.keys())), | |
flags=re.IGNORECASE|re.DOTALL) | |
def expand_match(contraction): | |
match = contraction.group(0) | |
first_char = match[0] | |
expanded_contraction = contraction_mapping.get(match)\ | |
if contraction_mapping.get(match)\ | |
else contraction_mapping.get(match.lower()) | |
expanded_contraction = first_char+expanded_contraction[1:] | |
return expanded_contraction | |
expanded_text = contractions_pattern.sub(expand_match, text) | |
expanded_text = re.sub("'", "", expanded_text) | |
return expanded_text |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment