Natural Language Processing with Java Cookbook
上QQ阅读APP看书,第一时间看更新

There's more...

The SentenceDetectorME class possesses a sentPosDetect method that passes a string and returns an array of Span objects. A Span object holds the beginning and ending positions within a string. In this case, it holds the beginning and ending indexes of a sentence. The following code illustrates how to use it to obtain the indexes of each sentence in the text:

Span spans[] = sentenceDetector.sentPosDetect(text);
for (Span span : spans) {
System.out.println(span);
}

Add this code to your project and you will get the following output:

[0..37)
[38..105)
[106..134)
[135..259)
[260..344)

We can also determine the probability that the sentences found are valid by using the getSentenceProbabilities method, which generates an array of probabilities, as shown next:

double probablities[] = sentenceDetector.getSentenceProbabilities();
for(int i=0; i<sentences.length; i++) {
System.out.printf("Sentence %d: %6.4f\n",i, probablities[i]);
}

When this code is executed, you will get the following output:

Sentence 0: 0.9999
Sentence 1: 0.8117
Sentence 2: 0.9157
Sentence 3: 0.9953
Sentence 4: 0.9706