1   /*
2    *  TestWordnet.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/02
12   *
13   *  $Id: TestWordNet.java,v 1.13 2005/01/12 15:29:58 valyt Exp $
14   */
15  
16  package gate.wordnet;
17  
18  import java.io.File;
19  import java.util.Iterator;
20  import java.util.List;
21  
22  import junit.framework.*;
23  
24  import gate.Gate;
25  import gate.GateConstants;
26  import gate.util.Err;
27  
28  public class TestWordNet extends TestCase {
29  
30    private static IndexFileWordNetImpl wnMain = null;
31  
32    public TestWordNet(String dummy) {
33      super(dummy);
34    }
35  
36    public static void main(String[] args) {
37      TestWordNet testWordNet1 = new TestWordNet("");
38  
39      try {
40  
41        testWordNet1.setUp();
42  
43        testWordNet1.testWN_01();
44  
45        testWordNet1.testWN_02();
46  
47        testWordNet1.testWN_03();
48  
49      }
50      catch(Exception ex) {
51        ex.printStackTrace();
52      }
53    }
54  
55    public void testWN_01() throws Exception {
56      //test the presence of the WN files
57      String wnConfigFile = (String)Gate.getUserConfig().
58                            get(GateConstants.WORDNET_CONFIG_FILE);
59      if(wnConfigFile == null){
60        Err.prln("WordNet not present. Test aborted...");
61        return;
62      }
63      //test synset access - read all senses for a word and compare them with the entries from the
64      //WN16 index files
65  
66      //get all synsets for "cup"
67      List senseList = wnMain.lookupWord("cup",WordNet.POS_NOUN);
68      Assert.assertTrue(senseList.size() == 8);
69  
70      Iterator itSenses = senseList.iterator();
71  
72      for (int i=0; i< senseList.size(); i++) {
73  
74        WordSense currSense = (WordSense)senseList.get(i);
75        Synset currSynset = currSense.getSynset();
76        Assert.assertNotNull(currSynset);
77  
78        switch(i+1) {
79  
80          case 1:
81            checkSynset(currSynset,
82                        "a small open container usually used for drinking; \"he put the cup back in the saucer\"; \"the handle of the cup was missing\"",
83                        1);
84            break;
85  
86          case 2:
87            checkSynset(currSynset,
88                        "the quantity a cup will hold; \"he drank a cup of coffee\"; \"he borrowed a cup of sugar\"",
89                        2);
90            break;
91  
92          case 3:
93            checkSynset(currSynset,
94                        "any cup-shaped concavity; \"bees filled the waxen cups with honey\"; \"he wore a jock strap with a metal cup\"; \"the cup of her bra\"",
95                        1);
96            break;
97  
98          case 4:
99            checkSynset(currSynset,
100                       "a United States liquid unit equal to 8 fluid ounces",
101                       1);
102           break;
103 
104         case 5:
105           checkSynset(currSynset,
106                       "cup-shaped plant organ",
107                       1);
108           break;
109 
110         case 6:
111           checkSynset(currSynset,
112                       "punch served in a pitcher instead of a punch bowl",
113                       1);
114           break;
115 
116         case 7:
117           checkSynset(currSynset,
118                       "the hole (or metal container in the hole) on a golf green; \"he swore as the ball rimmed the cup and rolled away\"; \"put the flag back in the cup\"",
119                       1);
120           break;
121 
122         case 8:
123           checkSynset(currSynset,
124                       "a large metal vessel with two handles that is awarded to the winner of a competition; \"the school kept the cups is a special glass case\"",
125                       2);
126           break;
127       }
128     }
129   }
130 
131 
132   public void testWN_02() throws Exception {
133     //test the presence of the WN files
134     String wnConfigFile = (String)Gate.getUserConfig().
135                           get(GateConstants.WORDNET_CONFIG_FILE);
136     if(wnConfigFile == null){
137       Err.prln("WordNet not present. Test aborted...");
138       return;
139     }
140 
141     //test hypernymy - traverse upwards the hierarchy starting from some word
142     //compare the result with the WN16 index files
143     //get all synsets for "cup"
144 
145     List senseList = wnMain.lookupWord("cup",WordNet.POS_NOUN);
146     Assert.assertTrue(senseList.size() == 8);
147 
148     Iterator itSenses = senseList.iterator();
149 
150     for (int i=0; i< senseList.size(); i++) {
151 
152       WordSense currSense = (WordSense)senseList.get(i);
153       Synset currSynset = currSense.getSynset();
154       Assert.assertNotNull(currSynset);
155 
156       if (false == currSynset.getGloss().equals("a small open container usually used for drinking; \"he put the cup back in the saucer\"; \"the handle of the cup was missing\"")) {
157         continue;
158       }
159 
160       List semRelations = currSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM);
161       Assert.assertNotNull(semRelations);
162       Assert.assertTrue(2 == semRelations.size());
163 
164       for (int j=0; j< semRelations.size(); j++ ) {
165 
166         SemanticRelation currHyperRel = (SemanticRelation)semRelations.get(j);
167 
168         Assert.assertTrue(currHyperRel.getType() == SemanticRelation.REL_HYPERNYM);
169         Assert.assertEquals(currHyperRel.getSymbol(),"@");
170         Assert.assertEquals(currHyperRel.getSource(), currSynset);
171 
172         Synset currHypernym = currHyperRel.getTarget();
173         Assert.assertNotNull(currHypernym);
174 
175         Synset hyperSynset = currHypernym;
176         if (currHypernym.getGloss().equals("eating and serving dishes collectively")) {
177 
178           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
179           Assert.assertEquals(hyperSynset.getGloss(),"articles for use at the table");
180 
181           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
182           Assert.assertEquals(hyperSynset.getGloss(),"articles of the same kind or material; usually used in combination: silverware; software");
183 
184           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
185           Assert.assertEquals(hyperSynset.getGloss(),"one of a class of artifacts; \"an article of clothing\"");
186 
187           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
188           Assert.assertEquals(hyperSynset.getGloss(),"a man-made object");
189 
190           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
191           Assert.assertEquals(hyperSynset.getGloss(),"a physical (tangible and visible) entity; \"it was full of rackets, balls and other objects\"");
192 
193           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
194           Assert.assertEquals(hyperSynset.getGloss(),"anything having existence (living or nonliving)");
195         }
196         else if (currHypernym.getGloss().equals("something that holds things, especially for transport or storage")) {
197 
198           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
199           Assert.assertEquals(hyperSynset.getGloss(),"an artifact (or system of artifacts) that is instrumental in accomplishing some end");
200 
201           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
202           Assert.assertEquals(hyperSynset.getGloss(),"a man-made object");
203 
204           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
205           Assert.assertEquals(hyperSynset.getGloss(),"a physical (tangible and visible) entity; \"it was full of rackets, balls and other objects\"");
206 
207           hyperSynset = ((SemanticRelation)hyperSynset.getSemanticRelations(SemanticRelation.REL_HYPERNYM).get(0)).getTarget();
208           Assert.assertEquals(hyperSynset.getGloss(),"anything having existence (living or nonliving)");
209         }
210         else {
211           Assert.fail();
212         }
213       }
214 
215       break;
216     }
217 
218   }
219 
220   public void testWN_03() throws Exception {
221     //test the presence of the WN files
222     String wnConfigFile = (String)Gate.getUserConfig().
223                           get(GateConstants.WORDNET_CONFIG_FILE);
224     if(wnConfigFile == null){
225       Err.prln("WordNet not present. Test aborted...");
226       return;
227     }
228 
229     //test hyponymy - check all direct hyponyms of a word
230     //compare the result with the WN16 index files
231 
232     List senseList = wnMain.lookupWord("cup",WordNet.POS_NOUN);
233     Assert.assertTrue(senseList.size() == 8);
234 
235     Iterator itSenses = senseList.iterator();
236 
237     for (int i=0; i< senseList.size(); i++) {
238 
239       WordSense currSense = (WordSense)senseList.get(i);
240       Synset currSynset = currSense.getSynset();
241       Assert.assertNotNull(currSynset);
242 
243       if (false == currSynset.getGloss().equals("a small open container usually used for drinking; \"he put the cup back in the saucer\"; \"the handle of the cup was missing\"")) {
244         continue;
245       }
246 
247       List semRelations = currSynset.getSemanticRelations(SemanticRelation.REL_HYPONYM);
248       Assert.assertNotNull(semRelations);
249       Assert.assertTrue(9 == semRelations.size());
250 
251       for (int j=0; j< semRelations.size(); j++ ) {
252         SemanticRelation currHypoRel = (SemanticRelation)semRelations.get(j);
253 
254         Assert.assertTrue(currHypoRel.getType() == SemanticRelation.REL_HYPONYM);
255         Assert.assertEquals(currHypoRel.getSymbol(),"~");
256         Assert.assertEquals(currHypoRel.getSource(), currSynset);
257 
258         Synset currHyponym = currHypoRel.getTarget();
259         Assert.assertNotNull(currHyponym);
260 
261         switch(j) {
262 
263           case 0:
264             checkSynset(currHyponym,
265                         "usually without a handle",
266                         1);
267             break;
268 
269           case 1:
270             checkSynset(currHyponym,
271                         "a bowl-shaped drinking vessel; especially the Eucharistic cup",
272                         2);
273             break;
274 
275           case 2:
276             checkSynset(currHyponym,
277                         "a cup from which coffee is drunk",
278                         1);
279             break;
280 
281           case 3:
282             checkSynset(currHyponym,
283                         "a paper cup for holding drinks",
284                         3);
285             break;
286 
287           case 4:
288             checkSynset(currHyponym,
289                         "cup to be passed around for the final toast after a meal",
290                         1);
291             break;
292 
293           case 5:
294             checkSynset(currHyponym,
295                         "a graduated cup used for measuring ingredients",
296                         1);
297             break;
298 
299           case 6:
300             checkSynset(currHyponym,
301                         "a drinking cup with a bar inside the rim to keep a man's mustache out of the drink",
302                         2);
303             break;
304 
305           case 7:
306             checkSynset(currHyponym,
307                         "an ancient Greek drinking cup; two handles and footed base",
308                         1);
309             break;
310 
311           case 8:
312             checkSynset(currHyponym,
313                         "a cup from which tea is drunk",
314                         1);
315             break;
316         }
317       }
318 
319     }
320   }
321 
322   private void checkSynset(Synset s, String gloss, int numWords) {
323 
324     Assert.assertEquals(s.getGloss(),gloss);
325 
326     List wordSenses = s.getWordSenses();
327     Assert.assertTrue(wordSenses.size() == numWords);
328   }
329 
330 /*
331   public void testWN_01() throws Exception {
332 
333     IndexFileWordNetImpl wnMain = new IndexFileWordNetImpl();
334     wnMain.setPropertyFile(new File("D:/PRJ/jwnl/file_properties.xml"));
335     wnMain.init();
336 
337     Dictionary dict = wnMain.getJWNLDictionary();
338     Assert.assertNotNull(dict);
339 
340     IndexWordSet iSet = dict.lookupAllIndexWords("cup");
341     IndexWord[] arr =  iSet.getIndexWordArray();
342     for (int i=0; i< arr.length; i++) {
343       IndexWord iw = arr[i];
344       net.didion.jwnl.data.Synset[] synsets = iw.getSenses();
345       for (int j=0; j< synsets.length; j++) {
346         net.didion.jwnl.data.Synset s = synsets[j];
347 //System.out.println("synset: "+s.toString());
348 //net.didion.jwnl.data.Word firstWord = s.getWord(0);
349 //System.out.println("0th word index is " + firstWord.getIndex());
350         Synset ss = new SynsetImpl(s,wnMain.getJWNLDictionary());
351         List rel = ss.getSemanticRelations();
352       }
353     }
354 
355 
356 System.out.println(iSet.size());
357 System.out.println(iSet);
358   }
359 */
360 
361   /** Test suite routine for the test runner */
362   public static Test suite() {
363     return new TestSuite(TestWordNet.class);
364   } // suite
365 
366 
367   protected void setUp() throws Exception {
368 
369     String wnConfigFile = (String)Gate.getUserConfig().
370                           get(GateConstants.WORDNET_CONFIG_FILE);
371     if(wnConfigFile == null) return;
372     if (null == wnMain) {
373       wnMain = new IndexFileWordNetImpl();
374       wnMain.setPropertyUrl(new File(wnConfigFile).toURL());
375       wnMain.init();
376     }
377   }
378 
379 }