Created
December 7, 2021 02:35
-
-
Save MaddoxRauch/9628d41f408743809238392ebae53f96 to your computer and use it in GitHub Desktop.
Replace multiple words with new words. Parameters are the text to be edited and dictionary of words to be replaced dictionary names are replaced with corresponding values.
This file contains hidden or 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 multiword_replace(text, word_dict): | |
"""Replaces multiple words using dictionary. Dictionary names found are replaced with their corresponding values.""" | |
rc = re.compile('|'.join(map(re.escape, word_dict))) | |
def translate(match): | |
return word_dict[match.group(0)] | |
return rc.sub(translate, text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment