1
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 Assert.assertNotNull(jwSynset);
41
42 this.wnDictionary = _wnDictionary;
44
45 this.synsetOffset = jwSynset.getOffset();
47
48 this.synsetPOS = WNHelper.POS2int(jwSynset.getPOS());
50
51 this.gloss = jwSynset.getGloss();
53
54 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 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
121 public int getPOS(){
122 return this.synsetPOS;
123 }
124
125
126 public boolean isUniqueBeginner() throws WordNetException {
127 List parents = getSemanticRelations(Relation.REL_HYPERNYM);
128 return parents.isEmpty();
129 }
130
131
132 public String getGloss(){
133 return this.gloss;
134 }
135
136
137
138 public List getWordSenses(){
139 return this.wordSenses;
140 }
141
142
143
144 public WordSense getWordSense(int offset){
145 return (WordSense)this.wordSenses.get(offset);
146 }
147
148
149
150 public List getSemanticRelations() throws WordNetException{
151
152 if (null == this.semRelations) {
153 _loadSemanticRelations();
154 }
155
156 return this.semRelations;
157 }
158
159
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 if (true == currPointer.isLexical()) {
198 continue;
199 }
200
201 PointerType currType = currPointer.getType();
202 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 this.semRelations.add(currSemRel);
213 }
214 }
215 catch(JWNLException e) {
216 throw new WordNetException(e);
217 }
218 }
219
220
221 public long getOffset() {
222
223 return this.synsetOffset;
224 }
225 }