Conditional chunking of text file in Python

78 Views Asked by At

Hopefully this is a pretty straight-forward question. I have a transcript that i am trying to split into chunks of each speaker. The code I currently have is;

text = '''
Speaker 1: hello there

this is some text. 

Speaker 2: hello there, 

this is also some text.
'''

a = text.split('\nSpeaker')

This splits the text as i would want it to, however I miss the 'Speaker' identifier from the second utterance. I would need to keep this for identification purposes. Specifically, what i am trying to obtain is a result akin to the following;

['Speaker 1: hello there\n\nI am checking to see if this works. \n', ' Speaker2: 
Hopefully it will, \n\nit seems pretty straightforward.\n']

Any suggestions are welcome

Thanks

2

There are 2 best solutions below

0
On BEST ANSWER

re.split in multiline mode, matching \n (newline), with a zero-width positive lookahead to match Speaker ((?=Speaker)):

re.split(r'\n(?=Speaker)', text, flags=re.MULTILINE)

Example:

In [228]: text = '''Speaker 1: hello there
     ...: 
     ...: this is some text. 
     ...: 
     ...: Speaker 2: hello there, 
     ...: 
     ...: this is also some text.
     ...: '''

In [229]: re.split(r'\n(?=Speaker)', text, flags=re.MULTILINE)
Out[229]: 
['Speaker 1: hello there\n\nthis is some text. \n',
 'Speaker 2: hello there, \n\nthis is also some text.\n']
0
On

non-regex solution:

['Speaker' + substr for substr in text.split('Speaker')[1:]]

output

['Speaker 1: hello there\n\nthis is some text. \n\n',
 'Speaker 2: hello there, \n\nthis is also some text.\n']