Python中文本文件的条件分块

时间:2018-08-03 15:19:41

标签: python text-processing transcription text-chunking

希望这是一个非常简单的问题。我有一个抄本,试图将每个发言人分成几部分。我当前拥有的代码是;

text = '''
Speaker 1: hello there

this is some text. 

Speaker 2: hello there, 

this is also some text.
'''

a = text.split('\nSpeaker')

这会按照我想要的方式拆分文本,但是我错过了第二个发音中的“扬声器”标识符。我需要保留此信息以用于识别。具体来说,我想要获得的结果类似于以下内容;

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

欢迎提出任何建议

谢谢

2 个答案:

答案 0 :(得分:2)

re.split在多行模式下,匹配\n(换行符),且零宽度正向超前以匹配Speaker(?=Speaker)):

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

示例:

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']

答案 1 :(得分:1)

非正则表达式解决方案:

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

输出

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