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

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


        }

        if(printTrees){
          //          Markable anaphor = vec.getAnaphor();
          //          Markable antecedent = vec.getAntecedent();
          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);
View Full Code Here


    copy.getChildren(0).setParent(copy);
    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.setNodeValue(orig.getNodeValue());
    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){
      if(!tree.getLeaf()){
        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(nodeType);
      newTree.setChildren(new FSArray(jcas, 1));
      newTree.setChildren(0, tree);
      newTree.setParent(tree.getParent());
      TreeUtils.replaceChild(tree.getParent(), tree, newTree);
      tree.setParent(newTree);
//      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;
     
      if(!tree.getLeaf()){
        // it can happen that the tree here is a terminal (pos tag:word) and thus has no children, in the case that the gold
        // standard entities are tokenized correctly and the tokenizer is wrong. With automatic tokens and entities this shouldn't happen.
        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(nodeType);
        newTree.setChildren(new FSArray(jcas, 1));
        newTree.setChildren(0, tree);
        newTree.setParent(tree.getParent());
        TreeUtils.replaceChild(tree.getParent(), tree, newTree);
        tree.setParent(newTree);
//        newTree.setNodeType(tree.getNodeType());
//        newTree.setChildren(tree.getChildren());
//        newTree.setParent(tree);
View Full Code Here

    pos = getPOS();
  }

  private String getPOS(){
    try{
      TreebankNode node = MarkableTreeUtils.markableNode(jcas, m.getBegin(), m.getEnd());
//      TerminalTreebankNode wordNode = (TerminalTreebankNode) node.getRoot().getTerminals().get(node.getHeadIndex());
      TerminalTreebankNode wordNode = MarkableTreeUtils.getHead(node);
      return wordNode.getNodeType();
    }catch(Exception e){
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
        FSArray newChildren = new FSArray(jcas, i);
        for(int j = 0; j < i; j++){
          newChildren.set(j, node.getChildren(j));
        }
View Full Code Here

  public static void removeLeftOfAnnotation(JCas jcas, TreebankNode node, Annotation annot) {
    if(node.getEnd() <= annot.getBegin() || node.getLeaf()) return;

    // go through tree and create a list of children that are overalpping or to the right of the concept node:
    for(int i = 0; i < node.getChildren().size(); i++){
      TreebankNode child = node.getChildren(i);
      if(child.getEnd() < annot.getBegin()){
        // ignore for now but this will be removed later
        continue;
      }else if(child.getEnd() > annot.getBegin()){
        // if it has substructure to the left of the concept we have to recurse
        if(child.getBegin() < annot.getBegin()){
          removeLeftOfAnnotation(jcas, child, annot);
        }
       
        if(i > 0){
          // if we're leaving some out we need to rebuild the whole children array
View Full Code Here

    }else if(dominates(node2, node1)){
      return node2;
    }
   
    // they were entered in the wrong order...
    TreebankNode temp;
    if(node1.getBegin() > node2.getBegin()){
      temp = node1;
      node1 = node2;
      node2 = temp;
    }
   
    TreebankNode ancestor = node2;
   
    while(true){
      if(ancestor == null || ancestor.getBegin() <= node1.getBegin()){
        break;
      }
      ancestor = ancestor.getParent();
    }
   
    return ancestor;
  }
View Full Code Here

  private static boolean isPleonastic (TerminalTreebankNode ttn,
      HashSet<String> modalAdj, HashSet<String> cogved, HashSet<String> othervb) {
    if (!ttn.getCoveredText().equalsIgnoreCase("it")) return false;

    if (ttn.getNodeType().equals("PRP")) {
      TreebankNode tn = ttn.getParent();
      while (tn.getNodeType().startsWith("NP"))
        tn = tn.getParent();
      if (tn.getNodeType().equals("S")) {
        TreebankNode par = tn;
        TreebankNode vp = findP(tn, "VP", 0);
        while (vp!=null) vp = findP(par = vp, "VP", 0);
        vp = par;
        par = vp.getParent();

        FSArray c = vp.getChildren();
        TreebankNode firstChild = (TreebankNode) c.get(0);
        if (isBe(firstChild)) {
          TreebankNode adjP = findP(vp, "ADJP", 1);
          if (adjP!=null && modalAdj.contains(adjP.getCoveredText()) &&
              (findP(vp, "SBAR", 1)!=null ||
              findP(vp, "S", 1)!=null ||
              findP(adjP, "SBAR", 1)!=null ||
              findP(adjP, "S", 1)!=null))
            return true;
View Full Code Here

  private static TreebankNode findP (TreebankNode n, String phraseTag, int startingChild) {
    FSArray c = n.getChildren();
    int i = startingChild;
    while (i < c.size()) {
      TreebankNode tn = (TreebankNode) c.get(i++);
      if (tn.getNodeType().equals(phraseTag) ||
          tn.getNodeType().startsWith(phraseTag+"-"))
        return tn;
    }
    return null;
  }
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.