OntoGazetteerImpl.java
001 /*
002  * OntoGazetteerImpl.java
003  *
004  * Copyright (c) 2002, The University of Sheffield.
005  *
006  * This file is part of GATE (see http://gate.ac.uk/), and is free
007  * software, licenced under the GNU Library General Public License,
008  * Version 2, June1991.
009  *
010  * A copy of this licence is included in the distribution in the file
011  * licence.html, and is also available at http://gate.ac.uk/gate/licence.html.
012  *
013  * borislav popov 02/2002
014  *
015  */
016 package gate.creole.gazetteer;
017 
018 import gate.*;
019 import gate.creole.ExecutionException;
020 import gate.creole.ResourceInstantiationException;
021 
022 /** OntoGazetteerImpl <br>
023  *  An ontology-aware gazetteer, producing additional annotations
024  *  with features [class] and [ontology].
025  */
026 public class OntoGazetteerImpl extends AbstractOntoGazetteer {
027 
028   public OntoGazetteerImpl() {
029   }
030 
031   public java.util.Set lookup(String singleItem) {
032     return gaz.lookup(singleItem);
033   }
034 
035   /** Initialize this onto gazetteer
036    *  @return .*/
037   public Resource init() throws ResourceInstantiationException {
038     try {
039       checkParameters();
040 
041       // load gazetteer class from GATE classloader
042       Class cl = Class.forName(gazetteerName, true, Gate.getClassLoader());
043 
044       FeatureMap params = Factory.newFeatureMap();
045 
046       mappingDefinition = new MappingDefinition();
047       mappingDefinition.setURL(mappingURL);
048       mappingDefinition.load();
049 
050       params.put("caseSensitive",caseSensitive);
051       params.put("listsURL",listsURL);
052       params.put("encoding",encoding);
053       params.put("mappingDefinition",mappingDefinition);
054       gaz = (Gazetteer)Factory.createResource(cl.getName(),params);
055 
056     catch (ClassNotFoundException e) {
057       throw new RuntimeException("ClassNotFoundException : "+e.getMessage());
058     catch (InvalidFormatException e) {
059       throw new ResourceInstantiationException(e);
060     }
061     return this;
062   // init
063 
064   /** Executes this onto gazetteer over a pre-set document
065    *  @throws ExecutionException if something goes wrong with the execution */
066   public void execute()throws ExecutionException {
067     if (null == gaz) {
068       throw new ExecutionException("gazetteer not initialized (null).");
069     }
070 
071     gaz.setDocument(document);
072     gaz.setAnnotationSetName(annotationSetName);
073     gaz.setEncoding(encoding);
074     gaz.setCorpus(corpus);
075     gaz.execute();
076   // execute
077 
078   /**
079    * Checks the parameters set to this gazetteer
080    @throws ResourceInstantiationException if something goes wrong
081    */
082   private void checkParameters() throws ResourceInstantiationException {
083     boolean set = null!=gazetteerName;
084     set &= null!=listsURL;
085     set&=null!=mappingURL;
086     if (!set) {
087      throw new ResourceInstantiationException("some parameters are not set (e.g.gazetteerName,"
088         +"listURL,mappingDefinition, document");
089     }
090 
091   // checkParameters
092 
093   /**
094    * Removes a single string item from the gazetteer model
095    @param singleItem removes a string item from the gazetteer model
096    @return true if the string is removed from the model, otherwise - false
097    */
098   public boolean remove(String singleItem) {
099     return gaz.remove(singleItem);
100   }
101 
102   /**
103    * Adds a string item to the model and associates it with a Lookup
104    @param singleItem the string item to be added
105    @param lookup the lookup to be associated with the string item
106    @return true if the item has been added, otherwise - false.
107    */
108   public boolean add(String singleItem, Lookup lookup) {
109     return gaz.add(singleItem,lookup);
110   }
111 
112 // OntoGazetteerImpl