📅  最后修改于: 2020-04-27 15:57:57             🧑  作者: Mango
WordNet是一个大型的英语词汇数据库。名词,动词,形容词和副词被分组为认知同义词(同义词集),每组表达不同的概念。同义词集通过概念语义和词汇关系相互关联。
WordNet的结构使其成为计算语言学和自然语言处理的有用工具。
WordNet表面上类似于一个同义词库,它根据单词的含义将单词分组在一起。但是,有一些重要的区别。
# 首先,您需要导入wordnet:
from nltk.corpus import wordnet
# 然后,我们将使用术语“program"来查找类似的同义词集:
syns = wordnet.synsets("program")
# 同义词集的示例:
print(syns[0].name())
# 只是这个词:
print(syns[0].lemmas()[0].name())
# 该第一个同义词集的定义:
print(syns[0].definition())
# 句子中使用的单词示例:
print(syns[0].examples())
输出将如下所示:
plan.n.01
plan
要执行的一系列步骤或要实现的目标
[‘they drew up a six-step plan’, ‘they discussed plans for a new bond issue’]
接下来,我们如何辨别单词的同义词和反义词?lemmas将是同义词,然后您可以使用.antonyms查找lemmas的反义词。因此,我们可以填充一些列表,例如:
import nltk
from nltk.corpus import wordnet
synonyms = []
antonyms = []
for syn in wordnet.synsets("good"):
for l in syn.lemmas():
synonyms.append(l.name())
if l.antonyms():
antonyms.append(l.antonyms()[0].name())
print(set(synonyms))
print(set(antonyms))
输出将是两组同义词和反义词
{‘beneficial’, ‘just’, ‘upright’, ‘thoroughly’, ‘in_force’, ‘well’, ‘skilful’, ‘skillful’, ‘sound’, ‘unspoiled’, ‘expert’, ‘proficient’, ‘in_effect’, ‘honorable’, ‘adept’, ‘secure’, ‘commodity’, ‘estimable’, ‘soundly’, ‘right’, ‘respectable’, ‘good’, ‘serious’, ‘ripe’, ‘salutary’, ‘dear’, ‘practiced’, ‘goodness’, ‘safe’, ‘effective’, ‘unspoilt’, ‘dependable’, ‘undecomposed’, ‘honest’, ‘full’, ‘near’, ‘trade_good’} {‘evil’, ‘evilness’, ‘bad’, ‘badness’, ‘ill’}
现在,让我们比较任意两个词的相似性指数
import nltk
from nltk.corpus import wordnet
# 让我们比较一下“ ship"和“ boat"的名词。
w1 = wordnet.synset('run.v.01') # v here denotes the tag verb
w2 = wordnet.synset('sprint.v.01')
print(w1.wup_similarity(w2))
输出:
0.857142857143
w1 = wordnet.synset('ship.n.01')
w2 = wordnet.synset('boat.n.01') # n denotes noun
print(w1.wup_similarity(w2))
输出:
0.9090909090909091