Skip to content

Instantly share code, notes, and snippets.

@yamanahlawat
Created January 25, 2017 12:17
Show Gist options
  • Save yamanahlawat/4443c6e9e65e74829dbb6b47dd81764a to your computer and use it in GitHub Desktop.
Save yamanahlawat/4443c6e9e65e74829dbb6b47dd81764a to your computer and use it in GitHub Desktop.
Using Regular Expressions to expand contractions of a Word
import re
replacement_patterns = [
(r'won\'t', 'will not'),
(r'can\'t', 'cannot'),
(r'i\'m', 'i am'),
(r'ain\'t', 'is not'),
(r'(\w+)\'ll', '\g<1> will'),
(r'(\w+)n\'t', '\g<1> not'),
(r'(\w+)\'ve', '\g<1> have'),
(r'(\w+)\'s', '\g<1> is'),
(r'(\w+)\'re', '\g<1> are'),
(r'(\w+)\'d', '\g<1> would')
]
class RegexpReplacer(object):
def __init__(self, patterns=replacement_patterns):
self.patterns = [(re.compile(regex), repl) for (regex, repl) in patterns]
def replace(self, text):
s = text
for (pattern, repl) in self.patterns:
s = re.sub(pattern, repl, s)
return s
@yamanahlawat
Copy link
Author

Usage:

  • create an instance of the class
    replacer = RegexpReplacer()
    replacer.replace("can't is a contraction")

  • output: "cannot is a contraction"

@KARUN014
Copy link

KARUN014 commented Mar 3, 2019

very good solution for contraction words in regular expressions.
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment