I'm attempting to create a script that replaces keywords found in body with <strong> tags. <strong>keyword</strong>
The problem is words can be capitalized or have punctuation like dog's toy (whereas I'd be aiming for dog, the strong tag would cut off 's)
I'm trying to create an efficient and dynamic replacer that works as described.
So far this is what I have:
def strongwords(text, dict):
rc = re.compile('|'.join(map(re.escape, dict)))
def translate(match):
return dict[match.group(0)]
return rc.sub(translate, text)
Unless I create a HUGE dict with every possibility (as described above) and expend resources on find/replace, I do not see this working as desired.
Is there a pythonic way to do this with a proven regex recipe? Perhaps a package or module has been designed for this?