【问题标题】:How to split text to sentences when my text has many dots in-between sentences? [closed]当我的文本在句子之间有很多点时,如何将文本拆分为句子? [关闭]
【发布时间】:2020-06-27 09:08:31
【问题描述】:

要求是:

  1. 句子的开头必须是首字母大写的单词( As, The, In) ONLY,句子的结尾应该是一个点。
    例如:

    输入:"This is a new book. I like to read this book"

    预期输出:['This is a new book.' , 'I like to read this book']

  2. 但是,如果句末有引文,则应将其包含在句子中。在这种情况下,点可能在引用之后(或)在引用之前和之后。

例如:

输入:"This is a new book.(Steve and Rasol 2014). I like to read this book (Rashi & Shabana 2015)."

预期输出:['This is a new book.(Steve and Rasol 2014)' , 'I like to read this book (Rashi & Shabana 2015).']

【问题讨论】:

  • 到目前为止你有什么尝试?
  • r'\w。 +(\w. +\d{4} ) ' - 这个模式对我有用。

标签: python text split nlp


【解决方案1】:

你可以使用这个正则表达式:

 [A-Z](.|\n)*?(\(.+?\))?\.(.|\n|$)(?!\() 

编辑

你可以使用这个优化的正则表达式

[A-Z](.|\n)*?\.(.|\n|$)(?!\()
  • [A-Z]句子以大写字母开头
  • (.|\n)*?句子可以有每个字符或换行
  • \. 句子以点结束...
  • (?!\() ...并且在
  • 之后没有括号“(”

使用此站点进行测试:https://regexr.com/

Java 示例

String s = "This is a new book. (Steve and Rasol 2014). I like to read this book (Rashi & Shabana 2015).";
Pattern pattern = Pattern.compile("[A-Z](.|\\n)*?\\.(.|\\n|$)(?!\\()");
Matcher matcher = pattern.matcher(s);
while (matcher.find()) {
   for (int i = 0; i < matcher.groupCount(); i++) {
       System.out.println(matcher.group(i));
   }
}

【讨论】:

    【解决方案2】:

    由于您列出的数据由. 分隔,您可以使用array = data.split('. ') 命令将数据拆分为一个数组。此外,要在每个项目的末尾添加 .,您可以遍历数组并检查 . 是否存在于末尾,如果不存在则追加它。

    【讨论】:

      【解决方案3】:
      sentence = "This is a new book(Steve and Rasol 2014). " \
                 "I like to read this book (Rashi & Shabana 2015)."
      str = sentence.split(".")
      print(str)
      

      输出:

      ['This is a new book(Steve and Rasol 2014)', 'I like to read this book (Rashi & Shabana 2015).']
      

      【讨论】:

      • 您的代码不会执行 OP 在问题中提出的要求。即'This is a new book.(Steve and Rasol 2014)' 会被错误地分成两个字符串。
      猜你喜欢
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-06
      • 2014-02-11
      • 1970-01-01
      相关资源
      最近更新 更多