I've a function to split text on punctuation and leave the rest of the string (including whitespace in other items of the list):
import unicodedata
def split_on_punc(text):
chars = list(text)
i = 0
start_new_word = True
output = []
while i < len(chars):
char = chars[i]
if is_punctuation(char):
output.append([char])
start_new_word = True
else:
if start_new_word:
output.append([])
start_new_word = False
output[-1].append(char)
i += 1
##print(output)
return ["".join(x) for x in output]
def is_punctuation(char):
return True if unicodedata.category(char).startswith("P") else False
E.g.
split_on_punctuation("This, this a sentence. with lotsa' puncts!?@ hahaha looo world")
[out]:
['This',
',',
' this a sentence',
'.',
' with lotsa',
"'",
' puncts',
'!',
'?',
'@',
' hahaha looo world']
It looks like a very complicated way to check through each character and then keep track of whether it's a start of a new word or not.
Is there a better (faster/simpler) way to achieve the same output? I'm looking for improvements in speed and simplicity.