001 /*
002 *
003 * Copyright (c) 1995-2010, The University of Sheffield. See the file
004 * COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
005 *
006 * This file is part of GATE (see http://gate.ac.uk/), and is free
007 * software, licenced under the GNU Library General Public License,
008 * Version 2, June 1991 (in the distribution as file licence.html,
009 * and also available at http://gate.ac.uk/gate/licence.html).
010 *
011 * Valentin Tablan, 26/Feb/2002
012 *
013 * $Id: TestDiffer.java 12006 2009-12-01 17:24:28Z thomas_heitz $
014 */
015
016 package gate.util;
017
018 import java.net.URL;
019 import java.util.ArrayList;
020 import java.util.List;
021
022 import junit.framework.*;
023
024 import gate.*;
025
026 public class TestDiffer extends TestCase{
027 /** Construction */
028 public TestDiffer(String name) { super(name); }
029
030 /** Fixture set up */
031 public void setUp() {
032 } // setUp
033
034 /** Put things back as they should be after running tests.
035 */
036 public void tearDown() throws Exception {
037 } // tearDown
038
039 /** Test suite routine for the test runner */
040 public static Test suite() {
041 return new TestSuite(TestDiffer.class);
042 } // suite
043
044 /** Jdk compiler */
045 public void testDiffer() throws Exception {
046 Document doc = Factory.newDocument(
047 new URL(gate.corpora.TestDocument.getTestServerName() +
048 "tests/ft-bt-03-aug-2001.html"),
049 "windows-1252"
050 );
051 AnnotationSet annSet = doc.getAnnotations();
052 //create 100 annotations
053 FeatureMap features = Factory.newFeatureMap();
054 features.put("type", "BAR");
055 for (int i = 0; i < 100; i++) {
056 annSet.add(new Long(i * 10), new Long( (i + 1) * 10), "Foo", features);
057 }
058 List keySet = new ArrayList(annSet);
059 List responseSet = new ArrayList(annSet);
060
061 //check 100% Precision and recall
062 AnnotationDiffer differ = new AnnotationDiffer();
063 differ.setSignificantFeaturesSet(null);
064 differ.calculateDiff(keySet, responseSet);
065 differ.sanityCheck();
066 if(DEBUG) differ.printMissmatches();
067 double value = differ.getPrecisionStrict();
068 Assert.assertEquals("Precision Strict: " + value + " instead of 1!",
069 1, value, 0);
070 value = differ.getRecallStrict();
071 Assert.assertEquals("Recall Strict: " + value + " instead of 1!",
072 1, value, 0);
073 value = differ.getPrecisionLenient();
074 Assert.assertEquals("Precision Lenient: " + value + " instead of 1!",
075 1, value, 0);
076 value = differ.getRecallLenient();
077 Assert.assertEquals("Recall Lenient: " + value + " instead of 1!",
078 1, value, 0);
079
080 //check low precision
081 Integer id = annSet.add(new Long(2), new Long(4), "Foo", features);
082 Annotation falsePositive = annSet.get(id);
083 responseSet.add(falsePositive);
084 differ.calculateDiff(keySet, responseSet);
085 differ.sanityCheck();
086 if(DEBUG) differ.printMissmatches();
087 value = differ.getPrecisionStrict();
088 Assert.assertEquals("Precision Strict: " + value + " instead of .99!",
089 .99, value, .001);
090 //recall should still be 100%
091 value = differ.getRecallStrict();
092 Assert.assertEquals("Recall Strict: " + value + " instead of 1!",
093 1, value, 0);
094 value = differ.getRecallLenient();
095 Assert.assertEquals("Recall Lenient: " + value + " instead of 1!",
096 1, value, 0);
097
098
099 //check low recall
100 responseSet.remove(falsePositive);
101 keySet.add(falsePositive);
102 differ.calculateDiff(keySet, responseSet);
103 differ.sanityCheck();
104 if(DEBUG) differ.printMissmatches();
105 value = differ.getRecallStrict();
106 Assert.assertEquals("Recall Strict: " + value + " instead of .99!",
107 .99, value, .001);
108 //precision should still be 100%
109 value = differ.getPrecisionStrict();
110 Assert.assertEquals("Precision Strict: " + value + " instead of 1!",
111 1, value, 0);
112 value = differ.getPrecisionLenient();
113 Assert.assertEquals("Precision Lenient: " + value + " instead of 1!",
114 1, value, 0);
115 }
116
117 /** Debug flag */
118 private static final boolean DEBUG = false;
119
120 }
|