replace only full words
MRAB
python at mrabarnett.plus.com
Sat Sep 28 13:00:29 EDT 2013
On 28/09/2013 17:11, cerr wrote:
> Hi,
>
> I have a list of sentences and a list of words. Every full word that appears within sentence shall be extended by <WORD> i.e. "I drink in the house." Would become "I <drink> in the <house>." (and not "I <d<rink> in the <house>.")I have attempted it like this:
> for sentence in sentences:
> for noun in nouns:
> if " "+noun+" " in sentence or " "+noun+"?" in sentence or " "+noun+"!" in sentence or " "+noun+"." in sentence:
> sentence = sentence.replace(noun, '<' + noun + '>')
>
> print(sentence)
>
> but what if The word is in the beginning of a sentence and I also don't like the approach using defined word terminations. Also, is there a way to make it faster?
>
It sounds like a regex problem to me:
import re
nouns = ["drink", "house"]
pattern = re.compile(r"\b(" + "|".join(nouns) + r")\b")
for sentence in sentences:
sentence = pattern.sub(r"<\g<0>>", sentence)
print(sentence)
More information about the Python-list
mailing list