Skip to content

Instantly share code, notes, and snippets.

@himlohiya
Created June 29, 2018 18:33
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 himlohiya/e2f3d4e25584e0c27f0d2132284d6218 to your computer and use it in GitHub Desktop.
Save himlohiya/e2f3d4e25584e0c27f0d2132284d6218 to your computer and use it in GitHub Desktop.
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