0

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?

1Up
  • 994
  • 2
  • 12
  • 24

1 Answers1

1

The first problem of case can be resolved with the re.IGNORECASE flag. The second is a bit more complicated but an optional match group could work.

rc = re.compile("(%s)('s|re)?" % ('|'.join(map(re.escape, dict)), re.IGNORECASE)

That pattern would ignore case and allow any of the keywords to be suffixed with 's or 're (add more as necessary). The optional match group would need to be joined back by with the keyword match but that should be easy enough