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.eval;
 
 import java.io.File;
 import java.util.List;
 import java.util.Set;
 
 import  org.cleartk.eval.AnnotationStatistics;
 import  org.cleartk.util.ViewUriUtil;
 
 
 public abstract class EvaluationOfAnnotationSpans_ImplBase extends
 Evaluation_ImplBase<AnnotationStatistics<String>> {
 
 	private final Logger logger = Logger.getLogger(this.getClass().getName());
 	public void setLogging(Level levelFile outputFilethrows IOException {
 		if (!outputFile.getParentFile().exists()) {
 			outputFile.getParentFile().mkdirs();
 		}
 		this..setLevel(level);
 		FileHandler handler = new FileHandler(outputFile.getPath());
 		handler.setFormatter(new Formatter() {
 			public String format(LogRecord record) {
 				return record.getMessage() + '\n';
 			}
 		});
 		this..addHandler(handler);
 	}
 
 	private Class<? extends AnnotationannotationClass;
 
 			File baseDirectory,
 			File rawTextDirectory,
 			File xmlDirectory,
 			XMLFormat xmlFormat,
 			Subcorpus subcorpus,
 			File xmiDirectory,
 			File treebankDirectory,
 			Class<? extends AnnotationannotationClass) {
 		super(baseDirectoryrawTextDirectoryxmlDirectoryxmlFormatsubcorpusxmiDirectorytreebankDirectory);
 		this. = annotationClass;
 	}
 
 			File baseDirectory,
 			File rawTextDirectory
 			File xmlDirectory,
 			XMLFormat xmlFormat,
 			Subcorpus subcorpus,
 			File xmiDirectory,
 			Class<? extends AnnotationannotationClass) {
 		this(baseDirectory,rawTextDirectoryxmlDirectoryxmlFormatsubcorpusxmiDirectorynullannotationClass);
 	}
 
 	protected abstract AnalysisEngineDescription getDataWriterDescription(File directory)
	protected abstract void trainAndPackage(File directorythrows Exception;
	protected void train(CollectionReader collectionReaderFile directorythrows Exception {
		aggregateBuilder.add(CopyFromGold.getDescription(this.));
		aggregateBuilder.add(this.getDataWriterDescription(directory), "TimexView".);
		SimplePipeline.runPipeline(collectionReaderaggregateBuilder.createAggregate());
		this.trainAndPackage(directory);
	}
	protected abstract AnalysisEngineDescription getAnnotatorDescription(File directory)
	protected abstract Collection<? extends AnnotationgetGoldAnnotations(JCas jCasSegment segment);
  protected abstract Collection<? extends AnnotationgetSystemAnnotations(JCas jCasSegment segment);
	protected AnnotationStatistics<Stringtest(CollectionReader collectionReaderFile directory)
			throws Exception {
		aggregateBuilder.add(this.getAnnotatorDescription(directory), "TimexView".);
		if(this. != null){
			aggregateBuilder.add(AnalysisEngineFactory.createEngineDescription(WriteI2B2XML.class.this.), "TimexView".);
		}
		AnnotationStatistics<Stringstats = new AnnotationStatistics<>();
					public List<Integerapply(Annotation annotation) {
						return Arrays.asList(annotation.getBegin(), annotation.getEnd());
					}
				});
		for (Iterator<JCascasIter = new JCasIterator(collectionReaderaggregateBuilder.createAggregate()); casIter.hasNext();) {
			JCas jCas = casIter.next();
			JCas goldView = jCas.getView();
			JCas systemView = jCas.getView(.);
      this..fine("Errors in : " + ViewUriUtil.getURI(jCas).toString());
			for (Segment segment : JCasUtil.select(jCasSegment.class)) {
					Collection<? extends AnnotationgoldAnnotations = this.getGoldAnnotations(goldViewsegment);
					Collection<? extends AnnotationsystemAnnotations = this.getSystemAnnotations(systemViewsegment);
					stats.add(goldAnnotationssystemAnnotations);
					Set<AnnotationgoldSet = new TreeSet<>(bySpans);
					for (Annotation goldAnnotation : goldAnnotations) {
						// TODO: fix data so that this is not necessary
						if (goldAnnotation.getBegin() == . || goldAnnotation.getEnd() == .) {
							this..warning("Invalid annotation");
							continue;
						}
						goldSet.add(goldAnnotation);
					}
					//goldSet.addAll(goldAnnotations);
					Set<AnnotationsystemSet = new TreeSet<>(bySpans);
					systemSet.addAll(systemAnnotations);
					Set<AnnotationgoldOnly = new TreeSet<>(bySpans);
					goldOnly.addAll(goldSet);
					goldOnly.removeAll(systemSet);
					Set<AnnotationsystemOnly = new TreeSet<>(bySpans);
					systemOnly.addAll(systemSet);
					systemOnly.removeAll(goldSet);
					String text = jCas.getDocumentText().replaceAll("[\r\n]"" ");
					if (!goldOnly.isEmpty() || !systemOnly.isEmpty()) {
						this..fine("Errors in : " + ViewUriUtil.getURI(jCas).toString());
						Set<Annotationerrors = new TreeSet<>(bySpans);
						errors.addAll(goldOnly);
						errors.addAll(systemOnly);
						for (Annotation annotation : errors) {
							int begin = annotation.getBegin();
							int end = annotation.getEnd();
							int windowBegin = Math.max(0, begin - 50);
							int windowEnd = Math.min(text.length(), end + 50);
							String label = goldOnly.contains(annotation) ? "DROPPED:" : "ADDED:  ";
							this..fine(String.format(
									"%s  ...%s[!%s!:%d-%d]%s...",
									label,
									text.substring(windowBeginbegin),
									text.substring(beginend),
									begin,
									end,
									text.substring(endwindowEnd)));
						}
						//add correct predictions:
						for (Annotation annotationgoldSet){
							if (!errors.contains(annotation)){
								int begin = annotation.getBegin();
								int end = annotation.getEnd();
								int windowBegin = Math.max(0, begin - 50);
								int windowEnd = Math.min(text.length(), end + 50);
								String label = "CORRECT:";
								this..fine(String.format(
										"%s  ...%s[!%s!:%d-%d]%s...",
										label,
										text.substring(windowBeginbegin),
										text.substring(beginend),
										begin,
										end,
										text.substring(endwindowEnd)));
							}
						}
					}
					Set<AnnotationpartialGold = new HashSet<>();
					Set<AnnotationpartialSystem = new HashSet<>();
					// get overlapping spans
						// iterate over all remaining gold annotations
						for(Annotation gold : goldOnly){
							Annotation bestSystem = null;
							int bestOverlap = 0;
							for(Annotation system : systemOnly){
								if(system.getBegin() >= gold.getBegin() && system.getEnd() <= gold.getEnd()){
									// system completely contained by gold
									int overlap = system.getEnd() - system.getBegin();
									if(overlap > bestOverlap){
										bestOverlap = overlap;
										bestSystem = system;
									}
								}else if(gold.getBegin() >= system.getBegin() && gold.getEnd() <= system.getEnd()){
									// gold completely contained by gold
									int overlap = gold.getEnd() - gold.getBegin();
									if(overlap > bestOverlap){
										bestOverlap = overlap;
										bestSystem = system;
									}
								}
							}
							if(bestSystem != null){
								this..info(String.format("Allowed overlapping annotation: Gold(%s) => System(%s)\n"gold.getCoveredText(), bestSystem.getCoveredText()));
								partialGold.add(gold);
								partialSystem.add(bestSystem);
							}
						}
						if(partialGold.size() > 0){
							goldOnly.removeAll(partialGold);
							systemOnly.removeAll(partialSystem);
							assert partialGold.size() == partialSystem.size();
							this..info(String.format("Found %d overlapping spans and removed from gold/system errors\n"partialGold.size()));
						}
					}
				}
			}
		}
		return stats;
	}
New to GrepCode? Check out our FAQ X