Chunking a text with some specific words or characters like comma using Regex in python

57 Views Asked by At

I am using the following code to chunk a text with conjunction words. However I wish to add "," comma character as well. So whenever a text faces with one of conjunction words or comma it splits the text. How can I do that?

import re
sent = 'food good and service bad'
result = re.split(r"\s+(?:but|and|because|,)\s+", sent)
1

There are 1 best solutions below

0
Andrej Kesely On

Maybe this is what are you looking for?

import re
sent = 'food, good and service bad'
result = re.split(r"(?:\s+(?:but|and|because)\s+)|(?:\s*,\s*)", sent)

print(result)

Prints:

['food', 'good', 'service bad']