1
15
16 package gate;
17
18 import java.io.*;
19 import java.util.*;
20
21 import junit.framework.*;
22
23 import gate.creole.*;
24 import gate.creole.gazetteer.DefaultGazetteer;
25 import gate.creole.orthomatcher.OrthoMatcher;
26 import gate.creole.splitter.SentenceSplitter;
27 import gate.creole.tokeniser.DefaultTokeniser;
28 import gate.util.*;
29
30
31
116 public class CookBook extends TestCase
117 {
118
119 private static final boolean DEBUG = false;
120
121
122 Corpus corpus = null;
123
124
125 Document doc1 = null;
126
127
128 Document doc2 = null;
129
130
131 public void testResourceCreation() throws GateException {
132
133 FeatureMap params = Factory.newFeatureMap();
136
137 params.put(Document.DOCUMENT_URL_PARAMETER_NAME,
140 Gate.getUrl("tests/doc0.html"));
141 params.put(Document.DOCUMENT_MARKUP_AWARE_PARAMETER_NAME,
142 new Boolean(true));
143 Resource res = Factory.createResource("gate.corpora.DocumentImpl", params);
144
145 assertTrue(
147 "should be document but the class is: " + res.getClass().getName(),
148 res instanceof gate.Document
149 );
150 Document doc = (Document) res;
151 AnnotationSet markupAnnotations = doc.getAnnotations(
152 GateConstants.ORIGINAL_MARKUPS_ANNOT_SET_NAME);
153 assertNotNull("no markup annotations on doc " + doc, markupAnnotations);
155 int numMarkupAnnotations = markupAnnotations.size();
156 if(DEBUG)
157 Out.prln("annotations on doc after unpack= " + numMarkupAnnotations);
158 assertTrue(
159 "wrong number annots on doc: " + doc + numMarkupAnnotations,
160 numMarkupAnnotations == 27
161 );
162
163 }
165
166 public void testCorpusConstruction() throws GateException {
167
168 corpus = Factory.newCorpus("My example corpus");
170
171 assertTrue(corpus.isEmpty());
173
174 }
176
177 public void testAddingDocuments() throws GateException {
178
179 corpus = Factory.newCorpus("My example corpus");
180
181 corpus.add(doc1);
183 corpus.add(doc2);
184
185 Iterator iter = corpus.iterator();
187 while(iter.hasNext()) {
188 Document doc = (Document) iter.next();
189 assertTrue(
190 "document url not as expected",
191 doc.getSourceUrl().toExternalForm().endsWith("doc0.html") ||
192 doc.getSourceUrl().toExternalForm().endsWith("test1.htm")
193 );
194 }
196 }
198
199 public void testAddingAnnotations() {
200 AnnotationSet as = doc1.getAnnotations();
201 FeatureMap fm = doc1.getFeatures();
202 Integer id;
203
204 try {
207 id = as.add(new Long(10), new Long(20), "T1", fm);
208 } catch (InvalidOffsetException e) {
209 fail(e.toString());
210 }
211 }
213
214 public void testUsingFeatures() {
215 AnnotationSet as = doc1.getAnnotations();
216 Integer id;
218 FeatureMap fm = Factory.newFeatureMap();
220 doc1.setFeatures(fm);
221 assertTrue(fm.size() == 0);
222 fm.put("author", "segovia");
223 assertTrue(fm.get("author").equals("segovia"));
224 fm.put("author", "brendl"); assertTrue(fm.get("author").equals("brendl"));
226 assertTrue(fm.size() == 1);
227
228 }
230
231 private static String usage =
232 "usage: CookBook [-dir directory-name | file(s)]";
233
234
254 public static void main(String[] args) throws Exception {
255 Out.prln("CookBook.main");
257 Out.prln("processing command line arguments");
258
259 List inputFiles = null;
261 if(args.length < 1) throw new GateException(usage);
262
263 if(args[0].equals("-dir")) { if(args.length < 2) throw new GateException(usage);
266 File dir = new File(args[1]);
267 File[] filesArray = dir.listFiles();
268 if(filesArray == null)
269 throw new GateException(
270 dir.getPath() + " is not a directory; " + usage
271 );
272 inputFiles = Arrays.asList(filesArray);
273
274 } else { inputFiles = new ArrayList();
276 for(int i = 0; i < args.length; i++)
277 inputFiles.add(new File(args[i]));
278 }
279
280 if(inputFiles.isEmpty()) {
282 throw new GateException("No files to process!");
283 }
284
285 Out.prln("initialising GATE");
287 Gate.init();
288
289 Out.prln("creating PRs");
291 DefaultTokeniser tokeniser = (DefaultTokeniser)Factory.createResource(
293 "gate.creole.tokeniser.DefaultTokeniser");
294 SentenceSplitter splitter = (SentenceSplitter)Factory.createResource(
296 "gate.creole.splitter.SentenceSplitter");
297 POSTagger tagger = (POSTagger)Factory.createResource(
299 "gate.creole.POSTagger");
300
301 DefaultGazetteer gazetteer = (DefaultGazetteer)Factory.createResource(
303 "gate.creole.gazetteer.DefaultGazetteer");
304
305 ANNIETransducer transducer = (ANNIETransducer)Factory.createResource(
307 "gate.creole.ANNIETransducer");
308
309 OrthoMatcher orthomatcher = (OrthoMatcher) Factory.createResource(
311 "gate.creole.orthomatcher.OrthoMatcher");
312
313 String outDirName =
315 ((File) inputFiles.get(0)).getParent() + Strings.getFileSep() + "out";
316 if(! new File(outDirName).mkdir()){
317 throw new GateException("Could not create the output directory");
318 }
319
320 String nl = Strings.getNl(); String fsep =
323 Strings.getFileSep(); String indexName =
325 ( (File) inputFiles.get(0) ).getParent() + fsep + "index.html";
326 FileWriter indexWriter = new FileWriter(new File(indexName));
327 indexWriter.write("<HTML><HEAD><TITLE>Documents list</TITLE></HEAD>");
328 indexWriter.write(nl + "<BODY>" + nl + "<UL>" + nl);
329
330
338 Iterator filesIter = inputFiles.iterator();
340 Out.prln("looping on input files list");
341 while(filesIter.hasNext()) {
342 File inFile = (File) filesIter.next(); Out.prln("processing file " + inFile.getPath());
344 FeatureMap params = Factory.newFeatureMap();
346 params.put(Document.DOCUMENT_URL_PARAMETER_NAME,
348 inFile.toURL().toExternalForm());
349
350 params.put(Document.DOCUMENT_ENCODING_PARAMETER_NAME, "");
352
353 Document doc = (Document) Factory.createResource(
355 "gate.corpora.DocumentImpl", params
356 );
357
358 tokeniser.setDocument(doc);
360 splitter.setDocument(doc);
361 tagger.setDocument(doc);
362 gazetteer.setDocument(doc);
363 transducer.setDocument(doc);
364 orthomatcher.setDocument(doc);
365
366 tokeniser.execute();
368 splitter.execute();
369 tagger.execute();
370 gazetteer.execute();
371 transducer.execute();
372 orthomatcher.execute();
373
374
376 StringBuffer outFileName = new StringBuffer(inFile.getParent());
378 outFileName.append(fsep);
379 outFileName.append("out");
380 outFileName.append(fsep);
381 outFileName.append("gate__");
382 outFileName.append(inFile.getName());
383 outFileName.append(".txt");
384 File outFile = new File(outFileName.toString());
385 FileWriter outFileWriter = new FileWriter(outFile);
386 Out.prln("dumping " + outFile.getPath());
387
388 AnnotationSet tokens = doc.getAnnotations("nercAS").
391 get(ANNIEConstants.TOKEN_ANNOTATION_TYPE);
392 Iterator iter = tokens.iterator();
393 while(iter.hasNext()) {
394 Annotation token = (Annotation) iter.next();
395 FeatureMap tokFeats = token.getFeatures();
396 String tokStr = (String) tokFeats.
397 get(ANNIEConstants.TOKEN_STRING_FEATURE_NAME);
398 String tokPos = (String) tokFeats.
399 get(ANNIEConstants.TOKEN_CATEGORY_FEATURE_NAME);
400 outFileWriter.write(tokStr + "\t" + tokPos + nl);
401 }
402 outFileWriter.write(doc.getFeatures().get("entitySet").toString());
403
404 outFileWriter.close();
406 indexWriter.write(
407 "<LI><A href=\"" + inFile.getName() + "\">" + inFile.getName() +
408 "</a>" + " -> " + "<a href=\"" + "out" + fsep + outFile.getName() +
409 "\">" + "out" + fsep + outFile.getName() + "</a></LI>\n"
410 );
411
412 Out.prln("deleting gate doc");
414
415 Factory.deleteResource(doc);
416 }
418 indexWriter.write(nl + "</UL>" + nl + "</BODY></HTML>" + nl);
420 indexWriter.close();
421
422 Out.prln("The End (roll credits)");
423 }
425
426 public void setUp() throws GateException, IOException {
427 corpus = Factory.newCorpus("My example corpus");
428
429 doc1 = Factory.newDocument(Gate.getUrl("tests/doc0.html"));
430 doc2 = Factory.newDocument(Gate.getUrl("tests/html/test1.htm"));
431 }
433
434 public CookBook(String name) { super(name); }
435
436
437 public static Test suite() {
438 return new TestSuite(CookBook.class);
439 }
441 }