I am trying to convert German words to English using custom dictionary. In below code,replace should only happen if the suffix or prefix of the matching word falls in characters
[,\/!?()_1234567890-=+."""' "]
For exampple:
Mein should be converted at first but not in MeinName as the prefix and suffix are not characters mentioned above. If there were single word like _Mein or Mein. it need be converted.
import re
str = "Mein ,Name, ist John, Wo23 bist+ ,_du? , MeinName "
replacements = { 'Mein':'my', 'ist':'is', 'Wo':'where', 'bist':'are', 'du':'you', 'is':'iis'}
re.sub('({})'.format('|'.join(map(re.escape, replacements.keys()))), lambda m: replacements[m.group()], str)
Expected output :
my ,name,is John,where23 are+,_you? ,MeinName