基于标点符号的文本分割,尤其是在子句级别

text segmentation based on punctuation marks, especially at clause level

当我们在句子或段落中遇到标点符号时,我想对文本进行分段。如果我在我的正则表达式中使用逗号 (,),它也会对用逗号分隔的单个名词动词或形容词进行分块。 假设我们有 "dogs, cats, rats and other animals"。狗变成了一个单独的块,我不想发生这种情况。 无论如何我可以忽略在 nltk 中使用正则表达式或任何其他方式我只能将逗号分隔的子句作为文本段

代码

from nltk import sent_tokenize
import re
text = "Peter Mattei's 'Love in the Time of Money' is a visually stunning film to watch. Mrs. Mattei offers us a vivid portrait about human relations. This is a movie that seems to be telling us what money, power and success do to people in the different situation we encounter.
text= re.sub("(?<=..Dr|.Mrs|..Mr|..Ms|Prof)[.]","<prd>", text)
txt = re.split(r'\.\s|;|:|\?|\'\s|"\s|!|\s\'|\s\"', text)
print(txt)

这太复杂了,无法用正则表达式解决:正则表达式无法知道候选子句中有谓词(动词),如果扩展它,就会进入另一个子句。

你要解决的问题在NLP中叫做chunking。传统上,这是基于 POS 标记的基于正则表达式的算法(因此,您需要先进行 POS 标记)。 NLTK 有 a tutorial for that,但是,这是一种相当过时的方法。

现在,当快速可靠的标记器和解析器可用时(例如,在 Spacy 中)。我建议先分析句子,然后在选区分析中找到块。