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;
 
 import java.io.File;
 //import java.io.IOException;
 import java.util.List;
 import java.util.Map;
 //import java.util.Map;
 
 //import org.apache.ctakes.temporal.ae.feature.CoveredTextToValuesExtractor;
 //import org.apache.ctakes.temporal.ae.feature.SectionHeaderExtractor;
 //import org.apache.ctakes.temporal.ae.feature.duration.DurationExpectationFeatureExtractor;
 import  org.cleartk.ml.CleartkAnnotator;
 import  org.cleartk.ml.DataWriter;
 import  org.cleartk.ml.Feature;
 import  org.cleartk.ml.Instance;
 import  org.cleartk.ml.feature.extractor.CleartkExtractor;
 import  org.cleartk.ml.feature.extractor.CleartkExtractor.Covered;
 import  org.cleartk.ml.feature.extractor.CleartkExtractor.Following;
 import  org.cleartk.ml.feature.extractor.CleartkExtractor.Preceding;
 import  org.cleartk.ml.feature.extractor.CleartkExtractorException;
 import  org.cleartk.ml.feature.extractor.CombinedExtractor1;
 import  org.cleartk.ml.feature.extractor.CoveredTextExtractor;
 import  org.cleartk.ml.feature.extractor.TypePathExtractor;
 import  org.cleartk.ml.jar.DefaultDataWriterFactory;
 import  org.cleartk.ml.jar.DirectoryDataWriterFactory;
 import  org.cleartk.ml.jar.GenericJarClassifierFactory;
 
 
 //import com.google.common.base.Charsets;
 
 public class EventDischargeTimeAnnotator extends CleartkAnnotator<String> {
 
 			Class<? extends DataWriter<String>> dataWriterClass,
 					File outputDirectorythrows ResourceInitializationException {
 		return AnalysisEngineFactory.createEngineDescription(
 				CleartkAnnotator.PARAM_IS_TRAINING,
 				true,
 				DefaultDataWriterFactory.PARAM_DATA_WRITER_CLASS_NAME,
 				dataWriterClass,
 				DirectoryDataWriterFactory.PARAM_OUTPUT_DIRECTORY,
 				outputDirectory);
 	}
 
 		return AnalysisEngineFactory.createEngineDescription(
 				CleartkAnnotator.PARAM_IS_TRAINING,
 				false,
 				GenericJarClassifierFactory.PARAM_CLASSIFIER_JAR_PATH,
 				modelPath);
 	}	

Deprecated:
use String path instead of File. ClearTK will automatically Resolve the String to an InputStream. This will allow resources to be read within from a jar as well as File.
		return AnalysisEngineFactory.createEngineDescription(
				CleartkAnnotator.PARAM_IS_TRAINING,
				false,
				GenericJarClassifierFactory.PARAM_CLASSIFIER_JAR_PATH,
				new File(modelDirectory"model.jar"));
	}
	private CleartkExtractor<EventMentionBaseTokencontextExtractor;
	//	private SectionHeaderExtractor sectionIDExtractor;
	//  private CoveredTextToValuesExtractor disSemExtractor;
	//  private DurationExpectationFeatureExtractor durationExtractor;
	public void initialize(UimaContext contextthrows ResourceInitializationException {
		super.initialize(context);
		CombinedExtractor1<BaseTokenbaseExtractor = new CombinedExtractor1<>(
				new CoveredTextExtractor<BaseToken>(),
				new TypePathExtractor<>(BaseToken.class"partOfSpeech"));
		this. = new CleartkExtractor<>(
				BaseToken.class,
				baseExtractor,
				new Preceding(3),
				new Covered(),
				new Following(3));
		//		this.sectionIDExtractor = new SectionHeaderExtractor();
	}
	public void process(JCas jCasthrows AnalysisEngineProcessException {
		//get discharge Time id: T1:
		TimeMention dischargeTime = null;
		//TODO
		//may need better way to identify Discharge Time other than relative span information:
		for(TimeMention time: JCasUtil.selectCovered(jCasTimeMention.class, 40, 60)){
			if(time.getTimeClass().equals("DATE")){
				dischargeTime = time;
				break;
			}
		}
		//2. identify the Hospital Course section:
		List<Segmentcourses = Lists.newArrayList();
		Collection<Segmentsegments = JCasUtil.select(jCasSegment.class);
		for(Segment segsegments){
			if (seg.getId().equals("course")){//find the right segment
				if(JCasUtil.selectCovered(jCas,Sentence.class,seg).size()>0){//ignore empty section
					courses.add(seg);
				}
			}
		}
		//get event-time1 relations:
		Map<List<Annotation>, TemporalTextRelationdischargeTimeRelationLookup;
		dischargeTimeRelationLookup = new HashMap<>();
		if (this.isTraining()) {
			dischargeTimeRelationLookup = new HashMap<>();
			for (TemporalTextRelation relation : JCasUtil.select(jCasTemporalTextRelation.class)) {
				Annotation arg1 = relation.getArg1().getArgument();
				Annotation arg2 = relation.getArg2().getArgument();
				// The key is a list of args so we can do bi-directional lookup
				if(arg1 instanceof TimeMention && arg2!=null && arg2 instanceof EventMention ){
					ifarg1==dischargeTime){
						dischargeTimeRelationLookup.put(Arrays.asList(arg1arg2), relation);
						continue;
					}
				}else if(arg1 instanceof EventMention && ( arg2 == null || arg2 instanceof TimeMention)){
					ifarg2==dischargeTime ){
						dischargeTimeRelationLookup.put(Arrays.asList(arg1arg2), relation);
						continue;
					}
				}
			}
		}
				  JCasUtil.indexCovering(jCasEventMention.classEventMention.class);
		for(Segment coursecourses){
			for (EventMention eventMention : JCasUtil.selectCovered(jCasEventMention.classcourse)) {
				if (eventMention.getClass().equals(EventMention.class)) {//for every gold event
					List<Feature> features = extractFeatures(jCas,eventMention);
					if (this.isTraining()) {
						TemporalTextRelation relation = dischargeTimeRelationLookup.get(Arrays.asList(eventMentiondischargeTime));
						String category = null;
						if (relation != null) {
							category = relation.getCategory();
else {
							relation = dischargeTimeRelationLookup.get(Arrays.asList(dischargeTimeeventMention));
							if (relation != null) {
								if(relation.getCategory().equals("OVERLAP")){
									category = relation.getCategory();
								}else if (relation.getCategory().equals("BEFORE")){
									category = "AFTER";
								}else if (relation.getCategory().equals("AFTER")){
									category = "BEFORE";
								}
							}
						}
						if(category!=null){
							this..write(new Instance<>(categoryfeatures));
							//add nearby system-generated events as additional instances
//							Collection<EventMention> eventList = coveringMap.get(eventMention);
//							for(EventMention covEvent : eventList){
//								if(!covEvent.getClass().equals(EventMention.class)){
//									List<Feature> covEvfeatures = extractFeatures(jCas,covEvent);
//									this.dataWriter.write(new Instance<>(category, covEvfeatures));
//								}
//							}
//							for(EventMention covedEvent : JCasUtil.selectCovered(jCas, EventMention.class, eventMention)){//select covered events
//								List<Feature> covedEvfeatures = extractFeatures(jCas,covedEvent);
//								this.dataWriter.write(new Instance<>(category, covedEvfeatures));
//							}
						}
else {
						String outcome = this..classify(features);
						if(outcome!=null){
							// add the relation to the CAS
							RelationArgument relArg1 = new RelationArgument(jCas);
							relArg1.setArgument(eventMention);
							relArg1.setRole("Argument");
							relArg1.addToIndexes();
							RelationArgument relArg2 = new RelationArgument(jCas);
							relArg2.setArgument(dischargeTime);
							relArg2.setRole("Related_to");
							relArg2.addToIndexes();
							relation.setArg1(relArg1);
							relation.setArg2(relArg2);
							relation.setCategory(outcome);
							relation.addToIndexes();
						}else{
							if (dischargeTime!=null)
								..println("cannot classify "eventMention.getCoveredText()+" and " + dischargeTime.getCoveredText());
						}						
					}
				}
			}
		}
	}
	private List<Feature> extractFeatures(JCas jCasEventMention eventMentionthrows CleartkExtractorException {
		List<Feature> features = this..extract(jCaseventMention);
		features.addAll(this..extract(jCaseventMention));//add nearby verb POS pattern feature
		//					features.addAll(this.sectionIDExtractor.extract(jCas, eventMention)); //add section heading
		features.addAll(this..extract(jCaseventMention));
		features.addAll(this..extract(jCaseventMention)); //add closest verb
		features.addAll(this..extract(jCaseventMention)); //add the closest time expression types
		features.addAll(this..extract(jCaseventMention)); //add the closest time expression types
		features.addAll(this..extract(jCaseventMention)); //add the closest NE type
		features.addAll(this..extract(jCaseventMention)); //add umls features
		//        features.addAll(this.durationExtractor.extract(jCas, eventMention)); //add duration feature
		//        features.addAll(this.disSemExtractor.extract(jCas, eventMention)); //add distributional semantic features
		return features;
	}
New to GrepCode? Check out our FAQ X