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