user9956940
user9956940

Reputation:

Python highlight each word if text contains combination of words

I have a problem with combination of words highlighting.

Current code finds matches in text and wraps words with tag.

import re

text = 'Just for testing, it is fantastic. Lets do it. Cmon'
keywords = ['testing', 'is fantastic']


def highlight(text, keywords):
    replacement = lambda match: "<mark>" + match.group() + "</mark>"
    text = re.sub("|".join(map(re.escape, keywords)), replacement, text, flags=re.I)
    print(text)

highlight(text, keywords)

Output: Just for <mark>testing</mark>, it <mark>is fantastic</mark>. Lets do it. Cmon

Problem: if is fantastic combination of words are found I want to wrap each word in this combination.

Expectation: Just for <mark>testing</mark>, it <mark>is</mark> <mark>fantastic</mark>. Lets do it. Cmon

Thank you.

Upvotes: 0

Views: 840

Answers (2)

Andrej Kesely
Andrej Kesely

Reputation: 195418

Inside your replacement function, you can do other regex:

import re

text = 'Just for testing, it is fantastic. Lets do it. Cmon'
keywords = ['testing', 'is fantastic']


def highlight(text, keywords):
    replacement = lambda match: re.sub(r'([^\s]+)', r'<mark>\1</mark>', match.group())
    text = re.sub("|".join(map(re.escape, keywords)), replacement, text, flags=re.I)
    print(text)

highlight(text, keywords)

Prints:

Just for <mark>testing</mark>, it <mark>is</mark> <mark>fantastic</mark>. Lets do it. Cmon

Upvotes: 1

norok2
norok2

Reputation: 26886

Just replace:

keywords = ['testing', 'is fantastic']

with:

keywords = ['testing', 'is', 'fantastic']

and it will work.

Upvotes: 1

Related Questions