Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.ctakes.temporal.ae.feature.treekernel;
 
 import java.util.List;
 
 import  org.cleartk.ml.Feature;
 import  org.cleartk.ml.TreeFeature;
 
 
 	public List<Feature> extract(JCas jcasIdentifiedAnnotation arg1,
 		List<Feature> features = new ArrayList<Feature>();
 		// first get the root and print it out...
 		TopTreebankNode root = AnnotationTreeUtils.getTreeCopy(jcas, AnnotationTreeUtils.getAnnotationTree(jcasarg1));
 		
 		if(root == null){
 			SimpleTree fakeTree = new SimpleTree("(S (NN null))");
 			features.add(new TreeFeature("TK_PET"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 = (EventMentionarg1;
 		  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 = (EventMentionarg2;
 		  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(jcasrootarg1"ARG1-"+a1type);
 		TreebankNode t2 = AnnotationTreeUtils.insertAnnotationNode(jcasrootarg2"ARG2-"+a2type);
 
 //		addOtherTimes(jcas,root, arg1, arg2);
 				
 		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(t1t2jcas);
		}
    tree.setGeneralizeLeaf(true);
		features.add(new TreeFeature("TK_PET"tree.toString()));
		return features;
	}
	public static void addOtherTimes(JCas jcasTopTreebankNode rootIdentifiedAnnotation arg1IdentifiedAnnotation arg2) {
		List<TimeMentiontimexes = JCasUtil.selectCovered(TimeMention.classroot);
		for(TimeMention timex : timexes){
			// don't want the same timex that we are looking at the argument for...
			if(timex.getBegin() == arg1.getBegin() && timex.getEnd() == arg1.getEnd() ||
			   timex.getBegin() == arg2.getBegin() && timex.getEnd() == arg2.getEnd()) continue;
			// but make sure it is in the correct span...
			if(timex.getBegin() > arg1.getBegin() && timex.getEnd() <= arg2.getBegin()){
				AnnotationTreeUtils.insertAnnotationNode(jcasroottimex"TIMEX");
			}
		}
	}
	public static void moveTimexDownToNP(SimpleTree tree) {
		if(tree.cat.contains("-TIMEX-")){
			SimpleTree child = tree.children.get(0);
			// we've found the correct node: only correct clear violations: PP -> IN NP
			if(child.cat.contains("PP") && child.children.size() == 2 && child.children.get(0)..equals("IN") && child.children.get(1)..startsWith("NP")){
				// swap labels
				String fullCat = tree.cat;
				tree.cat = "PP";
				child.cat = fullCat;
				// now point the new PP at the preposition and stop the new TIMEX from pointing at it:
				tree.children.add(0, child.children.get(0));
				child.children.remove(0);
			}
			return;
		}else if(tree.children == null || tree.children.size() == 0){
			return;
		}
		// if we are not there we have to return
		for(SimpleTree child : tree.children){
		}
	}
	public static void simplifyGCG(SimpleTree tree){
	  if(tree.children == null || tree.children.size() == 0) return;
	  
	  int ampInd = tree.cat.indexOf('+');
	  if(ampInd > 0){
	    tree.cat = tree.cat.substring(0, ampInd);
	  }
	  for(SimpleTree child : tree.children){
	    simplifyGCG(child);
	  }
	}
New to GrepCode? Check out our FAQ X