1   /*
2    *  WordNet.java
3    *
4    *  Copyright (c) 1998-2005, 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, June 1991 (in the distribution as file licence.html,
9    *  and also available at http://gate.ac.uk/gate/licence.html).
10   *
11   *  Marin Dimitrov, 16/May/2002
12   *
13   *  $Id: IndexFileWordNetImpl.java,v 1.10 2005/01/11 13:51:38 ian Exp $
14   */
15  
16  package gate.wordnet;
17  
18  import java.io.InputStream;
19  import java.net.URL;
20  import java.util.*;
21  
22  import junit.framework.Assert;
23  import net.didion.jwnl.JWNL;
24  import net.didion.jwnl.JWNLException;
25  import net.didion.jwnl.data.IndexWord;
26  import net.didion.jwnl.dictionary.Dictionary;
27  
28  import gate.*;
29  import gate.creole.AbstractLanguageResource;
30  import gate.creole.ResourceInstantiationException;
31  import gate.persist.PersistenceException;
32  import gate.util.GateRuntimeException;
33  import gate.util.MethodNotImplementedException;
34  
35  
36  public class IndexFileWordNetImpl extends AbstractLanguageResource
37                                    implements WordNet {
38  
39  
40    /** JWNL dictionary */
41    private Dictionary wnDictionary;
42    /** JWNL property file  */
43    private URL       propertyUrl;
44  
45  
46    public IndexFileWordNetImpl() {
47    }
48  
49    /** Initialise this resource, and return it. */
50    public Resource init() throws ResourceInstantiationException {
51  
52      if (null == this.propertyUrl) {
53        throw new ResourceInstantiationException("property file not set");
54      }
55  
56      try {
57  
58        InputStream inProps = this.propertyUrl.openStream();
59  
60        JWNL.initialize(inProps);
61        this.wnDictionary = Dictionary.getInstance();
62        Assert.assertNotNull(this.wnDictionary);
63      }
64      catch(Exception e) {
65        throw new ResourceInstantiationException(e);
66      }
67  
68      return this;
69    } // init()
70  
71  
72    /** helper method */
73    public Dictionary getJWNLDictionary() {
74      return this.wnDictionary;
75    }
76  
77  
78    public void setPropertyUrl(URL _propertiesUrl) {
79  
80      //0.
81      Assert.assertNotNull(_propertiesUrl);
82  
83      if (null != this.propertyUrl) {
84        throw new GateRuntimeException("props are alredy set");
85      }
86  
87      this.propertyUrl = _propertiesUrl;
88    }
89  
90    public URL getPropertyUrl() {
91      return this.propertyUrl;
92    }
93  
94  
95    /** returns the WordNet version */
96    public String getVersion() {
97  
98      JWNL.Version ver = JWNL.getVersion();
99      return ver.toString();
100   }
101 
102   /** returns all synsets for specific POS */
103   public Iterator getSynsets(int _pos)
104     throws WordNetException {
105 
106     net.didion.jwnl.data.POS pos = WNHelper.int2POS(_pos);
107 
108     try {
109       net.didion.jwnl.data.Synset jwnSynset = null;
110 
111       Iterator itSynsets = this.wnDictionary.getSynsetIterator(pos);
112       return new SynsetIterator(itSynsets);
113     }
114     catch(JWNLException jwne) {
115       throw new WordNetException(jwne);
116     }
117 
118   }
119 
120   /** returns all unique beginners */
121   public Iterator getUniqueBeginners() {
122     throw new MethodNotImplementedException();
123   }
124 
125   /**
126    * Sets the parent LR of this LR.
127    * Only relevant for LRs that support shadowing. Most do not by default.
128    */
129   public void setParent(LanguageResource parentLR)
130     throws PersistenceException,SecurityException {
131 
132     throw new UnsupportedOperationException();
133   }
134 
135   /**
136    * Returns the parent LR of this LR.
137    * Only relevant for LRs that support shadowing. Most do not by default.
138    */
139   public LanguageResource getParent()
140     throws PersistenceException,SecurityException{
141 
142     throw new UnsupportedOperationException();
143   }
144 
145   /**
146    * Returns true of an LR has been modified since the last sync.
147    * Always returns false for transient LRs.
148    */
149   public boolean isModified() {
150     return false;
151   }
152 
153   /** Save: synchonise the in-memory image of the LR with the persistent
154     * image.
155     */
156   public void sync() throws PersistenceException,SecurityException {
157     throw new UnsupportedOperationException();
158   }
159 
160   /** Sets the persistence id of this LR. To be used only in the
161    *  Factory and DataStore code.
162    */
163   public void setLRPersistenceId(Object lrID){
164     throw new UnsupportedOperationException();
165   }
166 
167    /** Returns the persistence id of this LR, if it has been stored in
168    *  a datastore. Null otherwise.
169    */
170   public Object getLRPersistenceId(){
171     throw new UnsupportedOperationException();
172   }
173 
174   /** Get the data store that this LR lives in. Null for transient LRs. */
175   public DataStore getDataStore(){
176     throw new UnsupportedOperationException();
177   }
178 
179    /** Set the data store that this LR lives in. */
180   public void setDataStore(DataStore dataStore) throws PersistenceException{
181     throw new UnsupportedOperationException();
182   }
183 
184 
185   /** returns list of WordSense-s for specific lemma */
186   public List lookupWord(String lemma) throws WordNetException {
187 
188     try {
189       IndexWord[] jwIndexWordArr = this.wnDictionary.lookupAllIndexWords(lemma).getIndexWordArray();
190       return _lookupWord(lemma,jwIndexWordArr);
191     }
192     catch(JWNLException jex) {
193       throw new WordNetException(jex);
194     }
195   }
196 
197   /** returns list of WordSense-s for specific lemma of the specified POS */
198   public List lookupWord(String lemma, int pos) throws WordNetException {
199 
200     try {
201       IndexWord jwIndexWord = this.wnDictionary.lookupIndexWord(WNHelper.int2POS(pos), lemma);
202 
203       //do we have a word with this POS?
204       if (null == jwIndexWord) {
205         //return dummy
206         return new ArrayList();
207       }
208 
209       IndexWord[] jwIndexWordArr = new IndexWord[1];
210       jwIndexWordArr[0] = jwIndexWord;
211 
212       return _lookupWord(lemma,jwIndexWordArr);
213     }
214     catch(JWNLException jex) {
215       throw new WordNetException(jex);
216     }
217   }
218 
219   /** helper method */
220   private List _lookupWord(String lemma, IndexWord[] jwIndexWords) throws WordNetException{
221 
222     List result = new ArrayList();
223 
224     try {
225       for (int i=0; i< jwIndexWords.length; i++) {
226         IndexWord iw = jwIndexWords[i];
227         net.didion.jwnl.data.Synset[] jwSynsetArr = iw.getSenses();
228 
229         for (int j=0; j< jwSynsetArr.length; j++) {
230           net.didion.jwnl.data.Synset jwSynset = jwSynsetArr[j];
231           Synset gateSynset = new SynsetImpl(jwSynset,this.wnDictionary);
232           //find the word of interest
233           List wordSenses = gateSynset.getWordSenses();
234 
235           Iterator itSenses = wordSenses.iterator();
236           while (itSenses.hasNext()) {
237             WordSense currSynsetMember = (WordSense)itSenses.next();
238             if (currSynsetMember.getWord().getLemma().equalsIgnoreCase(lemma)) {
239               //found match
240               result.add(currSynsetMember);
241               break;
242             }
243           }
244         }
245       }
246     }
247     catch(JWNLException jex) {
248       throw new WordNetException(jex);
249     }
250 
251     return result;
252   }
253 
254   /** iterator for synsets - may load synsets when necessary, not all at once */
255   class SynsetIterator implements java.util.Iterator {
256 
257     private Iterator it;
258 
259     public SynsetIterator(Iterator _it) {
260 
261       Assert.assertNotNull(_it);
262       this.it = _it;
263     }
264 
265     public boolean hasNext() {
266       return this.it.hasNext();
267     }
268 
269     public void remove() {
270       throw new UnsupportedOperationException();
271     }
272 
273     public Object next() {
274 
275       net.didion.jwnl.data.Synset jwnlSynset = (net.didion.jwnl.data.Synset)this.it.next();
276       Synset gateSynset = new SynsetImpl(jwnlSynset, wnDictionary);
277       return gateSynset;
278     }
279   }
280 }