OverlapHelper { public static void evaluateOverlap(AnnotationFS gold, AnnotationFS sys, RecognitionMeasures measures) { if (gold.getEnd() <= sys.getBegin() || sys.getEnd() <= gold.getBegin()) { throw new IllegalArgumentException(String.format( "Annotations %s and %s do not overlap!", gold, sys)); } float goldBefore = measures.getGoldScore(); float goldLength = gold.getEnd() - gold.getBegin(); int deltaBefore = sys.getBegin() - gold.getBegin(); if (deltaBefore > 0) { measures.incrementMissing(deltaBefore / goldLength); } else { measures.incrementSpurious(-deltaBefore / goldLength); } int deltaAfter = sys.getEnd() - gold.getEnd(); if (deltaAfter > 0) { measures.incrementSpurious(deltaAfter / goldLength); } else { measures.incrementMissing(-deltaAfter / goldLength); } float overlapLength = Math.min(sys.getEnd(), gold.getEnd()) - Math.max(sys.getBegin(), gold.getBegin()); if (overlapLength <= 0) { throw new IllegalStateException("Overlap length = " + overlapLength); } measures.incrementMatching(overlapLength / goldLength); float goldAfter = measures.getGoldScore(); if (goldAfter - goldBefore - 1 > 0.01f) { throw new IllegalStateException("Sanity check failed: goldAfter - goldBefore = " + (goldAfter - goldBefore)); } } private  OverlapHelper(); static void evaluateOverlap(AnnotationFS gold, AnnotationFS sys,
                                       RecognitionMeasures measures);  }
@Test public void notOverlappingMustFail() { try { OverlapHelper.evaluateOverlap(anno(0, 10), anno(10, 15), new RecognitionMeasures()); } catch (IllegalArgumentException e) { return; } fail("IllegalArgumentException expected"); }
@Test public void sameBordersGive1() { RecognitionMeasures rm = new RecognitionMeasures(); OverlapHelper.evaluateOverlap(anno(5, 10), anno(5, 10), rm); assertEquals(1, rm.getMatchedScore(), 0.001d); assertEquals(0, rm.getMissedScore(), 0.001d); assertEquals(0, rm.getSpuriousScore(), 0.001d); }
@Test public void goldWiderBefore() { RecognitionMeasures rm = new RecognitionMeasures(); OverlapHelper.evaluateOverlap(anno(5, 10), anno(7, 10), rm); assertEquals(0.6, rm.getMatchedScore(), 0.001d); assertEquals(0.4, rm.getMissedScore(), 0.001d); assertEquals(0, rm.getSpuriousScore(), 0.001d); }
@Test public void goldWiderAfter() { RecognitionMeasures rm = new RecognitionMeasures(); OverlapHelper.evaluateOverlap(anno(5, 15), anno(5, 10), rm); assertEquals(0.5, rm.getMatchedScore(), 0.001d); assertEquals(0.5, rm.getMissedScore(), 0.001d); assertEquals(0, rm.getSpuriousScore(), 0.001d); }
@Test public void goldBefore() { RecognitionMeasures rm = new RecognitionMeasures(); OverlapHelper.evaluateOverlap(anno(5, 15), anno(10, 20), rm); assertEquals(0.5, rm.getMatchedScore(), 0.001d); assertEquals(0.5, rm.getMissedScore(), 0.001d); assertEquals(0.5, rm.getSpuriousScore(), 0.001d); }
@Test public void goldAfter() { RecognitionMeasures rm = new RecognitionMeasures(); OverlapHelper.evaluateOverlap(anno(5, 15), anno(0, 10), rm); assertEquals(0.5, rm.getMatchedScore(), 0.001d); assertEquals(0.5, rm.getMissedScore(), 0.001d); assertEquals(0.5, rm.getSpuriousScore(), 0.001d); }
@Test public void goldWider() { RecognitionMeasures rm = new RecognitionMeasures(); OverlapHelper.evaluateOverlap(anno(0, 20), anno(10, 15), rm); assertEquals(0.25, rm.getMatchedScore(), 0.001d); assertEquals(0.75, rm.getMissedScore(), 0.001d); assertEquals(0, rm.getSpuriousScore(), 0.001d); }
@Test public void goldInside() { RecognitionMeasures rm = new RecognitionMeasures(); OverlapHelper.evaluateOverlap(anno(5, 15), anno(0, 20), rm); assertEquals(1, rm.getMatchedScore(), 0.001d); assertEquals(0, rm.getMissedScore(), 0.001d); assertEquals(1, rm.getSpuriousScore(), 0.001d); }