def multi_replace(text, word_dic):
 rc = re.compile('|'.join(map(re.escape, word_dic)))
 def translate(match):
  return word_dic[match.group(0)]
 return rc.sub(translate, text)

def multi_replace ( text , word_dic ) : rc = re . compile ( '|' . join ( map ( re . escape , word_dic ) ) ) def translate ( match ) : return word_dic [ match . group ( 0 ) ] return rc . sub ( translate , text )
takes a string and replace words that match a key in a dictionary with the associated value .