1   /*
2    *  TestFlexibleGazetteer.java
3    *
4    *  Copyright (c) 1998-2005, The University of Sheffield.
5    *
6    *  This file is part of GATE (see http://gate.ac.uk/), and is free
7    *  software, licenced under the GNU Library General Public License,
8    *  Version 2, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Mike Dowman, 25/3/2004
12   *
13   *  $Id: TestFlexibleGazetteer.java,v 1.9 2005/01/11 13:51:32 ian Exp $
14   */
15  
16  package gate.creole.gazetteer;
17  
18  import junit.framework.*;
19  import gate.*;
20  import gate.corpora.*;
21  import java.io.File;
22  import java.net.*;
23  import gate.gui.MainFrame;
24  import gate.util.GateRuntimeException;
25  
26  public class TestFlexibleGazetteer extends TestCase {
27  
28    private static final boolean DEBUG=false;
29  
30    public TestFlexibleGazetteer(String name) {
31      super(name);
32    }
33  
34    /** Fixture set up - does nothing */
35    public void setUp() throws Exception {
36      //make sure the right plugin is loaded
37      File pluginsHome = new File(System.getProperty(
38              GateConstants.GATE_HOME_PROPERTY_NAME), 
39              "plugins");
40      try{
41        Gate.getCreoleRegister().registerDirectories(
42                new File(pluginsHome, "Tools").toURL());
43      }catch(Exception e){
44        throw new GateRuntimeException(e);
45      }
46    }
47  
48    /** Fixture tear down - does nothing */
49    public void tearDown() throws Exception {
50    } // tearDown
51  
52    /** Tests the flexible gazetteer */
53    public void testFlexibleGazetteer() throws Exception {
54  
55      // Display the gui for debugging purposes.
56      if (DEBUG) {
57        MainFrame mainFrame = new MainFrame();
58        mainFrame.setVisible(true);
59      }
60  
61      //get a document - take it from the gate server.
62      // tests/doc0.html is a simple html document.
63      Document doc = Factory.newDocument(
64        new URL(TestDocument.getTestServerName() + "tests/doc0.html")
65      );
66  
67      // Get a tokeniser - just use all the default settings.
68      gate.creole.tokeniser.DefaultTokeniser tokeniser=
69          (gate.creole.tokeniser.DefaultTokeniser) Factory.createResource(
70          "gate.creole.tokeniser.DefaultTokeniser");
71  
72      gate.creole.splitter.SentenceSplitter splitter =
73          (gate.creole.splitter.SentenceSplitter) Factory.createResource(
74          "gate.creole.splitter.SentenceSplitter");
75  
76      gate.creole.POSTagger tagger = (gate.creole.POSTagger) Factory.createResource(
77          "gate.creole.POSTagger");
78  
79      // Get a morphological analyser, again just use all the default settings.
80      gate.creole.morph.Morph morphologicalAnalyser=
81          (gate.creole.morph.Morph) Factory.createResource(
82          "gate.creole.morph.Morph");
83  
84      // Get a default gazetteer, again just use all the default settings
85      gate.creole.gazetteer.Gazetteer gazetteerInst =
86          (gate.creole.gazetteer.DefaultGazetteer) Factory.createResource(
87          "gate.creole.gazetteer.DefaultGazetteer");
88  
89      //create a flexible gazetteer
90      // First create a feature map containing all the relevant parameters.
91      FeatureMap params = Factory.newFeatureMap();
92      // Create a list of input features with just one feature (root) and add it
93      // to the feature map.
94      java.util.ArrayList testInputFeatures=new java.util.ArrayList();
95      testInputFeatures.add("Token.root");
96      params.put("inputFeatureNames", testInputFeatures);
97      params.put("gazetteerInst",gazetteerInst);
98  
99      // Actually create the gazateer
100     FlexibleGazetteer flexGaz = (FlexibleGazetteer) Factory.createResource(
101                           "gate.creole.gazetteer.FlexibleGazetteer", params);
102 
103     // runtime stuff - set the document to be used with the gazetteer, the
104     // tokeniser and the analyser to doc, and run each of them in turn.
105     tokeniser.setDocument(doc);
106     tokeniser.execute();
107     splitter.setDocument(doc);
108     splitter.execute();
109     tagger.setDocument(doc);
110     tagger.execute();
111     morphologicalAnalyser.setDocument(doc);
112     morphologicalAnalyser.execute();
113     flexGaz.setDocument(doc);
114     flexGaz.execute();
115 
116     // Now check that the document has been annotated as expected.
117     // First get the default annotations.
118     AnnotationSet defaultAnnotations=doc.getAnnotations();
119 
120     // Now just get the lookups out of that set.
121     AnnotationSet lookups=defaultAnnotations.get("Lookup");
122 
123     // And check that all the correct lookups have been found.
124     // N.B. If the default gazetteer lists are ever changed, the correct value
125     // for the number of lookups found may also change.
126 
127     if (DEBUG) {
128       System.out.println("There are this many lookup annotations: "+
129                          lookups.size());
130     }
131     assertTrue(lookups.size()== 40);
132 
133     // Now clean up so we don't get a memory leak.
134     Factory.deleteResource(doc);
135     Factory.deleteResource(tokeniser);
136     Factory.deleteResource(morphologicalAnalyser);
137     Factory.deleteResource(flexGaz);
138   }
139 
140   /** Test suite routine for the test runner */
141   public static Test suite() {
142     return new TestSuite(TestFlexibleGazetteer.class);
143   } // suite
144 
145   // The main class allows this class to be tested on its own, without the
146   // need to call it from another class.
147   public static void main(String[] args) {
148     try{
149       Gate.init();
150       TestFlexibleGazetteer testGaz = new TestFlexibleGazetteer("");
151       testGaz.setUp();
152       testGaz.testFlexibleGazetteer();
153       testGaz.tearDown();
154     } catch(Exception e) {
155       e.printStackTrace();
156     }
157   } // main
158 
159 } // TestFlexibleGazetteer
160