Map<Integer,TreebankNode> innerMap = null;
// mark the boundaries of every NP:
FSIterator<Annotation> nodeIter = jcas.getAnnotationIndex(TreebankNode.type).iterator();
HashMap<Integer,Map<Integer,TreebankNode>> npMap = new HashMap<Integer,Map<Integer,TreebankNode>>();
while(nodeIter.hasNext()){
TreebankNode node = (TreebankNode) nodeIter.next();
if(node.getNodeType().equals("NP")){
innerMap = npMap.get(node.getBegin());
if(innerMap == null){
innerMap = new HashMap<Integer,TreebankNode>();
}
innerMap.put(node.getEnd(), node);
npMap.put(node.getBegin(), innerMap);
}
}
// now check if any of the NE markables share the exact same boundaries:
FSIterator<Annotation> neIter = jcas.getAnnotationIndex(Markable.type).iterator();
while(neIter.hasNext()){
Markable nem = (Markable) neIter.next();
innerMap = npMap.get(nem.getBegin());
if(innerMap != null && innerMap.containsKey(nem.getEnd())){
// found one! Check if it has an NP parent and a PP sibling:
TreebankNode node = innerMap.get(nem.getEnd());
TreebankNode parent = node.getParent();
if(parent.getChildren().size() == 2 && parent.getChildren(0) == node && parent.getNodeType().equals("NP") && parent.getChildren(1).getNodeType().equals("PP")){
// Jackpot! Expand the original markable to be the whole NP -> NP PP construction.
nem.setEnd(parent.getEnd());
}
}
}
}