TestAnnic.java
001 /*
002  *  TestAnnic.java
003  *
004  *  Niraj Aswani, 19/March/07
005  *
006  *  $Id: TestAnnic.html,v 1.0 2007/03/19 16:22:01 niraj Exp $
007  */
008 package gate.creole.annic.test;
009 
010 import junit.framework.Test;
011 import junit.framework.TestSuite;
012 import junit.framework.TestCase;
013 import gate.creole.annic.Constants;
014 import gate.creole.annic.Hit;
015 import gate.creole.annic.Parser;
016 import gate.creole.annic.lucene.LuceneSearcher;
017 import java.io.*;
018 import java.util.ArrayList;
019 import java.util.HashMap;
020 
021 import gate.*;
022 import gate.creole.splitter.SentenceSplitter;
023 
024 /**
025  * A class to test ANNIC Functionalities.
026  
027  @author niraj
028  */
029 public class TestAnnic extends TestCase {
030 
031   /**
032    * Corpus to index
033    */
034   private Corpus testCorpus;
035 
036   /**
037    * ANNIC Home
038    */
039   private File annicHome;
040 
041   /**
042    * Index URL
043    */
044   private File indexURL;
045 
046   /**
047    * Constructor
048    
049    @param dummy
050    */
051   public TestAnnic(String dummy) {
052     super(dummy);
053   }
054 
055   /**
056    * This method sets up the parameters for the files to be testes It
057    * initialises the Tokenizer and sets up the other parameters for the
058    * morph program
059    */
060   protected void setUp() throws Exception {
061     super.setUp();
062     indexURL = new File(File.createTempFile("abc""abc").getParentFile(),
063             "test-index");
064     if(!indexURL.exists() || !indexURL.isDirectory()) indexURL.mkdir();
065   }
066 
067   /**
068    * Called when tests ends
069    */
070   protected void tearDown() throws Exception {
071     // clean up
072     super.tearDown();
073   }
074 
075   /**
076    * Testing the annic indexing functionalities
077    
078    @throws Exception
079    */
080   public void testAnnicIndexing() throws Exception {
081     // lets create a corpus
082     testCorpus = Factory.newCorpus("TestAnnic");
083 
084     File directory = new File(new File(new File(new File(new File(new File(Gate
085             .getGateHome()"src")"gate")"resources")"gate.ac.uk"),
086             "tests")"annic");
087     File[] files = directory.listFiles();
088     for(int i = 0; i < files.length; i++) {
089       if(files[i].isFile()) {
090         Document doc = Factory.newDocument(files[i].toURI().toURL()
091                 "ISO-8859-1");
092         testCorpus.add(doc);
093       }
094     }
095 
096     AnnicIndexing annicPR = new AnnicIndexing();
097     SentenceSplitter splitter = (SentenceSplitter)Factory
098             .createResource("gate.creole.splitter.SentenceSplitter");
099     splitter.setInputASName("Key");
100     splitter.setOutputASName("Key");
101     for(int i = 0; i < testCorpus.size(); i++) {
102       splitter.setDocument((Document)testCorpus.get(i));
103       splitter.execute();
104     }
105 
106     // index
107     annicPR.setAnnotationSetName("Key");
108     annicPR.setBaseTokenAnnotationType("Token");
109     annicPR.setCorpus(testCorpus);
110     annicPR.setIndexUnitAnnotationType("Sentence");
111     annicPR.setIndexOutputDirectoryLocation(indexURL.toURI().toURL());
112     annicPR.execute();
113     Factory.deleteResource(testCorpus);
114 
115   }
116 
117   /**
118    * Testing annic searching functionalities.
119    
120    @throws Exception
121    */
122   public void testSearcher() throws Exception {
123     LuceneSearcher searcher = new LuceneSearcher();
124     HashMap parameters = new HashMap();
125     ArrayList indexLocations = new ArrayList();
126     indexLocations.add(indexURL.getAbsolutePath());
127     parameters.put(Constants.INDEX_LOCATIONS, indexLocations);
128     parameters.put(Constants.CONTEXT_WINDOW, new Integer(5));
129     String query = "{Person}";
130     boolean success = searcher.search(query, parameters);
131     int noOfHits = searcher.next(-1).length;
132     assertEquals(12, noOfHits);
133 
134     query = "{Organization}({Token})*3{Person}";
135     success = searcher.search(query, parameters);
136     noOfHits = searcher.next(-1).length;
137     assertEquals(noOfHits, 0);
138 
139     query = "{Organization}({Token})*3 (\"up\" | \"down\") ({Token})*3 ({Money} | {Percent})";
140     success = searcher.search(query, parameters);
141     Hit toExport[] = searcher.next(-1);
142     assertEquals(toExport.length, 0);
143 
144     String xmlRepresentation = Parser.toXML(toExport);
145     // and then read it back
146     toExport = Parser.fromXML(xmlRepresentation);
147     xmlRepresentation = Parser.toXML(toExport);
148   }
149 
150   /**
151    * A Method that is called from gate.TestGate to invoke the testing
152    * methods.
153    
154    @return
155    */
156   public static Test suite() {
157     return new TestSuite(TestAnnic.class);
158   }
159 }