2

Could anyone please guide me how can I create one custom JAPE file and configure it with GATE source code. I have tried with following code and getting exception like "Error while parsing the grammar :" and "Neither grammarURL or binaryGrammarURL parameters are set!"

     try{
             Document doc = new DocumentImpl();
              String str = "This is test.";
              DocumentContentImpl impl = new DocumentContentImpl(str);
              doc.setContent(impl);
          System.setProperty("gate.home", "C:\\Program Files\\GATE_Developer_7.1"); 
          Gate.init();
          gate.Corpus corpus = (Corpus) Factory
            .createResource("gate.corpora.CorpusImpl");
          File gateHome = Gate.getGateHome();
          File pluginsHome = new File(gateHome, "plugins");
          Gate.getCreoleRegister().registerDirectories(new File(pluginsHome, "ANNIE").toURI().toURL());  

          Transducer transducer = new Transducer();
             transducer.setDocument(doc);
transducer.setGrammarURL(new URL("file:///D:/misc_workspace/gate-7.1-build4485-SRC/plugins/ANNIE/resources/NE/SportsCategory.jape"));
transducer.setBinaryGrammarURL(new URL("file:///D:/misc_workspace/gate-7.1-build4485-SRC/plugins/ANNIE/resources/NE/SportsCategory.jape"));

LanguageAnalyser jape = (LanguageAnalyser)Factory.createResource(
                  "gate.creole.Transducer", gate.Utils.featureMap(
                          "grammarURL", "D:/misc_workspace/gate-7.1-build4485-SRC/plugins/ANNIE/resources/NE/SportsCategory.jape",
                          "encoding", "UTF-8"));
abhijit nag
  • 451
  • 6
  • 24

3 Answers3

3

You would need to load the ANNIE plugin

Gate.getCreoleRegister().registerDirectories(
  new File(Gate.getPluginsHome(), "ANNIE").toURI().toURL());

and then create an instance of gate.creole.Transducer with the right parameters

LanguageAnalyser jape = (LanguageAnalyser)Factory.createResource(
  "gate.creole.Transducer", gate.Utils.featureMap(
      "grammarURL", new URL("file:///D:/path/to/my-grammar.jape"),
      "encoding", "UTF-8")); // ensure this matches the file

But the approach we usually advocate is to get your whole pipeline assembled and configured the way you want it in GATE Developer, with whatever standard components you need plus your own grammars, then save the application state to a file. You can then reload the whole application from code using a single line

CorpusController app = (CorpusController) PersistenceManager.loadObjectFromFile(savedAppFile);

Edit: the code you've added to your question has several fundamental problems. Firstly you must call Gate.init() before you do anything else with GATE - it must be before you create your Document. And secondly you must never call the constructor of a Resource class directly - always use the Factory. Likewise you should never need to call init() directly, as this is done for you as part of Factory.createResource. For example:

// initialise GATE
Gate.setGateHome(new File("C:\\Program Files\\GATE_Developer_7.1"));
Gate.init();

// load ANNIE plugin - you must do this before you can create tokeniser
// or JAPE transducer resources.
Gate.getCreoleRegister().registerDirectories(
   new File(Gate.getPluginsHome(), "ANNIE").toURI().toURL());

// Build the pipeline
SerialAnalyserController pipeline =
  (SerialAnalyserController)Factory.createResource(
     "gate.creole.SerialAnalyserController");
LanguageAnalyser tokeniser = (LanguageAnalyser)Factory.createResource(
     "gate.creole.tokeniser.DefaultTokeniser");
LanguageAnalyser jape = (LanguageAnalyser)Factory.createResource(
  "gate.creole.Transducer", gate.Utils.featureMap(
      "grammarURL", new File("D:\\path\\to\\my-grammar.jape").toURI().toURL(),
      "encoding", "UTF-8")); // ensure this matches the file
pipeline.add(tokeniser);
pipeline.add(jape);

// create document and corpus
Corpus corpus = Factory.newCorpus(null);
Document doc = Factory.newDocument("This is test.");
corpus.add(doc);
pipeline.setCorpus(corpus);

// run it
pipeline.execute();

// extract results
System.out.println("Found annotations of the following types: " +
  doc.getAnnotations().getAllTypes());

If you haven't already I strongly recommend you work through the training course materials for at least module 5, which will show you the correct way to load a document and run processing resources over it.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Thanks a lot Ian. Could you please share the code to create instance of Transducer. I am getting exception like "Error while parsing the grammar :" and nested cause like "Neither grammarURL or binaryGrammarURL parameters are set!" – abhijit nag Feb 25 '13 at 02:38
  • @abhijitnag that was the `(LanguageAnalyser)Factory.createResource`bit. If you have already tried something which didn't work you should edit your question and include the non-working code so we can suggest fixes. – Ian Roberts Feb 25 '13 at 08:37
  • Ian,I have updated the the question with the code I have tried. Could you please suggest. – abhijit nag Feb 25 '13 at 15:41
1

Thank you Ian. These training courses material is helpful. But my problem was different and I have solved it. The following code snap is how to use custom jape file in GATE. Now my custom jape file is capable to produce new annotation.

 System.setProperty("gate.home", "C:\\Program Files\\GATE_Developer_7.1"); 
  Gate.init();

  ProcessingResource token = (ProcessingResource)   Factory.createResource("gate.creole.tokeniser.DefaultTokeniser",Factory.newFeatureMap());



 String str = "This is a test. Myself Abhijit Nag sport";
   Document doc = Factory.newDocument(str);


  gate.Corpus corpus = (Corpus) Factory.createResource("gate.corpora.CorpusImpl");
  corpus.add(doc);
  File gateHome = Gate.getGateHome();
  File pluginsHome = new File(gateHome, "plugins");

  Gate.getCreoleRegister().registerDirectories(new File(pluginsHome, "ANNIE").toURI().toURL());  


 LanguageAnalyser jape = (LanguageAnalyser)Factory.createResource(
              "gate.creole.Transducer", gate.Utils.featureMap(
                      "grammarURL", "file:///D:/misc_workspace/gate-7.1-build4485-SRC/plugins/ANNIE/resources/NE/SportsCategory.jape","encoding", "UTF-8"));
      jape.setCorpus(corpus);
      jape.setDocument(doc);
      jape.execute();

  pipeline = (SerialAnalyserController) Factory.createResource("gate.creole.SerialAnalyserController",
                Factory.newFeatureMap(), Factory.newFeatureMap(),"ANNIE");
              initAnnie();
              pipeline.setCorpus(corpus);
              pipeline.add(token);
              pipeline.add((ProcessingResource)jape.init());
              pipeline.execute();
      AnnotationSetImpl ann = (AnnotationSetImpl) doc.getAnnotations();
      System.out.println(" ...Total annotation "+ann.getAllTypes());
abhijit nag
  • 451
  • 6
  • 24
  • 1
    You're still using `new DocumentImpl()`, `doc.setContent`, etc. which should all be replaced by `Document doc = Factory.newDocument("This is a test ....");` - DocumentImpl is a resource type and the "don't use `new`, use the `Factory`" rule applies. – Ian Roberts Feb 25 '13 at 21:02
0

This is another option if you want to update the ANNIE pipeline.

  1. First get a list of the default/existing processing resources in your pipeline
  2. Create an instance of your JAPE rule
  3. Iterate over the list of existing processing resources adding each to a new collection. Add your own custom JAPE rule to this collection.
  4. When you execute your ANNIE pipeline your JAPE rule will automatically be picked up so no need to specify document paths or execute individually.

Sample code:

File pluginsHome = Gate.getPluginsHome();
File anniePlugin = new File(pluginsHome, "ANNIE");
File annieGapp = new File(anniePlugin, "ANNIE_with_defaults.gapp");
annieController = (CorpusController) PersistenceManager.loadObjectFromFile(annieGapp);

LanguageAnalyser jape = (LanguageAnalyser)Factory.createResource(
                "gate.creole.Transducer", gate.Utils.featureMap(
                        "grammarURL", new URL("file:///C://Program Files//gate-7.1//plugins//ANNIE//resources//NE//opensource.jape"),
                        "encoding", "UTF-8")); 

Collection<ProcessingResource> newPRS = new ArrayList<ProcessingResource>();
Collection<ProcessingResource> prs = annieController.getPRs();
for(ProcessingResource resource: prs){
    newPRS.add(resource);
}
newPRS.add((ProcessingResource)jape.init());
annieController.setPRs(newPRS);
cdugga
  • 3,849
  • 17
  • 81
  • 127