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