1

我想把一个字符串分成句子。由于这并不简单(由于许多“。”不是句子的结尾)我正在使用 BreakIterator 如下:

public static List<String> textToSentences(String text) {
    BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.US);
    iterator.setText(text);
    List<String> sentences = new ArrayList<String>(); // empty list
    String oneSentence = "";
    int start = iterator.first();
    int ctr = 0;
    for (int end = iterator.next(); end != BreakIterator.DONE; start = end, end = iterator.next()) {
        oneSentence = text.substring(start,end);
        System.out.println(ctr + ": " + oneSentence);
        sentences.add(oneSentence);
        ctr += 1;
    }
    return sentences;
}

如果我现在对此进行测试:

String text = "This is a test. This is test 2 ... This is test 3?  This is test 4!!! This is test 5!?  This is a T.L.A. test. Now with a Dr. in it. And so associate-professor Dr. Smith said that it was 567 B.C.. Hi there! There is one thing: go home!";

结果是:

0: This is a test. 
1: This is test 2 ... 
2: This is test 3?  
3: This is test 4!!! 
4: This is test 5!?  
5: This is a T.L.A. test. 
6: Now with a Dr. in it. 
7: And so associate-professor Dr. 
8: Smith said that it was 567 B.C.. 
9: Hi there! 
10: There is one thing: go home!

在第 6 句中,它正确地忽略了 Dr.,但在第 7 句中,它在 Dr. 之后中断(7+8 应该是一个句子)。为什么会这样,我该如何解决?

4

0 回答 0