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
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 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
57 public Word getWord() {
58 return this.word;
59 }
60
61
62 public int getPOS() {
63 return this.synset.getPOS();
64 }
65
66
67 public Synset getSynset() {
68 return this.synset;
69 }
70
71
72 public int getSenseNumber() {
73 return this.senseNumber;
74 }
75
76
77 public int getOrderInSynset() {
78 return this.orderInSynset;
79 }
80
81
82
83 public boolean isSemcor() {
84 return this.isSemcor;
85 }
86
87
88
89 public List getLexicalRelations() throws WordNetException {
90
91 if (null == this.lexRelations) {
92 _loadLexicalRelations();
93 }
94
95 return this.lexRelations;
96 }
97
98
99
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 if (false == currPointer.isLexical()) {
148 continue;
149 }
150
151 PointerType currType = currPointer.getType();
152 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 this.lexRelations.add(gateLexRel);
179 }
180 }
181 catch(JWNLException e) {
182 throw new WordNetException(e);
183 }
184 }
185 }