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