1   /*
2    * OntoGazetteerImpl.java
3    *
4    * Copyright (c) 2002, The University of Sheffield.
5    *
6    * This file is part of GATE (see http://gate.ac.uk/), and is free
7    * software, licenced under the GNU Library General Public License,
8    * Version 2, June1991.
9    *
10   * A copy of this licence is included in the distribution in the file
11   * licence.html, and is also available at http://gate.ac.uk/gate/licence.html.
12   *
13   * borislav popov 02/2002
14   *
15   */
16  package gate.creole.gazetteer;
17  
18  import gate.*;
19  import gate.creole.ExecutionException;
20  import gate.creole.ResourceInstantiationException;
21  
22  /** OntoGazetteerImpl <br>
23   *  An ontology-aware gazetteer, producing additional annotations
24   *  with features [class] and [ontology].
25   */
26  public class OntoGazetteerImpl extends AbstractOntoGazetteer {
27  
28    public OntoGazetteerImpl() {
29    }
30  
31    public java.util.Set lookup(String singleItem) {
32      return gaz.lookup(singleItem);
33    }
34  
35    /** Initialize this onto gazetteer
36     *  @return .*/
37    public Resource init() throws ResourceInstantiationException {
38      try {
39        checkParameters();
40  
41        Class cl = Class.forName(gazetteerName);
42  
43        FeatureMap params = Factory.newFeatureMap();
44  
45        mappingDefinition = new MappingDefinition();
46        mappingDefinition.setURL(mappingURL);
47        mappingDefinition.load();
48  
49        params.put("caseSensitive",caseSensitive);
50        params.put("listsURL",listsURL);
51        params.put("encoding",encoding);
52        params.put("mappingDefinition",mappingDefinition);
53        gaz = (Gazetteer)Factory.createResource(cl.getName(),params);
54  
55      } catch (ClassNotFoundException e) {
56        throw new RuntimeException("ClassNotFoundException : "+e.getMessage());
57      } catch (InvalidFormatException e) {
58        throw new ResourceInstantiationException(e);
59      }
60      return this;
61    } // init
62  
63    /** Executes this onto gazetteer over a pre-set document
64     *  @throws ExecutionException if something goes wrong with the execution */
65    public void execute()throws ExecutionException {
66      if (null == gaz) {
67        throw new ExecutionException("gazetteer not initialized (null).");
68      }
69  
70      gaz.setDocument(document);
71      gaz.setAnnotationSetName(annotationSetName);
72      gaz.setEncoding(encoding);
73      gaz.setCorpus(corpus);
74      gaz.execute();
75    } // execute
76  
77    /**
78     * Checks the parameters set to this gazetteer
79     * @throws ResourceInstantiationException if something goes wrong
80     */
81    private void checkParameters() throws ResourceInstantiationException {
82      boolean set = null!=gazetteerName;
83      set &= null!=listsURL;
84      set&=null!=mappingURL;
85      if (!set) {
86       throw new ResourceInstantiationException("some parameters are not set (e.g.gazetteerName,"
87          +"listURL,mappingDefinition, document");
88      }
89  
90    } // checkParameters
91  
92    /**
93     * Removes a single string item from the gazetteer model
94     * @param singleItem removes a string item from the gazetteer model
95     * @return true if the string is removed from the model, otherwise - false
96     */
97    public boolean remove(String singleItem) {
98      return gaz.remove(singleItem);
99    }
100 
101   /**
102    * Adds a string item to the model and associates it with a Lookup
103    * @param singleItem the string item to be added
104    * @param lookup the lookup to be associated with the string item
105    * @return true if the item has been added, otherwise - false.
106    */
107   public boolean add(String singleItem, Lookup lookup) {
108     return gaz.add(singleItem,lookup);
109   }
110 
111 } // OntoGazetteerImpl