使用python将句子分成段落

时间:2016-06-29 03:59:00

标签: python nltk paragraphs

我需要使用python将句子分成视频标题的段落。我试图使用nltk.tokenize.texttiling但没有得到任何结果。 这里是文本的摘录:

– [Voiceover] Bob Dylan is,
you must be 20 years old now,
aren't you?
– [Voiceover] Yeah, I must be 20.
(laughing)
– [Voiceover] Are you?
– [Voiceover] Yeah, I'm 20, I'm 20.
(guitar music)
My hands are cold.
It's a pretty cold studio.
– [Voiceover] The coldest studio.
– [Voiceover] Usually can do this.
There I just want to do it once.
(guitar strumming)
– [Voiceover] When I first heard Bob Dylan
was, I think, about three
years ago in Minneapolis.
– [Voiceover] At that time I
was just sort of doing nothing.
I was there working, I guess.
I was making pretend I was
going to school out there.
I'd just come there from South Dakota.
– [Voiceover] You've sung
now at Goody's here in town.
Have you sung at any of the coffee houses?

1 个答案:

答案 0 :(得分:0)

看起来很简单,你可以使用正则表达式来做到这一点, 我不知道你想要哪种格式,但这是一个例子

import re

sentence = """
– [Voiceover] Bob Dylan is,
you must be 20 years old now,
aren't you?
– [Voiceover] Yeah, I must be 20.
(laughing)
– [Voiceover] Are you?
– [Voiceover] Yeah, I'm 20, I'm 20.
(guitar music)
My hands are cold.
It's a pretty cold studio.
– [Voiceover] The coldest studio.
– [Voiceover] Usually can do this.
There I just want to do it once.
(guitar strumming)
– [Voiceover] When I first heard Bob Dylan
was, I think, about three
years ago in Minneapolis.
– [Voiceover] At that time I
was just sort of doing nothing.
I was there working, I guess.
I was making pretend I was
going to school out there.
I'd just come there from South Dakota.
– [Voiceover] You've sung
now at Goody's here in town.
Have you sung at any of the coffee houses?

"""

start_re = re.compile(r'\–\s\[.*?\]')
result = re.split(start_re,sentence)
result = filter(lambda x:x, [s.replace('\n','').strip() for s in result])
print result

输出

["Bob Dylan is,you must be 20 years old now,aren't you?", 'Yeah, I must be 20.(laughing)', 'Are you?', "Yeah, I'm 20, I'm 20.(guitar music)My hands are cold.It's a pretty cold studio.", 'The coldest studio.', 'Usually can do this.There I just want to do it once.(guitar strumming)', 'When I first heard Bob Dylanwas, I think, about threeyears ago in Minneapolis.', "At that time Iwas just sort of doing nothing.I was there working, I guess.I was making pretend I wasgoing to school out there.I'd just come there from South Dakota.", "You've sungnow at Goody's here in town.Have you sung at any of the coffee houses?"]
相关问题