如何使用wordnet

介绍

WordNet是包含语义信息的英语词典。

  1. wordnet根据单词的语义分组,相同语义的单词组合在一起称为synset(同义词集),一个一词多义的单词将出现在它的每个语义对应的同义词集中
  2. wordnet为每一个synset提供了简短,概要的定义,并记录不同synset之间的语义关系
  3. 在 wordnet中,名词,动词,形容词和副词各自组织成一个同义词的网络,四种不同词性的网络之间没有连接

python调用wordnet

可通过nltk工具包来导入wordnet

from nltk.corpus import wordnet

如果导入错误,尝试重新下载wordnet

  import nltk
  nltk.download('wordnet')

获得单词对应的同义词集

print(wordnet.sysnets('room'))
[Synset('room.n.01'), Synset('room.n.02'), Synset('room.n.03'), Synset('room.n.04'), Synset('board.v.02')]

每个同义词集都有自己的名称,词性,以及编号。这里room有5个同义词集,其中四个叫room,是名词,最后一个叫board,是动词。

也可以指定词性,获取不同词性对应的同义词集

print(wordnet.synsets("room", pos=wordnet.NOUN))
# NOUN, ADJ and ADV,VERB
[Synset('room.n.01'), Synset('room.n.02'), Synset('room.n.03'), Synset('room.n.04')]

获得同义词集的定义

对于上面获得的5个同义词集,可以通过以下两种方式来获得第一个同义词集Synset('room.n.01')的定义

(1) 通过返回同义词集列表获得

syn_arr = wordnet.synsets('room')
print(syn_arr[0].definition())
an area within a building enclosed by walls and floor and ceiling
# 楼板建筑物内由墙壁、地板和天花板围起来的区域

(2)直接指定同义词集的名字

print(wordnet.synset('room.n.01').definition())
an area within a building enclosed by walls and floor and ceiling
# 楼板建筑物内由墙壁、地板和天花板围起来的区域

获取同义词集对应的例子

对于单词room的五个不同的语义,给出相应的例句或短语

for syn in wordnet.synsets("room"):
    print(syn.name())
    print(syn.examples())
room.n.01
['the rooms were very small but they had a nice view']
room.n.02
['room to pass', 'make way for', 'hardly enough elbow room to turn around']
room.n.03
['room for improvement']
room.n.04
['the whole room was cheering']
board.v.02
['she rooms in an old boarding house']

获得同义词集包含的lemma

同义词集包含的单词一般是词根(lemma)的形式,比如说love这个单词,同义词集中只会包含love而不会包含loves,loved这些变形。

syn_arr = wordnet.synsets("room")
print(syn_arr[1].lemmas())
print(syn_arr[1].lemma_names())
[Lemma('room.n.02.room'), Lemma('room.n.02.way'), Lemma('room.n.02.elbow_room')]
['room', 'way', 'elbow_room']

同样的,也可以反向获取lemma所在的同义词集

print(syn_arr[1].lemmas()[0].synset())
Synset('room.n.02')

获取单词对应的lemma

当使用wordnet查询单词时,需要知道单词的词根形式

print(wordnet.morphy('denied'))
deny
bike对应的同义集合

获取下位同义词集

不同synset之间的语义关系存在上下位关系,例如日历这个单词对应下位词:阳历,阴历

print(wordnet.synset('calendar.n.01').hyponyms())
[Synset('lunar_calendar.n.01'), Synset('lunisolar_calendar.n.01'), Synset('solar_calendar.n.01')]

获取上位同义词集

同样的,可以通过下位同义词集获得上位同义词集

print(wordnet.synset('solar_calendar.n.01').hypernyms())
[Synset('calendar.n.01')]

上述的结果,除了使用python nltk获得以外,还可以直接进入官网,在线输入,查询

在线查询例子

还有一个重点就是如何使用wordnet计算单词之间的语义相似度,更多的方法可见WordNet Interface

参考

wordnet官网

https://pythonprogramming.net/wordnet-nltk-tutorial/

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 算法技术解构 1、Python基础知识 (1)IPythonIPython的开发者吸收了标准解释器的基本概念,在此...
    shenciyou阅读 5,388评论 0 10
  • 1. 说明  今天讨论的是自然语言中的知识抽取和知识表示,换言之,就是如何从大量的书籍文献中剥离出我们关心的...
    xieyan0811阅读 4,364评论 0 0
  • 最近在coursera上学习Princeton大学的Algorithm PartII,这个系列的两门课是我见过最好...
    lyy0905阅读 1,384评论 1 0
  • 概述 nltk是一个自然语言处理工具包,在NLP领域中,最常使用的一个Python库。https://yiyibo...
    HyRer阅读 4,364评论 0 1
  • 表情是什么,我认为表情就是表现出来的情绪。表情可以传达很多信息。高兴了当然就笑了,难过就哭了。两者是相互影响密不可...
    Persistenc_6aea阅读 125,872评论 2 7