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