使用Python中的Regex进行句子分割

时间:2013-11-08 12:30:40

标签: python regex nlp text-segmentation

我正在编写一个脚本,用Python将文本拆分成句子。但是,编写更复杂的正则表达式我非常糟糕。

根据该规则,我希望将句子分开。我想分开句子:

* end with "!"  or
* end with "?"  or
* end with "..."  or
* end with "." and the full stop is not followed by a number  or
* end with "." and the full stop is followed by a whitespace

Python的正则表达式是什么?

1 个答案:

答案 0 :(得分:3)

您可以将五个要点转换为正则表达式:

!|\?|\.{3}|\.\D|\.\s

请注意,我只是创建一个由五个替代项组成的替代项,每个替代项代表您的一个要点:

  • !
  • \?
  • \.{3}
  • \.\D
  • \.\s

由于点(.)和问号(?)是正则表达式模式中的特殊字符,因此需要使用反斜杠(\)对其进行转义被视为文字。管道(|)是两个备选方案之间的分隔符。

使用上述正则表达式,您可以使用re.split将文本拆分为句子。