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