【问题标题】:Split text with quotes into sentences using BreakIterator Java使用 BreakIterator Java 将带引号的文本拆分为句子
【发布时间】:2015-02-17 12:08:23
【问题描述】:

我尝试使用 BreakIterator Java 将包含引号的段落拆分为句子。

这是我的段落,包含我要拆分的引用:

“人们现在变得越来越聪明,越来越挑剔。他们知道哪些是 有资格选择,哪一盘,哪里出金,”他说。 应对即将到来的选举的策略,Edi 说,这是 仍在等待提供。


这是我的代码:

public class SplitParagraph {
public static void main(String[] args){
    String paragraph = "\"People are now getting smarter and more critical. They know which are eligible to choose, which one pan, where the gold,\" he said. About strategies for coping with the upcoming elections, Edi said, it was still awaiting the provision.";
    BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.ENGLISH);
    iterator.setText(paragraph);
    int start = iterator.first();
    int i=1;
    for (int end = iterator.next();end != BreakIterator.DONE; start = end, end = iterator.next()) {
        System.out.println("Sentence "+i+" : "+paragraph.substring(start,end));
        i++;
    }
}}


输出程序:

句子 1:“人们现在变得越来越聪明,越来越挑剔。
句子2:他们知道哪些有资格选择,哪些一盘,黄金在哪里,”他说。 第 3 句:关于应对即将到来的选举的策略,Edi 说,它仍在等待提供。

输出程序不正确,因为该段落仅包含 2 句子。不是三句话。


正确的输出程序必须是这样的:

句子 1 :“人们现在变得越来越聪明,越来越挑剔。他们知道哪些有资格选择,哪些是平底锅,哪里有金子,”他说。
第 2 句:关于应对即将到来的选举的策略,Edi 说,它仍在等待提供。

对我的问题有什么想法吗?

【问题讨论】:

    标签: java


    【解决方案1】:

    只需根据以下正则表达式拆分您的输入,

    "(?<=\\.)\\s+(?=(?:\"[^\"]*\"|[^\"])*$)"
    

    这匹配一个或多个空格,就在双引号中不存在的点之后。

    (?&lt;=\\.) - 正向向后看,只看所有的点。

    \\s+ - 匹配一个或多个空格字符。

    (?=...) - 肯定的前瞻,它断言匹配必须跟随,

    (?:\"[^\"]*\"|[^\"])* - 任何双引号块,如 "foobar" 或任何字符,但不是双引号,零次或多次。

    (?:\"[^\"]*\"|[^\"])*$ 然后它必须到达行尾。这不会匹配"foo. bar" 字符串中的空格,因为在空格之后存在一个单双引号而不是双引号块。

    DEMO

    String s = "\"People are now getting smarter and more critical. They know which are eligible to choose, which one pan, where the gold,\" he said. About strategies for coping with the upcoming elections, Edi said, it was still awaiting the provision.";
    String parts[] = s.split("(?<=\\.)\\s+(?=(?:\"[^\"]*\"|[^\"])*$)");
    for(String i: parts)
    {
    System.out.println(i);
    }
    

    输出:

    "People are now getting smarter and more critical. They know which are eligible to choose, which one pan, where the gold," he said.
    About strategies for coping with the upcoming elections, Edi said, it was still awaiting the provision.
    

    String s = "\"People are now getting smarter and more critical. They know which are eligible to choose, which one pan, where the gold,\" he said. About Mr. Mrs. strategies for coping with the upcoming elections, Edi said, it was still awaiting the provision.";
    String parts[] = s.split("(?<!Mrs?\\.)(?<=\\.)\\s+(?=(?:\"[^\"]*\"|[^\"])*$)");
    for(String i: parts)
    {
    System.out.println(i);
    }
    

    输出:

    "People are now getting smarter and more critical. They know which are eligible to choose, which one pan, where the gold," he said.
    About Mr. Mrs. strategies for coping with the upcoming elections, Edi said, it was still awaiting the provision.
    

    【讨论】:

    • BreakIterator 的目的是避免像“Mr. & Mrs. Smith”那样在分裂时出现许多陷阱。正则表达式可能不是这里的最佳解决方案。顺便说一句,我不是反对者。
    • 但是 op 在他的问题中没有提到这个问题。见regex101.com/r/uN4lT5/3
    • @Pshemo:没问题。我的段落是印度尼西亚语。不是用户 Mr. & Mrs
    • @AvinashRaj:我想问你。我有第二个问题。如何通过 \n 或 \n\n 示例文本拆分句子:我的名字是 Zulkifli。我来自印度尼西亚。我爱Java。拆分后的结果:句子 1:我的名字是 Zulkifli。句子 2:我来自印度尼西亚。第 3 句:我爱 Java。
    • 使用[\\n\\r]+正则表达式根据一个或多个换行符分割字符串
    猜你喜欢
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多