TestFlexibleGazetteer.java
001 /*
002  *  TestFlexibleGazetteer.java
003  *
004  *  Copyright (c) 1995-2010, The University of Sheffield. See the file
005  *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
006  *
007  *  This file is part of GATE (see http://gate.ac.uk/), and is free
008  *  software, licenced under the GNU Library General Public License,
009  *  Version 2, June 1991 (in the distribution as file licence.html,
010  *  and also available at http://gate.ac.uk/gate/licence.html).
011  *
012  *  Mike Dowman, 25/3/2004
013  *
014  *  $Id: TestFlexibleGazetteer.java 12006 2009-12-01 17:24:28Z thomas_heitz $
015  */
016 
017 package gate.creole.gazetteer;
018 
019 import junit.framework.*;
020 import gate.*;
021 import gate.corpora.*;
022 import java.io.File;
023 import java.net.*;
024 import gate.gui.MainFrame;
025 import gate.util.GateRuntimeException;
026 
027 public class TestFlexibleGazetteer extends TestCase {
028 
029   private static final boolean DEBUG=false;
030 
031   public TestFlexibleGazetteer(String name) {
032     super(name);
033   }
034 
035   /** Fixture set up - does nothing */
036   public void setUp() throws Exception {
037     //make sure the right plugin is loaded
038     File pluginsHome = new File(System.getProperty(
039             GateConstants.GATE_HOME_PROPERTY_NAME)
040             "plugins");
041     try{
042       Gate.getCreoleRegister().registerDirectories(
043               new File(pluginsHome, "Tools").toURI().toURL());
044     }catch(Exception e){
045       throw new GateRuntimeException(e);
046     }
047   }
048 
049   /** Fixture tear down - does nothing */
050   public void tearDown() throws Exception {
051   // tearDown
052 
053   /** Tests the flexible gazetteer */
054   public void testFlexibleGazetteer() throws Exception {
055 
056     // Display the gui for debugging purposes.
057     if (DEBUG) {
058       MainFrame mainFrame = new MainFrame();
059       mainFrame.setVisible(true);
060     }
061 
062     //get a document - take it from the gate server.
063     // tests/doc0.html is a simple html document.
064     Document doc = Factory.newDocument(
065       new URL(TestDocument.getTestServerName() "tests/doc0.html")
066     );
067 
068     // Get a tokeniser - just use all the default settings.
069     gate.creole.tokeniser.DefaultTokeniser tokeniser=
070         (gate.creole.tokeniser.DefaultTokeniserFactory.createResource(
071         "gate.creole.tokeniser.DefaultTokeniser");
072 
073     gate.creole.splitter.SentenceSplitter splitter =
074         (gate.creole.splitter.SentenceSplitterFactory.createResource(
075         "gate.creole.splitter.SentenceSplitter");
076 
077     gate.creole.POSTagger tagger = (gate.creole.POSTaggerFactory.createResource(
078         "gate.creole.POSTagger");
079 
080     // Get a morphological analyser, again just use all the default settings.
081     gate.creole.morph.Morph morphologicalAnalyser=
082         (gate.creole.morph.MorphFactory.createResource(
083         "gate.creole.morph.Morph");
084 
085     // Get a default gazetteer, again just use all the default settings
086     gate.creole.gazetteer.Gazetteer gazetteerInst =
087         (gate.creole.gazetteer.DefaultGazetteerFactory.createResource(
088         "gate.creole.gazetteer.DefaultGazetteer");
089 
090     //create a flexible gazetteer
091     // First create a feature map containing all the relevant parameters.
092     FeatureMap params = Factory.newFeatureMap();
093     // Create a list of input features with just one feature (root) and add it
094     // to the feature map.
095     java.util.ArrayList testInputFeatures=new java.util.ArrayList();
096     testInputFeatures.add("Token.root");
097     params.put("inputFeatureNames", testInputFeatures);
098     params.put("gazetteerInst",gazetteerInst);
099 
100     // Actually create the gazateer
101     FlexibleGazetteer flexGaz = (FlexibleGazetteerFactory.createResource(
102                           "gate.creole.gazetteer.FlexibleGazetteer", params);
103 
104     // runtime stuff - set the document to be used with the gazetteer, the
105     // tokeniser and the analyser to doc, and run each of them in turn.
106     tokeniser.setDocument(doc);
107     tokeniser.execute();
108     splitter.setDocument(doc);
109     splitter.execute();
110     tagger.setDocument(doc);
111     tagger.execute();
112     morphologicalAnalyser.setDocument(doc);
113     morphologicalAnalyser.execute();
114     flexGaz.setDocument(doc);
115     flexGaz.execute();
116 
117     // Now check that the document has been annotated as expected.
118     // First get the default annotations.
119     AnnotationSet defaultAnnotations=doc.getAnnotations();
120 
121     // Now just get the lookups out of that set.
122     AnnotationSet lookups=defaultAnnotations.get("Lookup");
123 
124     // And check that all the correct lookups have been found.
125     // N.B. If the default gazetteer lists are ever changed, the correct value
126     // for the number of lookups found may also change.
127 
128     if (DEBUG) {
129       System.out.println("There are this many lookup annotations: "+
130                          lookups.size());
131     }
132     assertTrue(lookups.size()== 28);
133 
134     // Now clean up so we don't get a memory leak.
135     Factory.deleteResource(doc);
136     Factory.deleteResource(tokeniser);
137     Factory.deleteResource(morphologicalAnalyser);
138     Factory.deleteResource(flexGaz);
139   }
140 
141   /** Test suite routine for the test runner */
142   public static Test suite() {
143     return new TestSuite(TestFlexibleGazetteer.class);
144   // suite
145 
146   // The main class allows this class to be tested on its own, without the
147   // need to call it from another class.
148   public static void main(String[] args) {
149     try{
150       Gate.init();
151       TestFlexibleGazetteer testGaz = new TestFlexibleGazetteer("");
152       testGaz.setUp();
153       testGaz.testFlexibleGazetteer();
154       testGaz.tearDown();
155     catch(Exception e) {
156       e.printStackTrace();
157     }
158   // main
159 
160 // TestFlexibleGazetteer