Friends No of sentences can be counted based on no.of (.) full stop...
But if there is a word like M.B.B.S , Mr.Ram..etc.., How to count the no
..of sentences..Plz help me to solve this in java...
Eagerly waiting for ur reply..
Thanking you
Your Friend guru
You could use BreakIterator. The program below counts 5 sentence in your
post (though I replaced "sentences..Plz" by "sentences? Plz").
import java.text.BreakIterator;
import java.util.Locale;
public class SentenceCount {
public static void main(String[] args) {
String text = "Friends No of sentences can be counted "
+ "based on no.of (.) full stop... But if there is a "
+ "word like M.B.B.S , Mr.Ram..etc.., How to count "
+ "the no. of sentences? Plz help me to solve this "
+ "in java... Eagerly waiting for ur reply. Thanking "
+ "you Your Friend guru";
BreakIterator boundary = BreakIterator.getSentenceInstance(
Locale.getDefault());
printAll(boundary, text);
System.out.println("No. of sentences: "
+ count(boundary, text));
}
public static int count(BreakIterator boundary, String source) {
boundary.setText(source);
int count = 0;
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE;
start = end, end = boundary.next(), count++) {
}
return count;
}
public static void printAll(BreakIterator boundary, String source) {
boundary.setText(source);
int count = 0;
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE;
start = end, end = boundary.next(), count++) {
System.out.print(count + 1);
System.out.print('\t');
System.out.println(source.substring(start, end));
}
}
}
--
Regards,
Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \