// continue;
// }else if(node.getHead().getHead() == null){
// topNode = node;
}
SimpleTree curTree = null;
SimpleTree headTree = null;
if(!node2tree.containsKey(node)){
curTree = SimpleTree.fromString(String.format("(%s %s)", node.getDeprel(), node.getCoveredText()));
node2tree.put(node, curTree);
}else{
curTree = node2tree.get(node);
}
if(curTree.parent == null && node.getHead() != null){
if(node2tree.containsKey(node.getHead())){
headTree = node2tree.get(node.getHead());
}else{
String token = node.getHead().getHead() == null ? "TOP" : node.getHead().getCoveredText();
headTree = SimpleTree.fromString(String.format("(%s %s)", node.getHead().getDeprel(), SimpleTree.escapeCat(token)));
node2tree.put(node.getHead(), headTree);
}
curTree.parent = headTree.children.get(0);
headTree.children.get(0).addChild(curTree);
}
}
ConllDependencyNode highestHead = null;
ConllDependencyNode leftmostHead = null;
ConllDependencyNode rightmostHead = null;
List<SimpleTree> annotationNodes = Lists.newArrayList();
// take the set of input annotations and the corresponding labels and insert them into the SimpleTree
for(int i = 0; i < annotations.length; i++){
// get the node representing the head of this annotation
List<ConllDependencyNode> coveredNodes = JCasUtil.selectCovered(jCas, ConllDependencyNode.class, annotations[i]);
if(coveredNodes == null || coveredNodes.size() == 0) continue;
ConllDependencyNode headNode = DependencyUtility.getNominalHeadNode(coveredNodes);
// is this the highest node of all the annotations we're looking at?
if(highestHead == null || (distanceFromRoot(headNode) < distanceFromRoot(highestHead))){
highestHead = headNode;
}
if(leftmostHead == null || headNode.getBegin() < leftmostHead.getBegin()){
leftmostHead = headNode;
}
if(rightmostHead == null || headNode.getEnd() > rightmostHead.getEnd()){
rightmostHead = headNode;
}
SimpleTree insertionPoint = node2tree.get(headNode);
SimpleTree insertingTree = new SimpleTree(insertionPoint.cat);
insertionPoint.cat = labels[i];
insertingTree.children = insertionPoint.children;
insertingTree.children.get(0).parent = insertingTree;
insertionPoint.children = new ArrayList<SimpleTree>();
insertionPoint.addChild(insertingTree);
insertingTree.parent = insertionPoint;
annotationNodes.add(insertionPoint);
}
if(highestHead == null) return null;
SimpleTree root = node2tree.get(topNode);
SimpleTree leftmostNode = getLeftmostNode(root, Sets.newHashSet(labels));
SimpleTree rightmostNode = getRightmostNode(root, Sets.newHashSet(labels));
SimpleTree pet = getPathEnclosedTree(root, annotationNodes, leftmostNode, rightmostNode);
if(getParent && pet.parent != null) pet = pet.parent;
String treeStr = pet.toString();
treeStr = treeStr.replaceAll("\\(([^\\(]+) \\)", "($1 nil)").toLowerCase();
return treeStr;
}