forked from rampatra/Algorithms-and-Data-Structures-in-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreakParagraph.java
47 lines (40 loc) · 1.47 KB
/
BreakParagraph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package com.rampatra.misc;
import java.text.BreakIterator;
import java.util.Locale;
/**
* Created by IntelliJ IDEA.
*
* @author rampatra
* @since 9/22/15
* @time: 10:02 AM
*/
public class BreakParagraph {
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. Mr.Ram is a good boy. 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;
}
}