Package org.apache.ctakes.utils.tree

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


          TreebankNode antecedentNode = MarkableTreeUtils.markableNode(jcas, antecedent.getBegin(), antecedent.getEnd());
          TreebankNode anaphorNode = MarkableTreeUtils.markableNode(jcas, anaphor.getBegin(), anaphor.getEnd());
          debug.println(TreeUtils.tree2str(antecedentNode));
          debug.println(TreeUtils.tree2str(anaphorNode));
//          TopTreebankNode pathTree = TreeExtractor.extractPathTree(antecedentNode, anaphorNode, jcas);
          SimpleTree pathTree = TreeExtractor.extractPathTree(antecedentNode, anaphorNode);
          SimpleTree petTree = TreeExtractor.extractPathEnclosedTree(antecedentNode, anaphorNode, jcas);
//          TopTreebankNode tree = mctTree;
//          String treeStr = TreeUtils.tree2str(tree);
//          String treeStr = mctTree.toString();
          String treeStr = pathTree.toString();
          PrintWriter writer = null;
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

    List<Feature> features = new ArrayList<Feature>();
    // first get the root and print it out...
    TopTreebankNode root = AnnotationTreeUtils.getTreeCopy(view, AnnotationTreeUtils.getAnnotationTree(view, focusAnnotation));

    if(root == null){
      SimpleTree fakeTree = new SimpleTree("(S (NN null))");
      features.add(new TreeFeature(FEAT_NAME, fakeTree.toString()));
      return features;
    }


    String etype="";
    String eventModality="";

    if(focusAnnotation instanceof EventMention){
      eventModality = ((EventMention)focusAnnotation).getEvent().getProperties().getContextualModality();
      etype = "EVENT-"+eventModality;
      AnnotationTreeUtils.insertAnnotationNode(view, root, focusAnnotation, etype);
    }
 
    SimpleTree tree = null;
    tree = TreeExtractor.getSimpleClone(root);

    TemporalPETExtractor.moveTimexDownToNP(tree);

    features.add(new TreeFeature(FEAT_NAME, tree.toString()));
    return features;
  }
View Full Code Here

      IdentifiedAnnotation arg2) throws AnalysisEngineProcessException {
    List<Feature> features = new ArrayList<Feature>();
    // first get the root and print it out...
    TopTreebankNode root = AnnotationTreeUtils.getTreeCopy(jcas, AnnotationTreeUtils.getAnnotationTree(jcas, arg1));
    if(root == null){
      SimpleTree fakeTree = new SimpleTree("(S (NN null))");
      features.add(new TreeFeature("TK_PATH", fakeTree.toString()));
      return features;
    }
    // swap the order if necessary:
    if(arg2.getBegin() <= arg1.getBegin() && arg2.getEnd() <= arg1.getEnd()){
      IdentifiedAnnotation temp = arg1;
      arg1 = arg2;
      arg2 = temp;
    }
   
    String a1type="", a2type="";
    String eventModality="";
    String timeClass="";

    if(arg1 instanceof EventMention){
      EventMention mention = (EventMention) arg1;
      if(mention.getEvent() != null && mention.getEvent().getProperties() != null){
        eventModality = mention.getEvent().getProperties().getContextualModality();
      }
      a1type = "EVENT-"+eventModality;
    }else if(arg1 instanceof TimeMention){
      timeClass = ((TimeMention)arg1).getTimeClass();
      a1type = "TIMEX-"+timeClass;
    }
   
    if(arg2 instanceof EventMention){
      EventMention mention = (EventMention) arg2;
      if(mention.getEvent() != null && mention.getEvent().getProperties() != null){
        eventModality = mention.getEvent().getProperties().getContextualModality();
      }
      a2type = "EVENT"+eventModality;     
    }else if(arg2 instanceof TimeMention){
      timeClass = ((TimeMention)arg2).getTimeClass();
      a2type = "TIMEX-"+timeClass;     
    }
   
    TreebankNode t1 = AnnotationTreeUtils.insertAnnotationNode(jcas, root, arg1, "ARG1-"+a1type);
    TreebankNode t2 = AnnotationTreeUtils.insertAnnotationNode(jcas, root, arg2, "ARG2-"+a2type);

    SimpleTree tree = null;
    tree = TreeExtractor.extractPathTree(t1, t2);
    TemporalPETExtractor.simplifyGCG(tree);
    features.add(new TreeFeature("TK_PATH", tree.toString()));
    return features;
  }
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.