Java如何将段落分成句子?
本示例说明如何使用BreakIterator.getSentenceInstance()来将段落分为组成该段落的句子。要获取BreakIterator实例,我们调用getSentenceInstance()factory方法并传递语言环境信息。
在该count(BreakIteratorbi,Stringsource)方法中,我们迭代中断以提取组成该段落的句子,该段落的值存储在paragraph变量中。
package org.nhooo.example.text;
import java.text.BreakIterator;
import java.util.Locale;
public class BreakSentenceExample {
public static void main(String[] args) {
String paragraph =
"Line boundary analysis determines where a text " +
"string can be broken when line-wrapping. The " +
"mechanism correctly handles punctuation and " +
"hyphenated words. Actual line breaking needs to " +
"also consider the available line width and is " +
"handled by higher-level software. ";
BreakIterator iterator = BreakIterator.getSentenceInstance(Locale.US);
int sentences = count(iterator, paragraph);
System.out.println("Number of sentences: " + sentences);
}
private static int count(BreakIterator bi, String source) {
int counter = 0;
bi.setText(source);
int lastIndex = bi.first();
while (lastIndex != BreakIterator.DONE) {
int firstIndex = lastIndex;
lastIndex = bi.next();
if (lastIndex != BreakIterator.DONE) {
String sentence = source.substring(firstIndex, lastIndex);
System.out.println("sentence = " + sentence);
counter++;
}
}
return counter;
}
}我们的程序将在控制台屏幕上打印以下结果:
sentence = Line boundary analysis determines where a text string can be broken when line-wrapping. sentence = The mechanism correctly handles punctuation and hyphenated words. sentence = Actual line breaking needs to also consider the available line width and is handled by higher-level software. Number of sentences: 3
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短