Package org.apache.ctakes.utils.tree

Examples of org.apache.ctakes.utils.tree.SimpleTree


            }
          }
         
          // first get the root and print it out...
          TopTreebankNode root = AnnotationTreeUtils.getTreeCopy(jcas, AnnotationTreeUtils.getAnnotationTree(jcas, arg1));
          SimpleTree tempClone = TreeExtractor.getSimpleClone(root);
          trees.println(tempClone.toString());
//          TopTreebankNode t2 = AnnotationTreeUtils.getTreeCopy(AnnotationTreeUtils.getAnnotationTree(jcas, arg2));
         
          if(root == null){
            System.err.println("Root is null!");
          }
          TreebankNode t1 = AnnotationTreeUtils.insertAnnotationNode(jcas, root, arg1, "ARG1");
          TreebankNode t2 = AnnotationTreeUtils.insertAnnotationNode(jcas, root, arg2, "ARG2");
          String relationString = category + "(\"" + arg1.getCoveredText() + "\", \"" + arg2.getCoveredText() + "\")";
          rels.print("\t");
          rels.print(lineNum);
          rels.print("\t");
          rels.println(relationString);
          rels.flush();
          lineNum++;
//          root.setNodeType(category);
//          out.println(TreeExtractor.getSimpleClone(root));
//          out.flush();
         
         
          SimpleTree tree = null;
          if(t1.getBegin() <= t2.getBegin() && t1.getEnd() >= t2.getEnd()){
            // t1 encloses t2
            tree = TreeExtractor.getSimpleClone(t1);
          }else if(t2.getBegin() <= t1.getBegin() && t2.getEnd() >= t1.getEnd()){
            // t2 encloses t1
            tree = TreeExtractor.getSimpleClone(t2);
          }else{
            tree = TreeExtractor.extractPathEnclosedTree(t1, t2, jcas);
          }
          if(category.equals("location_of-1")){
            out.print("+1 |BT| ");
          }else{
            out.print("-1 |BT| ");
          }
          TreeExtractor.lowercaseWords(tree);
          out.print(tree.toString());
          out.println(" |ET|");
//          root.setNodeType("TOP");
          out.flush();
        }
      }
View Full Code Here


        }
      } catch (Exception e) { e.printStackTrace(); }
    }
   
    if(frags != null && frags.size() > 0){
      SimpleTree tn = TreeExtractor.extractPathTree(MarkableTreeUtils.markableNode(aJCas, antecedent.getBegin(), antecedent.getEnd()),
          MarkableTreeUtils.markableNode(aJCas, anaphor.getBegin(), anaphor.getEnd()));
//      SimpleTree tn = TreeExtractor.extractPathEnclosedTree(MarkableTreeUtils.markableNode(aJCas, antecedent.getBegin(), antecedent.getEnd()),
//          MarkableTreeUtils.markableNode(aJCas, anaphor.getBegin(), anaphor.getEnd()),
//          aJCas);
      // now go over the tree fragment features:
View Full Code Here

//        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;
  }
View Full Code Here

   
    return treeStr;
  }
 
  private static SimpleTree getRightmostNode(SimpleTree root, Set<String> labels) {
    SimpleTree node = null;
   
    for(int i = root.children.size()-1; i >= 0; i--){
      if(labels.contains(root.children.get(i).cat)){
        node = root.children.get(i);
        break;
View Full Code Here

   
    return node;
  }

  private static SimpleTree getLeftmostNode(SimpleTree root, Set<String> labels) {
    SimpleTree node = null;
   
    for(int i = 0; i < root.children.size(); i++){
      if(labels.contains(root.children.get(i).cat)){
        node = root.children.get(i);
        break;
View Full Code Here

      }
      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;

  }
View Full Code Here

    return treeStr;

  }
 
  public static SimpleTree getPathEnclosedTree(SimpleTree root, List<SimpleTree> annotationNodes, SimpleTree leftmostTree, SimpleTree rightmostTree){
    SimpleTree pet = null;
    // for the general case (>= 1 annotations) we need to first find the common set of ancestors
    Set<SimpleTree> commonAncestors = getAncestors(annotationNodes.get(0));
    for(int i = 1; i < annotationNodes.size(); i++){
      Set<SimpleTree> nodeAncestors = getAncestors(annotationNodes.get(i));
      commonAncestors = Sets.intersection(commonAncestors, nodeAncestors);
    }
    // of the common set, which is the lowest?
    SimpleTree lowestAncestor = null;
    for(SimpleTree ancestor : commonAncestors){
      if(lowestAncestor == null || distanceFromRoot(ancestor) > distanceFromRoot(lowestAncestor)){
        lowestAncestor = ancestor;
      }
    }
    // of the children of the lowest ancestors, which do not contain any of the annotations we are
    // interested in?
    root = lowestAncestor;
    SimpleTree curNode = leftmostTree;
    SimpleTree lastNode = null;
    while(curNode != root){
      lastNode = curNode;
      if(curNode == null || curNode.parent == null){
        logger.error("Something weird.");
      }
View Full Code Here

//        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);
View Full Code Here

    // 2) overlapping trees
    if(t1 == t2 || (t1.getBegin() >= t2.getBegin() && t1.getEnd() <= t2.getEnd()) || (t2.getBegin() >= t1.getBegin() && t2.getEnd() <= t1.getEnd())){
      return sameTree(t1, t2);
    }
   
    SimpleTree node = null;
    TreebankNode lca = getLCA(t1, t2);
    if(lca == null) node = new SimpleTree("TOP");
    else node = new SimpleTree(lca.getNodeType());
   
    ArrayList<TreebankNode> antePath = getUpwardPath(lca, t1);
    SimpleTree parent = node;
    for(TreebankNode child : antePath){
      SimpleTree newChild = new SimpleTree(child.getNodeType());
      parent.addChild(newChild);
      parent = newChild;   
    }
    parent.addChild(new SimpleTree("arg1"));
   
    ArrayList<TreebankNode> anaPath = getUpwardPath(lca, t2);
    parent = node;
    for(TreebankNode child : anaPath){
      SimpleTree newChild = new SimpleTree(child.getNodeType());
      parent.addChild(newChild);
      parent = newChild;
    }
    parent.addChild(new SimpleTree("arg2"));
   
    return node;
  }
View Full Code Here

   
    return node;
  }

  public static SimpleTree extractPathEnclosedTree(TreebankNode t1, TreebankNode t2, JCas jcas){
    SimpleTree node = null;
    // swap them if wrong order:
    if(t1.getBegin() > t2.getBegin()){
      TreebankNode temp = t1;
      t1 = t2;
      t2 = temp;
View Full Code Here

TOP

Related Classes of org.apache.ctakes.utils.tree.SimpleTree

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.