Package org.apache.ctakes.typesystem.type.syntax

Examples of org.apache.ctakes.typesystem.type.syntax.TreebankNode


  public static LinkedList<TreebankNode> bfs(Queue<TreebankNode> q, TreebankNode X, TreebankNode node){
    // now traverse the queue until it is empty, adding NPs
    LinkedList<TreebankNode> list = new LinkedList<TreebankNode>();
    HashSet<TreebankNode> path = new HashSet<TreebankNode>();
    TreebankNode cur = node;
    if(cur != null){
      path.add(cur);
      while(cur != X){
        cur = cur.getParent();
        path.add(cur);
      }
    }
    while(!q.isEmpty()){
      cur = q.remove();
      if(cur.getNodeType().equals("NP") && !path.contains(cur)){
        list.add(cur);
        // I think:
        // if cur is on the path then short circuit.
      }
      for(int i = 0; i < cur.getChildren().size(); i++){
        TreebankNode n = cur.getChildren(i);
        if(n == node) break;
        // BEFORE: did this because terminal node was word.  Now terminal is POS tag, which can go here,
        // shouldn't need special case to avoid adding terminals...
//        if(!(n instanceof TerminalTreebankNode)){
//          q.add(n);
View Full Code Here


//  }

  public static SimpleTree extractPathTree(TreebankNode t1, TreebankNode t2){
    // swap the ordering if necessary
    if(t1.getBegin() > t2.getBegin()){
      TreebankNode temp = t2;
      t2 = t1;
      t1 = temp;
    }

    // edge cases that don't really make sense....
    // 1) Same tree
    // 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());
View Full Code Here

  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;
    }
    if(t1 == t2 || (t1.getBegin() >= t2.getBegin() && t1.getEnd() <= t2.getEnd()) || (t2.getBegin() >= t1.getBegin() && t2.getEnd() <= t1.getEnd())){
      node = sameTree(t1,t2);
    }else{
      TreebankNode lca = getLCA(t1,t2);
      ArrayList<TreebankNode> l1 = getUpwardPath(lca, t1);
      ArrayList<TreebankNode> l2 = getUpwardPath(lca, t2);
      if(lca == null){
        lca = new TopTreebankNode(jcas);
        lca.setNodeType("TOP");
        lca.setChildren(new FSArray(jcas,2));
        if(l1.size()==0){
          l1.add(t1);
        }
        if(l2.size() == 0){
          l2.add(t2);
        }
        lca.setChildren(0, l1.get(0));
        lca.setChildren(1, l2.get(0));
      }
      node = buildSimpleClonePET(lca, t1, t2);
    }
    return node;
  }
View Full Code Here

  private static SimpleTree buildSimpleClonePET(TreebankNode lca, TreebankNode t1, TreebankNode t2){
    SimpleTree t = new SimpleTree(lca.getNodeType());
    if(!(lca instanceof TerminalTreebankNode)){
      for(int i = 0; i < lca.getChildren().size(); i++){
        TreebankNode tn = lca.getChildren(i);
        if(tn.getEnd() > t1.getBegin() && tn.getBegin() < t2.getEnd()){
          t.addChild(buildSimpleClonePET(lca.getChildren(i), t1, t2));
        }
      }
    }
    return t;
View Full Code Here

    return t;
  }
 
  // Find the least common ancestor of two other nodes, or null (top node) if they are in different sentences
  private static TreebankNode getLCA(TreebankNode t1, TreebankNode t2){
    TreebankNode lca = t2;
    while(lca != null && lca.getBegin() > t1.getBegin()){
      lca = lca.getParent();
    }
    return lca;
  }
View Full Code Here

    return SimpleTree.fromString(fullString);   
  }

  public static SimpleTree getSurroundingTree(TreebankNode node){
    SimpleTree tree = null;
    TreebankNode top = node;
    while(node.getParent() != null){
      node = node.getParent();
    }
    tree = getSimpleClone(node);
    return tree;
View Full Code Here

    copy.setChildren(0, getTreeCopy(jcas, orig.getChildren(0)));
    return copy;
  }

  public static TreebankNode getTreeCopy(JCas jcas, TreebankNode orig){
    TreebankNode copy = null;
    if(orig instanceof TerminalTreebankNode){
      copy = new TerminalTreebankNode(jcas);
      copy.setLeaf(true);
      copy.setChildren(null);
    }else{
      copy = new TreebankNode(jcas);
      copy.setChildren(new FSArray(jcas, orig.getChildren().size()));
      for(int i = 0; i < orig.getChildren().size(); i++){
        copy.setChildren(i, getTreeCopy(jcas, orig.getChildren(i)));
        copy.getChildren(i).setParent(copy);
      }
    }
    copy.setNodeType(orig.getNodeType());
    copy.setBegin(orig.getBegin());
    copy.setEnd(orig.getEnd());
    return copy;
  }
View Full Code Here

  public static TreebankNode annotationNode(JCas jcas, Annotation annot){
    return annotationNode(jcas, annot.getBegin(), annot.getEnd());
  }
 
  public static TreebankNode annotationNode(JCas jcas, int a, int b){
    TreebankNode lowestDom = null;
    int overage = Integer.MAX_VALUE;
    FSIterator<Annotation> iter = jcas.getJFSIndexRepository().getAnnotationIndex(TreebankNode.type).iterator();
    while(iter.hasNext()){
      TreebankNode node = (TreebankNode) iter.next();
      if(node.getBegin() == a && node.getEnd() == b){
        // this code will drill down -- actually want to go other way
//        while(node.getChildren() != null && node.getChildren().size() == 1){
//          node = node.getChildren(0);
//        }
       
        // this code will head up as long as parent has the same span
        try{
          while(node.getParent() != null && node.getParent().getChildren().size() == 1 && !node.getParent().getNodeType().equals("TOP")){
            node = node.getParent();
          }
        }catch(NullPointerException e){
          System.err.println("Null pointer exception in AttributeCalculator::markableNode()");
        }
        return node;
      }else if(node.getBegin() <= a && node.getEnd() >= b){
        int tempOver = (a-node.getBegin()) + (node.getEnd()-b);
        if(tempOver < overage){
          lowestDom = node;
          overage = tempOver;
        }
      }
View Full Code Here

  public static TreebankNode insertAnnotationNode(JCas jcas, TopTreebankNode root, Annotation arg1, String nodeType) {
    // tree did not match the arg exactly, so if possible we'll insert a node in the tree here that
    // is under tree but above its children.  So we'll try to find the start and end child that this
    // arg covers if possible.
    TreebankNode tree = root;
    TreebankNode lastTree = null; //tree;
    do{
      lastTree = tree;
      // only continue downward traversal if we are not at a POS node...
      if(tree.getChildren().size() > 1 || tree.getChildren(0).getChildren() != null){
        for(int i = 0; i < tree.getChildren().size(); i++){
          TreebankNode child = tree.getChildren(i);
          if(child.getBegin() <= arg1.getBegin() && child.getEnd() >= arg1.getEnd()){
            tree = child;
            break// break out of inner for-loop
          }
        }
      }
    }while(tree != lastTree);

    TreebankNode newTree = null;
    if(tree.getBegin() == arg1.getBegin() && tree.getEnd() == arg1.getEnd()){
      while(tree.getParent() != null && tree.getParent().getBegin() == arg1.getBegin() && tree.getParent().getEnd() == arg1.getEnd()){
        tree = tree.getParent();
      }
      // matches a node in tree, just insert one above it
      newTree = new TreebankNode(jcas, tree.getBegin(), tree.getEnd());
      newTree.setNodeType(tree.getNodeType());
      newTree.setChildren(tree.getChildren());
      newTree.setParent(tree);
      tree.setNodeType(nodeType);
      tree.setChildren(new FSArray(jcas, 1));
      tree.setChildren(0,newTree);
      newTree = tree;
    }else{
      // mismatch

      int startChild = -1;
      int endChild = -1;
      for(int i = 0; i < tree.getChildren().size(); i++){
        if(startChild == -1){
          if(tree.getChildren(i).getBegin() == arg1.getBegin()){
            startChild = i;
          }
        }else if(tree.getChildren(i).getEnd() == arg1.getEnd()){
          endChild = i;
          break;
        }
      }
      // here is where we insert if possible
      if(startChild >= 0 && endChild >= 0){
        newTree = new TreebankNode(jcas, tree.getChildren(startChild).getBegin(), tree.getChildren(endChild).getEnd());
        newTree.setNodeType(nodeType);
        newTree.setParent(tree);
        int numStolenChildren = endChild-startChild+1;
        newTree.setChildren(new FSArray(jcas, numStolenChildren));
        // add new children to new intermediate node
        for(int i = startChild; i <= endChild; i++){
          newTree.setChildren(i-startChild, tree.getChildren(i));
        }
        // create new children array for top node (tree)
        FSArray children = new FSArray(jcas, tree.getChildren().size() - numStolenChildren + 1);
        for(int i = 0; i < startChild; i++){
          children.set(i, tree.getChildren(i));
        }
        children.set(startChild, newTree);
        for(int i = endChild+1; i < tree.getChildren().size(); i++){
          children.set(i-numStolenChildren+1, tree.getChildren(i));
        }
        tree.setChildren(children);
      }else{
        // just put above here...
        newTree = new TreebankNode(jcas, tree.getBegin(), tree.getEnd());
        newTree.setNodeType(tree.getNodeType());
        newTree.setChildren(tree.getChildren());
        newTree.setParent(tree);
        tree.setNodeType(nodeType);
        tree.setChildren(new FSArray(jcas, 1));
        tree.setChildren(0,newTree);
        newTree = tree;
      }
View Full Code Here

    // if the whole tree is to the left of the annotation then do nothing:
    if(node.getEnd() <= annot.getBegin() || node.getLeaf()) return;

    // if there is some overlap then iterate over trees, ignoring those to the left, recursing on those that overlap, and deleting those to the right
    for(int i = 0; i < node.getChildren().size(); i++){
      TreebankNode child = node.getChildren(i);
      if(child.getEnd() <= annot.getBegin()){
        // child is to the left of annotation completely
        continue;
      }else if(child.getBegin() > annot.getEnd()){
        // child is to the right of annotation completely -- remove it and all to the right
        // TODO
        FSArray newChildren = new FSArray(jcas, i);
        for(int j = 0; j < i; j++){
          newChildren.set(j, node.getChildren(j));
View Full Code Here

TOP

Related Classes of org.apache.ctakes.typesystem.type.syntax.TreebankNode

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.