1   /*
2    *  Relation.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: SynsetImpl.java,v 1.11 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  import gate.util.GateRuntimeException;
26  
27  
28  public class SynsetImpl implements Synset {
29  
30    private ArrayList wordSenses;
31    private ArrayList semRelations;
32    private String gloss;
33    private int synsetPOS;
34    Dictionary wnDictionary;
35    private long synsetOffset;
36  
37    public SynsetImpl(net.didion.jwnl.data.Synset jwSynset, Dictionary _wnDictionary) throws GateRuntimeException {
38  
39      //0.
40      Assert.assertNotNull(jwSynset);
41  
42      //dictionary
43      this.wnDictionary = _wnDictionary;
44  
45      //offset
46      this.synsetOffset = jwSynset.getOffset();
47  
48      //pos
49      this.synsetPOS = WNHelper.POS2int(jwSynset.getPOS());
50  
51      //gloss
52      this.gloss = jwSynset.getGloss();
53  
54      //word senses
55      net.didion.jwnl.data.Word[] synsetWords = jwSynset.getWords();
56      this.wordSenses = new ArrayList(synsetWords.length);
57  
58      for (int i= 0; i< synsetWords.length; i++) {
59  
60        net.didion.jwnl.data.Word jwWord = synsetWords[i];
61        IndexWord jwIndexWord = null;
62  
63        try {
64          jwIndexWord = this.wnDictionary.lookupIndexWord(jwWord.getPOS(),jwWord.getLemma());
65        }
66        catch(JWNLException jwe) {
67          throw new GateRuntimeException(jwe.getMessage());
68        }
69  
70        Word gateWord = new WordImpl(jwWord.getLemma(),
71                                     jwIndexWord.getSenseCount(),
72                                     this.wnDictionary);
73  
74        //construct the proper word form
75        WordSense gateWordSense = null;
76  
77        if (this.synsetPOS == WordNet.POS_ADJECTIVE) {
78  
79          Assert.assertTrue(jwWord instanceof net.didion.jwnl.data.Adjective);
80          net.didion.jwnl.data.Adjective jwAdjective = (net.didion.jwnl.data.Adjective)jwWord;
81  
82          gateWordSense = new AdjectiveImpl(gateWord,
83                                            this,
84                                            0,
85                                            jwWord.getIndex(),
86                                            false,
87                                            WNHelper.AdjPosition2int(jwAdjective),
88                                            this.wnDictionary);
89        }
90  
91        else if (this.synsetPOS == WordNet.POS_VERB) {
92  
93          Assert.assertTrue(jwWord instanceof net.didion.jwnl.data.Verb);
94          net.didion.jwnl.data.Verb jwVerb = (net.didion.jwnl.data.Verb)jwWord;
95  
96          gateWordSense = new VerbImpl(gateWord,
97                                        this,
98                                        0,
99                                        jwWord.getIndex(),
100                                       false,
101                                       jwVerb,
102                                       this.wnDictionary);
103       }
104 
105       else {
106         gateWordSense = new WordSenseImpl(gateWord,
107                                           this,
108                                           0,
109                                           jwWord.getIndex(),
110                                           false,
111                                           this.wnDictionary);
112       }
113 
114       this.wordSenses.add(gateWordSense);
115     }
116 
117   }
118 
119 
120   /** returns the part-of-speech for this synset, see WordNet::POS_XXX constants */
121   public int getPOS(){
122     return this.synsetPOS;
123   }
124 
125   /** is this synset a UB - i.e. has no hypernym */
126   public boolean isUniqueBeginner() throws WordNetException {
127     List parents = getSemanticRelations(Relation.REL_HYPERNYM);
128     return parents.isEmpty();
129   }
130 
131   /** textual description of the synset */
132   public String getGloss(){
133     return this.gloss;
134   }
135 
136 
137   /** WordSenses contained in this synset */
138   public List getWordSenses(){
139     return this.wordSenses;
140   }
141 
142 
143   /** get specific WordSense according to its order in the synset - most important senses come first  */
144   public WordSense getWordSense(int offset){
145     return (WordSense)this.wordSenses.get(offset);
146   }
147 
148 
149   /** get the SemanticRelation-s of this synset */
150   public List getSemanticRelations() throws WordNetException{
151 
152     if (null == this.semRelations) {
153       _loadSemanticRelations();
154     }
155 
156     return this.semRelations;
157   }
158 
159   /** get the SemanticRelation-s of specific type (HYPERNYm) for this synset */
160   public List getSemanticRelations(int type) throws WordNetException{
161 
162     List result = new ArrayList(1);
163 
164     if (null == this.semRelations) {
165       _loadSemanticRelations();
166     }
167 
168     Iterator it = this.semRelations.iterator();
169     while (it.hasNext()) {
170       SemanticRelation sRel = (SemanticRelation)it.next();
171       Assert.assertNotNull(sRel);
172       if (type == sRel.getType()) {
173         result.add(sRel);
174       }
175     }
176 
177     return result;
178   }
179 
180 
181   private void _loadSemanticRelations() throws WordNetException{
182 
183     POS jwPOS = null;
184     jwPOS = WNHelper.int2POS(this.synsetPOS);
185 
186     try {
187       net.didion.jwnl.data.Synset jwSynset = this.wnDictionary.getSynsetAt(jwPOS,this.synsetOffset);
188       Assert.assertNotNull(jwSynset);
189       Pointer[] jwPointers = jwSynset.getPointers();
190 
191       this.semRelations = new ArrayList(jwPointers.length);
192 
193       for (int i= 0; i< jwPointers.length; i++) {
194         Pointer currPointer = jwPointers[i];
195 
196         //skip lexical relations
197         if (true == currPointer.isLexical()) {
198           continue;
199         }
200 
201         PointerType currType = currPointer.getType();
202 //        PointerTarget ptrSource = currPointer.getSource();
203         PointerTarget ptrTarget = currPointer.getTarget();
204         Assert.assertTrue(ptrTarget instanceof net.didion.jwnl.data.Synset);
205         net.didion.jwnl.data.Synset jwTargetSynset = (net.didion.jwnl.data.Synset)ptrTarget;
206 
207         Synset gateTargetSynset = new SynsetImpl(jwTargetSynset,this.wnDictionary);
208         SemanticRelation currSemRel = new SemanticRelationImpl(WNHelper.PointerType2int(currType),
209                                                             this,
210                                                             gateTargetSynset);
211         //add to list of sem relations for this synset
212         this.semRelations.add(currSemRel);
213       }
214     }
215     catch(JWNLException e) {
216       throw new WordNetException(e);
217     }
218   }
219 
220   /** offset in index files */
221   public long getOffset() {
222 
223     return this.synsetOffset;
224   }
225 }