1   /*
2    *  TestMaxentWrapper.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, 1/4/2004
12   *
13   *  $Id: TestMaxentWrapper.java,v 1.10 2005/01/11 13:51:32 ian Exp $
14   */
15  
16  package gate.creole.ml.maxent;
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.Files;
25  import gate.util.GateRuntimeException;
26  
27  public class TestMaxentWrapper extends TestCase {
28  
29    private static final boolean DEBUG=false;
30  
31    public TestMaxentWrapper(String name) {
32      super(name);
33    }
34  
35    /** Fixture set up - does nothing */
36    public void setUp() throws Exception {
37      //make sure the right plugin is loaded
38      File pluginsHome = new File(System.getProperty(
39              GateConstants.GATE_HOME_PROPERTY_NAME), 
40              "plugins");
41      try{
42        Gate.getCreoleRegister().registerDirectories(
43                new File(pluginsHome, "Machine_Learning").toURL());
44      }catch(Exception e){
45        throw new GateRuntimeException(e);
46      }
47      
48    }
49  
50    /** Fixture tear down - does nothing */
51    public void tearDown() throws Exception {
52    } // tearDown
53  
54    /** Tests the MAXENT machine learning wrapper, by training it to identify
55     * lookup annotations based on the precence of lookup annotations.
56     */
57    public void testMaxentWrapper() throws Exception {
58      // Store the original standard output stream, so we can restore it later.
59      java.io.PrintStream normalOutputStream=System.out;
60  
61      // Display the gui for debugging purposes.
62           if (DEBUG) {
63        MainFrame mainFrame = new MainFrame();
64        mainFrame.setVisible(true);
65      } else {
66        // We don't want the output displayed unless we are debugging, so set the
67        // standard output stream to a new one that never outputs anything.
68        System.setOut(new java.io.PrintStream(
69            new java.io.OutputStream() {
70          public void write(int b) { }
71          public void write(byte[] b, int off, int len) { }
72        }));
73      }
74  
75      //get a document - take it from the gate server.
76      // tests/doc0.html is a simple html document.
77      Document doc = Factory.newDocument(
78        new URL(TestDocument.getTestServerName() + "tests/doc0.html")
79      );
80  
81      // Get a tokeniser - just use all the default settings.
82      gate.creole.tokeniser.DefaultTokeniser tokeniser=
83          (gate.creole.tokeniser.DefaultTokeniser) Factory.createResource(
84          "gate.creole.tokeniser.DefaultTokeniser");
85  
86      // Get a default gazetteer, again just use all the default settings
87      gate.creole.gazetteer.Gazetteer gazetteerInst =
88          (gate.creole.gazetteer.DefaultGazetteer) Factory.createResource(
89          "gate.creole.gazetteer.DefaultGazetteer");
90  
91      // Create the Maxent ML Processing resource.
92      // First set up the parameters
93      FeatureMap maxentParameters = Factory.newFeatureMap();
94      maxentParameters.put("configFileURL",
95                           Gate.class.getResource(Files.getResourcePath() +  
96                                   "/gate.ac.uk/tests/TestMaxentConfigFile.xml"));
97      // Then actually make the PR
98      gate.creole.ml.MachineLearningPR maxentPR =
99          (gate.creole.ml.MachineLearningPR)
100         Factory.createResource("gate.creole.ml.MachineLearningPR",
101                                maxentParameters);
102 
103     // runtime stuff - set the document to be used with the gazetteer,the
104     // tokeniser and the ML PR to doc, and run each of them in turn.
105     tokeniser.setDocument(doc);
106     tokeniser.execute();
107     gazetteerInst.setDocument(doc);
108     gazetteerInst.execute();
109     maxentPR.setDocument(doc);
110     maxentPR.execute();
111 
112     // Now run the trained maxent model.
113     maxentPR.setTraining(new Boolean(false));
114     maxentPR.execute();
115 
116     // Now clean up so we don't get a memory leak.
117     Factory.deleteResource(doc);
118     Factory.deleteResource(tokeniser);
119     Factory.deleteResource(maxentPR);
120     Factory.deleteResource(gazetteerInst);
121 
122     // Restore the standard output stream.
123     System.setOut(normalOutputStream);
124   } // TestMaxentWrapper
125 
126   /** Test suite routine for the test runner */
127   public static Test suite() {
128     return new TestSuite(TestMaxentWrapper.class);
129   } // suite
130 
131   // The main class allows this class to be tested on its own, without the
132   // need to call it from another class.
133   public static void main(String[] args) {
134     try{
135       Gate.init();
136       TestMaxentWrapper testMax = new TestMaxentWrapper("");
137       testMax.setUp();
138       testMax.testMaxentWrapper();
139       testMax.tearDown();
140     } catch(Exception e) {
141       e.printStackTrace();
142     }
143   } // main
144 
145 } // TestFlexibleGazetteer
146