1   /*
2    *
3    *  Copyright (c) 1998-2005, The University of Sheffield.
4    *
5    *  This file is part of GATE (see http://gate.ac.uk/), and is free
6    *  software, licenced under the GNU Library General Public License,
7    *  Version 2, June 1991 (in the distribution as file licence.html,
8    *  and also available at http://gate.ac.uk/gate/licence.html).
9    *
10   *  Valentin Tablan, 26/Feb/2002
11   *
12   *  $Id: TestDiffer.java,v 1.5 2005/01/11 13:51:37 ian Exp $
13   */
14  
15  package gate.util;
16  
17  import java.net.URL;
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import junit.framework.*;
22  
23  import gate.*;
24  
25  public class TestDiffer extends TestCase{
26    /** Construction */
27    public TestDiffer(String name) { super(name); }
28  
29    /** Fixture set up */
30     public void setUp() {
31     } // setUp
32  
33     /** Put things back as they should be after running tests.
34       */
35     public void tearDown() throws Exception {
36     } // tearDown
37  
38     /** Test suite routine for the test runner */
39     public static Test suite() {
40       return new TestSuite(TestDiffer.class);
41     } // suite
42  
43    /** Jdk compiler */
44     public void testDiffer() throws Exception {
45       Document doc = Factory.newDocument(
46           new URL(gate.corpora.TestDocument.getTestServerName() +
47                   "tests/ft-bt-03-aug-2001.html"),
48           "windows-1252"
49           );
50       AnnotationSet annSet = doc.getAnnotations();
51       //create 100 annotations
52       FeatureMap features = Factory.newFeatureMap();
53       features.put("type", "BAR");
54       for (int i = 0; i < 100; i++) {
55         annSet.add(new Long(i * 10), new Long( (i + 1) * 10), "Foo", features);
56       }
57       List keySet = new ArrayList(annSet);
58       List responseSet = new ArrayList(annSet);
59  
60       //check 100% Precision and recall
61       AnnotationDiffer differ = new AnnotationDiffer();
62       differ.setSignificantFeaturesSet(null);
63       differ.calculateDiff(keySet, responseSet);
64       differ.sanityCheck();
65       if(DEBUG) differ.printMissmatches();
66       double value = differ.getPrecisionStrict();
67       Assert.assertEquals("Precision Strict: " + value + " instead of 1!",
68                           1, value, 0);
69       value = differ.getRecallStrict();
70       Assert.assertEquals("Recall Strict: " + value + " instead of 1!",
71                           1, value, 0);
72       value = differ.getPrecisionLenient();
73       Assert.assertEquals("Precision Lenient: " + value + " instead of 1!",
74                           1, value, 0);
75       value = differ.getRecallLenient();
76       Assert.assertEquals("Recall Lenient: " + value + " instead of 1!",
77                           1, value, 0);
78  
79       //check low precision
80       Integer id = annSet.add(new Long(2), new Long(4), "Foo", features);
81       Annotation falsePositive = annSet.get(id);
82       responseSet.add(falsePositive);
83       differ.calculateDiff(keySet, responseSet);
84       differ.sanityCheck();
85       if(DEBUG) differ.printMissmatches();
86       value = differ.getPrecisionStrict();
87       Assert.assertEquals("Precision Strict: " + value + " instead of .99!",
88                           .99, value, .001);
89       //recall should still be 100%
90       value = differ.getRecallStrict();
91       Assert.assertEquals("Recall Strict: " + value + " instead of 1!",
92                           1, value, 0);
93       value = differ.getRecallLenient();
94       Assert.assertEquals("Recall Lenient: " + value + " instead of 1!",
95                           1, value, 0);
96  
97  
98       //check low recall
99       responseSet.remove(falsePositive);
100      keySet.add(falsePositive);
101      differ.calculateDiff(keySet, responseSet);
102      differ.sanityCheck();
103      if(DEBUG) differ.printMissmatches();
104      value = differ.getRecallStrict();
105      Assert.assertEquals("Recall Strict: " + value + " instead of .99!",
106                          .99, value, .001);
107      //precision should still be 100%
108      value = differ.getPrecisionStrict();
109      Assert.assertEquals("Precision Strict: " + value + " instead of 1!",
110                          1, value, 0);
111      value = differ.getPrecisionLenient();
112      Assert.assertEquals("Precision Lenient: " + value + " instead of 1!",
113                          1, value, 0);
114    }
115 
116    /** Debug flag */
117    private static final boolean DEBUG = false;
118 
119 }