java.util.Collections.sort(sentList,
new gate.util.OffsetComparator());
// for each sentence get token annotations
for (Iterator iterator = sentList.iterator(); iterator.hasNext();) {
Annotation annotation = (Annotation) iterator.next();
AnnotationSet sentenceTokens = annotations.get("Token",
annotation.getStartNode().getOffset(), annotation
.getEndNode().getOffset());
// create a list
List<Annotation> annList = new LinkedList<Annotation>();
for (Iterator<Annotation> iterator2 = sentenceTokens.iterator(); iterator2
.hasNext();) {
annList.add(iterator2.next());
}
// order on offset
Collections.sort(annList, new gate.util.OffsetComparator());
// make the array be string[] sentence
String[] tokens = new String[sentenceTokens.size()];
String[] postags = new String[sentenceTokens.size()];
int i = 0;
for (Iterator iterator3 = annList.iterator(); iterator3
.hasNext();) {
Annotation token = (Annotation) iterator3.next();
tokens[i] = token.getFeatures().get("string").toString();
Object posObj = token.getFeatures().get("category");
if (posObj == null) {
throw new ExecutionException(
"Token found with no POS tag category!\n"
+ "Did you run a POS tagger before the OpenNLPChunker?");
}
postags[i] = posObj.toString();
i++;
}
// run pos chunker
String[] chunks = chunker.chunk(tokens, postags);
// add tohose chunk tags to token annotations
int j = 0;
for (Iterator iterator4 = annList.iterator(); iterator4
.hasNext();) {
Annotation token = (Annotation) iterator4.next();
FeatureMap fm = token.getFeatures();
fm.put("chunk", chunks[j]);
token.setFeatures(fm);
j++;
}
}