1   /*
2    *  WordImpl.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, 17/May/2002
12   *
13   *  $Id: WordSenseImpl.java,v 1.4 2002/05/30 09:54:17 marin Exp $
14   */
15  
16  package gate.wordnet;
17  
18  import java.util.*;
19  
20  import junit.framework.*;
21  import net.didion.jwnl.dictionary.Dictionary;
22  import net.didion.jwnl.data.POS;
23  import net.didion.jwnl.data.Pointer;
24  import net.didion.jwnl.data.PointerType;
25  import net.didion.jwnl.data.PointerTarget;
26  import net.didion.jwnl.data.IndexWord;
27  import net.didion.jwnl.JWNLException;
28  
29  import gate.util.*;
30  
31  
32  public class WordSenseImpl implements WordSense {
33  
34    private Word word;
35    private Synset  synset;
36    private int senseNumber;
37    private int orderInSynset;
38    private boolean isSemcor;
39    private List lexRelations;
40    private Dictionary wnDictionary;
41  
42    public WordSenseImpl(Word _word,
43                        Synset _synset,
44                        int _senseNumber,
45                        int _orderInSynset,
46                        boolean _isSemcor,
47                        Dictionary _wnDict) {
48  
49      //0.
50      Assert.assertNotNull(_word);
51      Assert.assertNotNull(_synset);
52      Assert.assertNotNull(_wnDict);
53  
54      this.word = _word;
55      this.synset = _synset;
56      this.senseNumber = _senseNumber;
57      this.orderInSynset = _orderInSynset;
58      this.isSemcor = _isSemcor;
59      this.wnDictionary = _wnDict;
60    }
61  
62    /** returns the Word of this WordSense */
63    public Word getWord() {
64      return this.word;
65    }
66  
67    /** part-of-speech for this sense (inherited from the containing synset) */
68    public int getPOS() {
69      return this.synset.getPOS();
70    }
71  
72    /** synset of this sense */
73    public Synset getSynset() {
74      return this.synset;
75    }
76  
77    /** order of this sense relative to the word - i.e. most important senses of the same word come first */
78    public int getSenseNumber() {
79      return this.senseNumber;
80    }
81  
82    /** order of this sense relative to the synset- i.e. most important senses of the same synset come first */
83    public int getOrderInSynset() {
84      return this.orderInSynset;
85    }
86  
87  
88    /** appears in SEMCOR? */
89    public boolean isSemcor() {
90      return this.isSemcor;
91    }
92  
93  
94    /** return the Lex relations this sense participates in */
95    public List getLexicalRelations() throws WordNetException {
96  
97      if (null == this.lexRelations) {
98        _loadLexicalRelations();
99      }
100 
101     return this.lexRelations;
102   }
103 
104 
105   /** return the Lex relations (of the specified type) this sense participates in */
106   public List getLexicalRelations(int type) throws WordNetException {
107 
108     List result = new ArrayList(1);
109 
110     if (null == this.lexRelations) {
111       _loadLexicalRelations();
112     }
113 
114     Iterator it = this.lexRelations.iterator();
115     while (it.hasNext()) {
116       LexicalRelation lRel = (LexicalRelation)it.next();
117       Assert.assertNotNull(lRel);
118       if (type == lRel.getType()) {
119         result.add(lRel);
120       }
121     }
122 
123     return result;
124   }
125 
126 
127   private void _loadLexicalRelations() throws WordNetException{
128 
129     POS jwPOS = null;
130     jwPOS = WNHelper.int2POS(this.getPOS());
131 
132     try {
133       net.didion.jwnl.data.Synset jwSynset = this.wnDictionary.getSynsetAt(jwPOS,this.synset.getOffset());
134       Assert.assertNotNull(jwSynset);
135 
136       Pointer[] jwPointers = null;
137 
138       net.didion.jwnl.data.Word[] jwWords = jwSynset.getWords();
139       for (int i=0; i< jwWords.length; i++) {
140         net.didion.jwnl.data.Word currJwWord = jwWords[i];
141         if (currJwWord.getLemma().equalsIgnoreCase(this.getWord().getLemma())) {
142           jwPointers = currJwWord.getPointers();
143           break;
144         }
145       }
146 
147       this.lexRelations = new ArrayList(jwPointers.length);
148 
149       for (int i= 0; i< jwPointers.length; i++) {
150 
151         Pointer currPointer = jwPointers[i];
152         //skip semantic relations
153         if (false == currPointer.isLexical()) {
154           continue;
155         }
156 
157         PointerType currType = currPointer.getType();
158 //        PointerTarget ptrSource = currPointer.getSource();
159         PointerTarget ptrTarget = currPointer.getTarget();
160         Assert.assertTrue(ptrTarget instanceof net.didion.jwnl.data.Word);
161 
162         net.didion.jwnl.data.Word jwTargetWord = (net.didion.jwnl.data.Word)ptrTarget;
163         net.didion.jwnl.data.Synset jwTargetSynset = jwTargetWord.getSynset();
164         IndexWord jwTargetIndexWord = this.wnDictionary.lookupIndexWord(jwTargetWord.getPOS(),
165                                                                       jwTargetWord.getLemma());
166 
167         Synset gateSynset = new SynsetImpl(jwTargetSynset,this.wnDictionary);
168 
169         Word gateWord = new WordImpl(jwTargetWord.getLemma(),
170                                       jwTargetIndexWord.getSenseCount(),
171                                       this.wnDictionary);
172 
173         WordSense gateTargetWordSense = new WordSenseImpl(gateWord,
174                                                           gateSynset,
175                                                           0,
176                                                           jwTargetWord.getIndex(),
177                                                           false,
178                                                           this.wnDictionary);
179 
180         LexicalRelation gateLexRel = new LexicalRelationImpl(WNHelper.PointerType2int(currType),
181                                                             this,
182                                                             gateTargetWordSense);
183         //add to list of sem relations for this synset
184         this.lexRelations.add(gateLexRel);
185       }
186     }
187     catch(JWNLException e) {
188       throw new WordNetException(e);
189     }
190   }
191 }