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