Java Reference
In-Depth Information
...
}
Previously, we did not try to identify which sentences the entities were found in. This is
not hard to do but we need to use a simple for statement instead of a for-each statement to
keep track of the sentence indexes. This is shown in the following example, where the
previous example has been modified to use the integer variable index to keep the sen-
tences. Otherwise, the code works the same way as earlier:
for (int index = 0; index < sentences.length; index++) {
String tokens[] = tokenizer.tokenize(sentences[index]);
Span nameSpans[] = nameFinder.find(tokens);
for(Span span : nameSpans) {
list.add("Sentence: " + index
+ " Span: " + span.toString() + " Entity: "
+ tokens[span.getStart()]);
}
}
The entities discovered are then displayed:
for(String element : list) {
System.out.println(element);
}
The output is as follows:
Sentence: 0 Span: [0..1) person Entity: Joe
Sentence: 0 Span: [7..9) person Entity: Fred
Sentence: 2 Span: [0..1) person Entity: Joe
Sentence: 2 Span: [19..20) person Entity: Sally
Sentence: 2 Span: [26..27) person Entity: Fred
Sentence: 1 Span: [4..5) location Entity: Boston
Sentence: 2 Span: [5..6) location Entity: Vermont
Sentence: 2 Span: [16..17) organization Entity: IBM
Search WWH ::




Custom Search