TestAnnotationMerging.java
001 package gate.util;
002 
003 import gate.Annotation;
004 import gate.AnnotationSet;
005 import gate.Corpus;
006 import gate.Document;
007 import gate.Factory;
008 import gate.Gate;
009 import gate.GateConstants;
010 import java.io.File;
011 import java.util.HashMap;
012 import junit.framework.Test;
013 import junit.framework.TestCase;
014 import junit.framework.TestSuite;
015 
016 public class TestAnnotationMerging extends TestCase {
017   /** The id of test case. */
018   int caseN;
019 
020   /** Construction */
021   public TestAnnotationMerging(String name) {
022     super(name);
023   }
024 
025   /** Fixture set up */
026   public void setUp() {
027   // setUp
028 
029   /**
030    * Put things back as they should be after running tests.
031    */
032   public void tearDown() throws Exception {
033   // tearDown
034 
035   /** Test suite routine for the test runner */
036   public static Test suite() {
037     return new TestSuite(TestAnnotationMerging.class);
038   // suite
039 
040   private Document loadDocument(String path, String namethrows Exception {
041     Document doc = Factory.newDocument(Gate.getUrl(path)"UTF-8");
042     doc.setName(name);
043     return doc;
044   }
045 
046   /** The test for AnnotationMerging. */
047   public void testAnnotationMerging() throws Exception {
048 
049     Boolean savedSpaceSetting = Gate.getUserConfig().getBoolean(
050             GateConstants.DOCUMENT_ADD_SPACE_ON_UNPACK_FEATURE_NAME);
051     Gate.getUserConfig().put(
052             GateConstants.DOCUMENT_ADD_SPACE_ON_UNPACK_FEATURE_NAME,
053             Boolean.FALSE);
054     try {
055 
056       //Gate.setGateHome(new File("C:\\svn\\gate"));
057       //Gate.setUserConfigFile(new File("C:\\svn\\gate.xml"));
058       //Gate.init();
059       // Load the documents into a corpus
060       Corpus data = Factory.newCorpus("data");
061       // Put the annotated document into a matrix for IAA
062       String nameAnnSet;
063       String nameAnnType = "";
064       String nameAnnFeat = "";
065       // Use the dataset of one document and three annotators
066       data.add(loadDocument("tests/iaa/beijing-opera.xml""beijing-opera.xml"));
067       //ExtensionFileFilter fileFilter = new ExtensionFileFilter();
068       //fileFilter.addExtension("xml");
069       //data.populate(new File("C:\\yaoyong_h\\work\\iaa\\data\\smallData").toURI().toURL(), fileFilter, "UTF-8", false);
070       
071       nameAnnSet = "ann1;ann2;ann3";
072       boolean isUsingMajority=false;
073       nameAnnType = "sent";
074       nameAnnFeat = "Op";
075       caseN = 1;
076       isUsingMajority=true;
077       testWithfeat(nameAnnSet, nameAnnType, nameAnnFeat,
078               data, isUsingMajority);
079       
080       caseN = 2;
081       isUsingMajority=false;
082       testWithfeat(nameAnnSet, nameAnnType, nameAnnFeat,
083               data, isUsingMajority);
084 
085       
086       nameAnnType = "Os";
087       nameAnnFeat = null;
088       caseN = 3;
089       isUsingMajority=true;
090       testWithfeat(nameAnnSet, nameAnnType, nameAnnFeat, data, isUsingMajority);
091       
092       caseN = 4;
093       isUsingMajority=false;
094       testWithfeat(nameAnnSet, nameAnnType, nameAnnFeat, data, isUsingMajority);
095     }
096     finally {
097       Gate.getUserConfig().put(
098               GateConstants.DOCUMENT_ADD_SPACE_ON_UNPACK_FEATURE_NAME,
099               savedSpaceSetting);
100     }
101 
102   }
103 
104  
105   /** The actual method for testing. */
106   public void testWithfeat(String nameAnnSets, String nameAnnType, String nameAnnFeat, Corpus data, boolean isUsingMajority) {
107     //  get the annotation sets
108     String [] annSetsN = nameAnnSets.split(";");
109     int numJudges = annSetsN.length;
110     int numDocs = data.size();
111     AnnotationSet[][] annArr2 = new AnnotationSet[numDocs][numJudges];
112     for(int i = 0; i < numDocs; ++i) {
113       Document doc = (Document)data.get(i);
114       for(int j=0; j<numJudges; ++j) {
115         // Get the annotation
116         annArr2[i][j= doc.getAnnotations(annSetsN[j]).get(nameAnnType);
117       }
118     }
119     //Annotation merging
120     boolean isTheSameInstances = true;
121     for(int i=0; i<annArr2.length; ++i)
122       if(!AnnotationMerging.isSameInstancesForAnnotators(annArr2[i]1)) {
123         isTheSameInstances = false;
124         break;
125       }
126     HashMap<Annotation,String>mergeInfor = new HashMap<Annotation,String>();
127     if(isUsingMajority)
128       AnnotationMerging.mergeAnnotationMajority(annArr2[0], nameAnnFeat, mergeInfor, isTheSameInstances);
129     else AnnotationMerging.mergeAnnotation(annArr2[0], nameAnnFeat, mergeInfor, 2, isTheSameInstances);
130     int numAnns=0;
131     if(isTheSameInstances) {
132       for(Annotation ann:mergeInfor.keySet()) {
133         if(ann.getFeatures().get(nameAnnFeat!= null)
134           ++numAnns;
135          
136       }
137     else {
138       numAnns = mergeInfor.size();
139     }
140     checkNumbers(numAnns);
141   }
142 
143   /** Check the numbers. */
144   private void checkNumbers(int numAnns) {
145     switch(caseN) {
146       case 1:
147         assertEquals(numAnns, 9);
148         break;
149       case 2:
150         assertEquals(numAnns, 9);
151         break;
152       case 3:
153         assertEquals(numAnns, 2);
154         break;
155       case 4:
156         assertEquals(numAnns, 2);
157         break;
158       default:
159         System.out.println("The test case " + caseN + " is not defined yet.");
160     }
161   }
162 
163 }