1
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
36 public void setUp() throws Exception {
37 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
51 public void tearDown() throws Exception {
52 }
54
57 public void testMaxentWrapper() throws Exception {
58 java.io.PrintStream normalOutputStream=System.out;
60
61 if (DEBUG) {
63 MainFrame mainFrame = new MainFrame();
64 mainFrame.setVisible(true);
65 } else {
66 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 Document doc = Factory.newDocument(
78 new URL(TestDocument.getTestServerName() + "tests/doc0.html")
79 );
80
81 gate.creole.tokeniser.DefaultTokeniser tokeniser=
83 (gate.creole.tokeniser.DefaultTokeniser) Factory.createResource(
84 "gate.creole.tokeniser.DefaultTokeniser");
85
86 gate.creole.gazetteer.Gazetteer gazetteerInst =
88 (gate.creole.gazetteer.DefaultGazetteer) Factory.createResource(
89 "gate.creole.gazetteer.DefaultGazetteer");
90
91 FeatureMap maxentParameters = Factory.newFeatureMap();
94 maxentParameters.put("configFileURL",
95 Gate.class.getResource(Files.getResourcePath() +
96 "/gate.ac.uk/tests/TestMaxentConfigFile.xml"));
97 gate.creole.ml.MachineLearningPR maxentPR =
99 (gate.creole.ml.MachineLearningPR)
100 Factory.createResource("gate.creole.ml.MachineLearningPR",
101 maxentParameters);
102
103 tokeniser.setDocument(doc);
106 tokeniser.execute();
107 gazetteerInst.setDocument(doc);
108 gazetteerInst.execute();
109 maxentPR.setDocument(doc);
110 maxentPR.execute();
111
112 maxentPR.setTraining(new Boolean(false));
114 maxentPR.execute();
115
116 Factory.deleteResource(doc);
118 Factory.deleteResource(tokeniser);
119 Factory.deleteResource(maxentPR);
120 Factory.deleteResource(gazetteerInst);
121
122 System.setOut(normalOutputStream);
124 }
126
127 public static Test suite() {
128 return new TestSuite(TestMaxentWrapper.class);
129 }
131 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 }
145 }