private static Collection getArgFeats()

in ctakes-temporal/src/main/java/org/apache/ctakes/temporal/ae/feature/ListFeaturesExtractor.java [57:142]


  private static Collection<? extends Feature> getArgFeats(JCas jCas, IdentifiedAnnotation primeArg,
      IdentifiedAnnotation secondArg, String prefix) {
    List<Feature> feats = Lists.newArrayList();
    
    List<TreebankNode> nodes = JCasUtil.selectCovered(TreebankNode.class, primeArg);
    if(nodes.size() > 0){
      TreebankNode node = nodes.get(0);
      if(node.getBegin() == primeArg.getBegin() && node.getEnd() == primeArg.getEnd()){
        HashSet<String> priorNPs = new HashSet<String>();
        // we have a node with the exact span as the argument
        // now check if it is an element of a list
        // first move NNs up to their constituent
        if(node.getNodeType().startsWith("NN")){
          node = node.getParent();
        }
        TreebankNode parent = node.getParent();
        if(parent == null) return feats;
        int childIndex = -1;
        for(int i = 0; i < parent.getChildren().size(); i++){
          if(parent.getChildren(i) == node){
            childIndex = i;
            break;
          }
          priorNPs.add(getKey(parent.getChildren(i)));
        }
           
        // cnoditions for this arg being an element of a list:
        // 1) is NP
        // 2) Parent is NP
        // 3) left neighbor is , or right neighbor is , or both neigbors are ,
        boolean lcComma=false, rcComma=false, lcAnd=false;
        if(node.getNodeType().equals("NP") && parent.getNodeType().equals("NP")){
          if(childIndex > 0 && parent.getChildren(childIndex-1).getNodeType().equals(",")){
            // left child is ","
            lcComma = true;
          }
          if(childIndex+1 < parent.getChildren().size() && parent.getChildren(childIndex+1).getNodeType().equals(",")){
            rcComma = true;
          }
          if(childIndex+1 == parent.getChildren().size() && childIndex > 0 && parent.getChildren(childIndex-1).getNodeType().equals("CC")){
            lcAnd = true;
          }
        }
        if(lcComma && rcComma){
          feats.add(new Feature(prefix + "_midlist", true));
        }else if(childIndex==0 && rcComma){
          feats.add(new Feature(prefix + "_startlist", true));
        }else if(lcAnd){
          feats.add(new Feature(prefix + "_endlist", true));
        }
        
        if(lcComma || rcComma || lcAnd){
          // somehow in a list
          // check to see if any element of the list is already part of a relation
          for(BinaryTextRelation otherRel : JCasUtil.select(jCas, BinaryTextRelation.class)){
            Annotation a1 = otherRel.getArg1().getArgument();
            Annotation a2 = otherRel.getArg2().getArgument();
            if(a1 instanceof TimeMention || a2 instanceof TimeMention) continue; // covered by another feature
            if(priorNPs.contains(getKey(a1))){
              // one of the left children is already in another relation!
              feats.add(new Feature(prefix + "_leftSiblingInRelation", true));
              
              // check if the other argument in that relation is the secondary arg
              if(secondArg.getBegin() == a2.getBegin() && secondArg.getEnd() == a2.getEnd()){
                // the other proposed arg of this relation is already in a relation with another element of this list!
                feats.add(new Feature(prefix + "_leftSiblingInRelationWithCurArg"));
              }
            }
            
            if(priorNPs.contains(getKey(a2))){
              feats.add(new Feature(prefix + "_leftSiblingInRelation", true));
              
              if(secondArg.getBegin() == a1.getBegin() && secondArg.getEnd() == a1.getEnd()){
                // the other proposed arg of this relation is already in a relation with another element of this list!
                feats.add(new Feature(prefix + "_leftSiblingInRelationWithCurArg"));
              }
            }
          }
        }
      }
      
      
    }
    
    return feats;
  }