1
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
41 private Dictionary wnDictionary;
42
43 private URL propertyUrl;
44
45
46 public IndexFileWordNetImpl() {
47 }
48
49
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 }
71
72
73 public Dictionary getJWNLDictionary() {
74 return this.wnDictionary;
75 }
76
77
78 public void setPropertyUrl(URL _propertiesUrl) {
79
80 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
96 public String getVersion() {
97
98 JWNL.Version ver = JWNL.getVersion();
99 return ver.toString();
100 }
101
102
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
121 public Iterator getUniqueBeginners() {
122 throw new MethodNotImplementedException();
123 }
124
125
129 public void setParent(LanguageResource parentLR)
130 throws PersistenceException,SecurityException {
131
132 throw new UnsupportedOperationException();
133 }
134
135
139 public LanguageResource getParent()
140 throws PersistenceException,SecurityException{
141
142 throw new UnsupportedOperationException();
143 }
144
145
149 public boolean isModified() {
150 return false;
151 }
152
153
156 public void sync() throws PersistenceException,SecurityException {
157 throw new UnsupportedOperationException();
158 }
159
160
163 public void setLRPersistenceId(Object lrID){
164 throw new UnsupportedOperationException();
165 }
166
167
170 public Object getLRPersistenceId(){
171 throw new UnsupportedOperationException();
172 }
173
174
175 public DataStore getDataStore(){
176 throw new UnsupportedOperationException();
177 }
178
179
180 public void setDataStore(DataStore dataStore) throws PersistenceException{
181 throw new UnsupportedOperationException();
182 }
183
184
185
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
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 if (null == jwIndexWord) {
205 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
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 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 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
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 }