WordNet词网研究6——之JWI(Java Wordnet Interface)WordNet Java接口

2023-05-16

 JWI (the MIT Java Wordnet Interface) is a Java library for interfacing with Wordnet. JWI supports access to Wordnet versions 1.6 through 3.0, among other related Wordnet extensions. Wordnet is a freely and publicly available semantic dictionary of English, developed at Princeton University.

JWI is written for Java 1.5.0 and has the package namespace edu.mit.jwi. The distribution does not include the Wordnet dictionary files; these can be downloaded from the Wordnet download site. This version of software is distributed under a license that makes it free to use for all purposes, as long as proper copyright acknowledgement is made.

The javadoc API is posted online for your convenience. So is the version changelog. If you find JWI useful, have found a bug, or would like to request a new feature, please contact me.

DescriptionLinkBinary Files Onlyedu.mit.jwi_2.2.2.jar (143 kb)User's Manualedu.mit.jwi_2.2.2_manual.pdf (276 kb)Source Onlyedu.mit.jwi_2.2.2_src.zip (143 kb)Javadocsedu.mit.jwi_2.2.2_javadoc.zip (617 kb) | onlineDevelopment Kit (binaries and source)edu.mit.jwi_2.2.2_jdk.jar (273 kb)All-in-One (jdk, javadocs, manual)edu.mit.jwi_2.2.2_all.zip (1,090 kb)

 From:http://projects.csail.mit.edu/jwi/

JWI是由MIT麻省理工学院,计算机科学与人工智能实验室, Mark Alan.Finlayson主持的项目。JWI是用于访问WordNet的Java API。

 

一、WordNet与JWI使用实例:

 

1.先安装WordNet

        安装过程略,设置环境变量WNHOME,指向WordNet的安装根目录。例如:

      WNHOME = “E:\Commonly Application\WordNet\2.1”;

2.下载JWI

     到http://projects.csail.mit.edu/jwi/下载JWI,使用其jdk来进行测试、开发等。

3.测试

    写一个测试类:

 

import java.io.File;

import java.io.IOException;

importjava.net.MalformedURLException;

import java.net.URL;

 

import edu.mit.jwi.Dictionary;

import edu.mit.jwi.IDictionary;

import edu.mit.jwi.item.IIndexWord;

import edu.mit.jwi.item.IWord;

import edu.mit.jwi.item.IWordID;

import edu.mit.jwi.item.POS;

 

 

public class test {

 

    public static void main(String[] args) throws IOException{

       testDitctionary();

    }

    

    public static void testDitctionary() throws IOException{

       // construct the URL to the Wordnet dictionary directory

       String wnhome = System.getenv("WNHOME"); //获取环境变量WNHOME

       String path = wnhome + File.separator+ "dict";

       URL url=new URL("file", null, path);  //创建一个URL对象,指向WordNet的ditc目录

       

       // construct the dictionary object and open it

       IDictionary dict=new Dictionary(url);

       dict.open(); //打开词典

       

       // look up first sense of the word "dog "

       IIndexWord idxWord=dict.getIndexWord("dog", POS.NOUN);//获取一个索引词,(dog,名词)

       IWordID wordID=idxWord.getWordIDs().get(0);//获取dog第一个词义ID

       IWord word = dict.getWord(wordID); //获取该词

       System .out . println ("Id = " + wordID );

       System .out . println (" 词元 = " + word . getLemma ());

       System .out . println (" 注解 = " + word . getSynset (). getGloss ());

    }

}

 

执行得到的结果如下:

 

Id = WID-02064081-N-??-dog

 词元 = dog

 注解 = a member of the genus Canis (probably descended from the common wolf) that has been domesticated by man since prehistoric times; occurs in many breeds; "the dog barked all night"

 

这是一个初步是使用JWI的例子。

 

二、JWI装载WordNet到内存的遍历性能优化:

 

JWI2.2.x系列一个新的特点是可以将WordNet装载到内存中,这一举措大大的改善了遍历WordNet的性能。其中实现该功能的是,JWI的edu.mit.jwi.RAMDictionary类,该类可以设定是否将WordNet装入内存。

 

写一个遍历WordNet的函数trek(),对使用RAMDictionary来打开WordNet,对不装载入内存和装载入内存进行比较;

 importjava.io.File;

importjava.io.IOException;

importjava.util.Iterator;

 

importedu.mit.jwi.IDictionary;

importedu.mit.jwi.IRAMDictionary;

importedu.mit.jwi.RAMDictionary;

importedu.mit.jwi.data.ILoadPolicy;

importedu.mit.jwi.item.IIndexWord;

importedu.mit.jwi.item.IWordID;

importedu.mit.jwi.item.POS;

 

public classRAMDictionaryTest {

     public static voidmain(String[] args) throwsIOException, Exception{

      String wnhome = System.getenv("WNHOME"); //获取环境变量WNHOM 

       String path = wnhome + File.separator+ "dict"

       File wnDir=newFile(path);

       testRAMDictionary(wnDir);

    }

    public static voidtestRAMDictionary(File wnDir)throwsIOException, InterruptedException{

       IRAMDictionary dict=newRAMDictionary(wnDir, ILoadPolicy.NO_LOAD);

       dict.open();

       //周游WordNet

       System.out.print("没装载前:\n");

       trek(dict);

       //now load into memor

       System.out.print("\nLoading Wordnet into memory...");

       longt=System.currentTimeMillis();

       dict.load(true);

       System.out.printf("装载时间:done(%1d msec)\n", System.currentTimeMillis()-t);

 

       //装载后在周游

       System.out.print("\n装载后:\n");

       trek(dict);

    }

 

    /*

     * this method is Achieved to trek around the WordNet

     */

    public static voidtrek(IDictionary dict){

       inttickNext=0;

       inttickSize=20000;

       intseen=0;

       System.out.print("Treking across Wordnet");

       longt=System.currentTimeMillis();

       for(POS pos:POS.values()){ //遍历所有词性

           for(Iterator<IIndexWord> i=dict.getIndexWordIterator(pos);i.hasNext();){

              //遍历某一个词性的所有索引 

              for(IWordID wid:i.next().getWordIDs()){

                  //遍历每一个词的所有义项

                  seen+=dict.getWord(wid).getSynset().getWords().size();//获取某一个synsets所具有的词

                  if(seen>tickNext){

                     System.out.print(".");

                     tickNext=seen + tickSize;

                  }

              }

           }

       }

 

       System.out.printf("done (%1d msec)\n",System.currentTimeMillis()-t);

       System.out.println("In my trek I saw "+ seen + " words");

    }

}

执行的效果为:

 没装载前

Treking across Wordnet...........................done (3765 msec)

In my trek I saw 523260 words 

Loading Wordnet into memory...装载时间:done(10625 msec)

装载后:

Treking across Wordnet...........................done (328 msec)

In my trek I saw 523260 words

 

      由结果可见,不装如内存的周游WordNet的时间为3765 ms,而装入内存后的周游时间为328 ms,结果中的10625ms把装入内存所消耗的时间。就周游的时间而言转入内存后的时间更快速。

 

三、JWI获取同义词以及抛出NullPointerException原因解析

 

获取一个词的Synset内的同义词,一下是一个示例:获取“go”的同义词。

import java.io.File;

import java.io.IOException;

import java.net.URL;

 

import edu.mit.jwi.Dictionary;

import edu.mit.jwi.IDictionary;

import edu.mit.jwi.item.IIndexWord;

import edu.mit.jwi.item.ISynset;

import edu.mit.jwi.item.IWord;

import edu.mit.jwi.item.IWordID;

import edu.mit.jwi.item.POS;

 

public class GetWordSynsetsTest {

    public static void main(String[] args) throws IOException{

       String wnhome = System.getenv("WNHOME"); //获取WordNet根目录环境变量WNHOME

       String path = wnhome + File.separator+ "dict";       

       File wnDir=new File(path);

       URL url=new URL("file", null, path);

       IDictionary dict=new Dictionary(url);

       dict.open();//打开词典

       getSynonyms(dict); //testing

    }

 

    public static void getSynonyms(IDictionary dict){

       // look up first sense of the word "go"

       IIndexWord idxWord =dict.getIndexWord("go", POS.VERB);

       IWordID wordID = idxWord.getWordIDs().get(0) ; // 1st meaning

       IWord word = dict.getWord(wordID);

       ISynset synset = word.getSynset (); //ISynset是一个词的同义词集的接口

 

       // iterate over words associated with the synset

       for(IWord w : synset.getWords())

         System.out.println(w.getLemma());//打印同义词集中的每个同义词

    }

}

执行的结果为:

travel

go

move

locomote

 

    在获取一个IndexWord时,很容易抛出一个运行异常,即NullPointerException。这是因为在WordNet里找不着你想要的词。但是这个词在实际英语环境中是存在的。可能的原因如下:

1.所输入的词形式不对,比如:go写成了gone,或者dog写成dogs等非源词形式了。

2.可能是你在构造词时,在如getIndexWord("go", POS.VERB)函数中词性参数输入错误,比如上例中输入的词性是POS.ADVERB。由于go没有副词,所以汇报NullPointerException异常。

3.一些新的单词根本还没录入WordNet。

 

四、JWI寻找父类词

 

上义词Hypernyms,即俗称的父类,再次称为父类词。查找一个词的父类词,则使用JWI提供的ISynset对象的getRelatedSynsets(IPointerType)方法来寻找,其中IPointerType为Synsets之间的关系指针,通过设置其为Pointer.HYPERNYM则可以实现寻找该词的父类。

实例:

import java.io.File;

import java.io.IOException;

import java.util.Iterator;

import java.util.List;

 

import edu.mit.jwi.Dictionary;

import edu.mit.jwi.IDictionary;

import edu.mit.jwi.item.IIndexWord;

import edu.mit.jwi.item.ISynset;

import edu.mit.jwi.item.ISynsetID;

import edu.mit.jwi.item.IWord;

 import edu.mit.jwi.item.IWordID;

import edu.mit.jwi.item.POS;

import edu.mit.jwi.item.Pointer;

 

public class GetHypernymsTest {

    public static void main(String[] args) throws IOException{

       String wnhome = System.getenv("WNHOME"); //获取WordNet根目录环境变量WNHOME

       String path = wnhome + File.separator+ "dict";       

       File wnDir=new File(path);

       IDictionary dict=new Dictionary(wnDir);

       dict.open();//打开词典

       getHypernyms(dict);//testing

    }

    public static void getHypernyms(IDictionary dict){

 

       //获取指定的synset

       IIndexWord idxWord = dict.getIndexWord("article", POS.NOUN);//获取dog的IndexWord

       IWordID wordID = idxWord.getWordIDs().get(0); //取出第一个词义的词的ID号

       IWord word = dict.getWord(wordID); //获取词

       ISynset synset = word.getSynset(); //获取该词所在的Synset

 

       // 获取hypernyms

       List<ISynsetID> hypernyms =synset.getRelatedSynsets(Pointer.HYPERNYM );//通过指针类型来获取相关的词集,其中Pointer类型为HYPERNYM

       // print out each hypernyms id and synonyms

       List <IWord > words ;

       for( ISynsetID sid : hypernyms ){

           words = dict.getSynset(sid).getWords(); //从synset中获取一个Word的list

           System.out.print(sid + "{");

           for( Iterator<IWord > i = words.iterator(); i.hasNext();){

              System.out.print(i.next().getLemma ());

              if(i. hasNext ()){

                  System.out.print(", ");

              }

           }

           System .out . println ("}");

       }

    }

}

 

执行结果为:

SID-06282025-N{nonfiction, nonfictional_prose}

SID-06186871-N{piece}

从结果看出article有两个父类,第一个为文学、小说、文章等,第二个指片段、一张、片等。

 

五、JWI的词典指针和语义指针

 

为什么要说明这个问题呢?

 

    是因为有时候明明知道在WordNet中又这么两个词,但却无法使用JWI由item X指向Item Y,原因就是混用了JWI的词典指针lexical pointers和语义指针semantic pointers。这里的指针不是C/C++的指针,是指词条之间、词集之间的一个连接关系。

 

词典指针——指词与词一次之间的关联关系,比如说dog与domestic dog的近义词等关系。包括词同形异义的两个词,派生关系等等;其工作的范围是词与词之间。

语义指针——指词集与词集之间的关联关系,一个词集Synset是包含多个词的,词集之间包括了词典指针的关系,只要是使用关联比如上下词关系、部分与整体等。其工作的范围是词集。

WordNet结果图如下,包括词索引,词,词集以及相应的指针:

 

 

其中,红色的指针就是语义指针,而Word之间的蓝绿指针是词典指针。

 

指针使用方法

 

      而当使用语义指针时,必须使用ISynset对象的ISynset.getRelatedSynsets(IPointer)方法。而使用词典指针时,你可以使用IWord对象的IWord.getRelatedWords(IPointer)方法。

 

      至于如何去使用这两个指针,什么时候使用词典指针,什么时候使用语义指针,什么时候可以两者都是用,很不幸地是,WordNet的相关文档解释很少。不过M.A. Finlayson为我们通过观察和统计WordNet,指出了如图说是的指针统计表。

 

 

WordNet2.1以上的版本都几乎相似。

 

 

 

 

转载于:https://www.cnblogs.com/sl-shilong/archive/2013/01/22/2871997.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

WordNet词网研究6——之JWI(Java Wordnet Interface)WordNet Java接口 的相关文章

  • 如何使用 Java 和 Selenium WebDriver 在 C 目录中创建文件夹并需要将屏幕截图保存在该目录中?

    目前正在与硒网络驱动程序和代码Java 我有一种情况 我需要在 C 目录中创建一个文件夹 并在该文件夹中创建我通过 selenium Web 驱动程序代码拍摄的屏幕截图 它需要存储在带有时间戳的文件夹中 如果我每天按计划运行脚本 所有屏幕截
  • 如何默认将 Maven 插件附加到阶段?

    我有一个 Maven 插件应该在编译阶段运行 所以在项目中consumes我的插件 我必须做这样的事情
  • Java - 将节点添加到列表的末尾?

    这是我所拥有的 public class Node Object data Node next Node Object data Node next this data data this next next public Object g
  • Java JDBC:更改表

    我希望对此表进行以下修改 添加 状态列 varchar 20 日期列 时间戳 我不确定该怎么做 String createTable Create table aircraft aircraftNumber int airLineCompa
  • Android MediaExtractor seek() 对 MP3 音频文件的准确性

    我在使用 Android 时无法在eek 上获得合理的准确度MediaExtractor 对于某些文件 例如this one http www archive org download emma solo librivox emma 01
  • 多个 Maven 配置文件激活多个 Spring 配置文件

    我想在 Maven 中构建一个环境 在其中我想根据哪些 Maven 配置文件处于活动状态来累积激活多个 spring 配置文件 目前我的 pom xml 的相关部分如下所示
  • Spring @RequestMapping 带有可选参数

    我的控制器在请求映射中存在可选参数的问题 请查看下面的控制器 GetMapping produces MediaType APPLICATION JSON VALUE public ResponseEntity
  • 无法解析插件 Java Spring

    我正在使用 IntelliJ IDEA 并且我尝试通过 maven 安装依赖项 但它给了我这些错误 Cannot resolve plugin org apache maven plugins maven clean plugin 3 0
  • 十进制到八进制的转换[重复]

    这个问题在这里已经有答案了 可能的重复 十进制转换错误 https stackoverflow com questions 13142977 decimal conversion error 我正在为一个类编写一个程序 并且在计算如何将八进
  • 加密 JBoss 配置中的敏感信息

    JBoss 中的标准数据源配置要求数据库用户的用户名和密码位于 xxx ds xml 文件中 如果我将数据源定义为 c3p0 mbean 我会遇到同样的问题 是否有标准方法来加密用户和密码 保存密钥的好地方是什么 这当然也与 tomcat
  • Java Integer CompareTo() - 为什么使用比较与减法?

    我发现java lang Integer实施compareTo方法如下 public int compareTo Integer anotherInteger int thisVal this value int anotherVal an
  • AWS 无法从 START_OBJECT 中反序列化 java.lang.String 实例

    我创建了一个 Lambda 函数 我想在 API 网关的帮助下通过 URL 访问它 我已经把一切都设置好了 我还创建了一个application jsonAPI Gateway 中的正文映射模板如下所示 input input params
  • 在 Mac 上正确运行基于 SWT 的跨平台 jar

    我一直致力于一个基于 SWT 的项目 该项目旨在部署为 Java Web Start 从而可以在多个平台上使用 到目前为止 我已经成功解决了由于 SWT 依赖的系统特定库而出现的导出问题 请参阅相关thread https stackove
  • Google App Engine 如何预编译 Java?

    App Engine 对应用程序的 Java 字节码使用 预编译 过程 以增强应用程序在 Java 运行时环境中的性能 预编译代码的功能与原始字节码相同 有没有详细的信息这是做什么的 我在一个中找到了这个谷歌群组消息 http groups
  • 如何从终端运行处理应用程序

    我目前正在使用加工 http processing org对于一个小项目 但是我不喜欢它附带的文本编辑器 我使用 vim 编写所有代码 我找到了 pde 文件的位置 并且我一直在从 vim 中编辑它们 然后重新打开它们并运行它们 重新加载脚
  • 如何从指定日期获取上周五的日期? [复制]

    这个问题在这里已经有答案了 如何找出上一个 上一个 星期五 或指定日期的任何其他日期的日期 public getDateOnDay Date date String dayName 我不会给出答案 先自己尝试一下 但是 也许这些提示可以帮助
  • 编译器抱怨“缺少返回语句”,即使不可能达到缺少返回语句的条件

    在下面的方法中 编译器抱怨缺少退货声明即使该方法只有一条路径 并且它包含一个return陈述 抑制错误需要另一个return陈述 public int foo if true return 5 鉴于Java编译器可以识别无限循环 https
  • 在 Maven 依赖项中指定 jar 和 test-jar 类型

    我有一个名为 commons 的项目 其中包含运行时和测试的常见内容 在主项目中 我添加了公共资源的依赖项
  • Firebase 添加新节点

    如何将这些节点放入用户节点中 并创建另一个节点来存储帖子 我的数据库参考 databaseReference child user getUid setValue userInformations 您需要使用以下代码 databaseRef
  • 捕获的图像分辨率太大

    我在做什么 我允许用户捕获图像 将其存储到 SD 卡中并上传到服务器 但捕获图像的分辨率为宽度 4608 像素和高度 2592 像素 现在我想要什么 如何在不影响质量的情况下获得小分辨率图像 例如我可以获取或设置捕获的图像分辨率为原始图像分

随机推荐

  • ubuntu - 如何以root身份使用图形界面管理文件?

    nautilus 是gnome的文件管理器 xff0c 但是如果不是root账号下 xff0c 权限受限 xff0c 我们可以通过以下方式以root权限使用 xff01 一 xff0c 快捷键 ctrl 43 alt 43 t 调出shel
  • debian添加删除用户

    debian添加删除用户 增加普通用户 命令 xff1a adduser abc passwd abc exit 用abc登录 etc passwd中保存了用户信息 LINUX创建用户的命令 useradd g test d home te
  • MongoDB——副本集的同步、选举和回滚

    1 同步 复制用于在多台服务器之间备份数据 MongoDB的复制功能是使用操作日志oplog实现的 xff0c 操作日志包含了主节点的每一次写操作 oplog是主节点的locl数据库中的一个固定集合 备份节点通过查询这个集合就可以知道需要进
  • ftp (文件传输协议)

    ftp xff08 文件传输协议 xff09 锁定 本词条由 科普中国 百科科学词条编写与应用工作项目 审核 FTP 是File Transfer Protocol xff08 文件传输协议 xff09 的英文简称 xff0c 而中文简称为
  • SQL Server 中的 JSON 数据

    下面是 JSON 文本的示例 34 name 34 34 John 34 34 skills 34 34 SQL 34 34 C 34 34 Azure 34 34 name 34 34 Jane 34 34 surname 34 34 D
  • 去除地址栏带#的问题

    vue cil搭建的项目 第一步 xff1a 找到目录下router js文件 第二步 xff1a 在new Router xff08 routes xff1a xff09 中添加mode属性 xff0c 默认mode是hash expor
  • 如何给自己的Python项目制作安装包

    Packaging Python Projects 本教程将指导您如何打包一个简单的Python项目 它将向您展示如何添加必要的文件和结构来创建包 xff0c 如何构建包以及如何将其上载到Python包索引 A simple project
  • linux安装解压工具gzip,笔记6 压缩工具(gzip,bzip2,xz,zip,tar)。

    压缩打包 常见的压缩文件 windows rar zip 7z Linux zip gz bz2 xz tar gz tar bz2 tar xz gzip压缩工具 不能压缩目录 gzip压缩后边直接跟文件名就可以 xff0c gunzip
  • 洛谷 P3367 【模板】并查集

    P3367 模板 并查集 题目描述 如题 xff0c 现在有一个并查集 xff0c 你需要完成合并和查询操作 输入输出格式 输入格式 xff1a 第一行包含两个整数N M xff0c 表示共有N个元素和M个操作 接下来M行 xff0c 每行
  • js计算器(正则)

    lt doctype html gt lt html gt lt head gt lt meta charset 61 34 utf 8 34 gt lt title gt 我的计算器 lt title gt lt style gt mar
  • MySQL 分组后取每组前N条数据

    与oracle的 rownumber over partition by xxx order by xxx 语句类似 xff0c 即 xff1a 对表分组后排序 创建测试emp表 DROP TABLE IF EXISTS emp CREAT
  • archlinux 安装搜狗输入法

    安装可能需要 archlinuxcn 的源 xff0c 我这里已经配置好了 一 安装 fcitx fcitx configtool fcitx im pacman S fcitx fcitx configtool fcitx im 二 在
  • MongoDB——JavaAPI详解

    环境配置 引入MongoDB驱动 xff1a span class token tag span class token tag span class token punctuation lt span dependency span sp
  • 练习题||并发编程

    线程 进程 队列 IO多路模型 操作系统工作原理介绍 线程 进程演化史 特点 区别 互斥锁 信号 事件 join GIL 进程间通信 管道 队列 生产者消息者模型 异步模型 IO多路复用模型 select poll epoll 高性能IO模
  • luogu P2078 朋友

    题目背景 小明在A公司工作 xff0c 小红在B公司工作 题目描述 这两个公司的员工有一个特点 xff1a 一个公司的员工都是同性 A公司有N名员工 xff0c 其中有P对朋友关系 B公司有M名员工 xff0c 其中有Q对朋友关系 朋友的朋
  • Debian 9 Stretch国内常用镜像源

    使用说明 一般情况下 xff0c 修改 etc apt sources list文件 xff0c 将Debian的默认源地址改成新的地址即可 xff0c 比如将http deb debian org改成https mirrors xxx c
  • ubuntu下编译ffmpeg并用eclipse调试

    一 下载ffnpeg源码 下载地址 xff1a http ffmpeg org download html 二 解决版本问题 可能之前你编译过ffmpeg xff0c 或者装过相关的库 xff0c 那都要先卸载掉 xff0c 否则用的时候会
  • 定时器初值计算

    1 定时器初值的计算 xff1a xff08 1 xff09 计算出机器周期 每次定时计算器加1所用的时间 xff08 2 xff09 根据你要定时的时间去算出初值 xff1a 假设你要定时Xms xff08 X lt 65 535ms x
  • ceph部署出现错误及解决

    ceph deploy new error hostname node1 is not resolvable 解决办法 xff0c 修改 etc hosts 127 0 0 1 localhost 127 0 1 1 ubuntu1 192
  • WordNet词网研究6——之JWI(Java Wordnet Interface)WordNet Java接口

    JWI the MIT Java Wordnet Interface is a Java library for interfacing with Wordnet JWI supports access to Wordnet version