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