JWNL是用JAVA语言编写的用于读取WordNet词典库的组件库。
此次采用JWNL1.4-RC2,最高支持WordNet2.1(WINDOWS),3.0(UNIX)。
1. 配置WordNet词典库。
配置文件位置:..\jwnl14-rc2\config\file_properties.xml中的D:\Programs\WordNet\2.1\dict"/>
2. 在Main函数中使用JWNL加载
1 String propsFile="..\jwnl14-rc2\config\file_properties.xml";2 JWNL.initialize(newFileInputStream(propsFile));
3. 获得某一单词,如dog
1 IndexWord DOG=Dictionary.getInstance().getIndexWord(POS.NOUN,"dog");
4. 获得单词的直接上义词(Hypernym)
使用PointerUtils.getInstance().getDirectHypernyms(Synset
synset)
1 this.demonstrateListOperation(DOG);
具体方法实现:
1 privatevoiddemonstrateListOperation(IndexWord word)throwsJWNLException {2 //Get all of the hypernyms (parents) of the first sense of word3 PointerTargetNodeList hypernyms=PointerUtils.getInstance().getDirectHypernyms(word.getSense(1));4 System.out.println("Direct hypernyms of ""+word.getLemma()+"":");5 for(intidx=0; idx<hypernyms.size(); idx++){6 PointerTargetNode nn=(PointerTargetNode)hypernyms.get(idx);7 for(intwrdIdx=0; wrdIdx<nn.getSynset().getWordsSize(); wrdIdx++){8 System.out.println("Syn"+idx+"of direct hypernyms of""+word.getLemma()+"" :"+9nn.getSynset().getWord(wrdIdx).getLemma());10}11}12hypernyms.print();13 }
5. 获得所有下义词树(Hyponym),包含了其下义词的下义词
5.1 直接使用JWNL的方法打印出,单词DOG所有的下义词
1 this.demonstrateListOperation(ACCOMPLISH);234 privatevoiddemonstrateTreeOperation(IndexWord word)throwsJWNLException {5 //Get all the hyponyms (children) of the first sense of word6 PointerTargetTree hyponyms=PointerUtils.getInstance().getHyponymTree(word.getSense(1));7 System.out.println("Hyponyms of ""+word.getLemma()+"":");8hyponyms.print();9 }
5.2 通过循环获得单词的下义词来遍历DOG的所有下义词
1 BMTreeNode root=newBMTreeNode(DOG.getLemma());2 demonstrateTreeOperation(DOG.getSense(1), root);3System.out.println(root.toString());456 privatevoiddemonstrateTreeOperation(Synset word, BMTreeNode myTreeNode)throwsJWNLException {7 //Get all the hyponyms (children) of the first sense of word8 PointerTargetNodeList hypernyms=PointerUtils.getInstance().getDirectHyponyms(word);9 System.out.println("Direct hypernyms of ""+getAllWordString(w