return getTokenRelTreeString(jCas, nodes, annotations, labels, false);
}
public static String getTokenRelTreeString(JCas jCas, List<ConllDependencyNode> nodes, Annotation[] annotations, String[] labels, boolean getParent){
Map<ConllDependencyNode, SimpleTree> node2tree = new HashMap<ConllDependencyNode, SimpleTree>();
ConllDependencyNode topNode = null;
// create a SimpleTree object that corresponds to this dependency tree, where the
// root is the head of the sentence and the children are all the words such that the parent
// is their head. In this case every word is represented by its relationship as well as
// its word
for(ConllDependencyNode node : nodes){
if(node.getHead() == null){
topNode = node;
continue;
// do absolutely nothing with this -- it covers the whole sentence and has no useful info
// 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);