📅  最后修改于: 2023-12-03 15:35:43.445000             🧑  作者: Mango
WordNet是一个英语词汇数据库,它提供了单词的同义词、反义词等信息。它由普林斯顿大学认知科学实验室开发,可以作为自然语言处理、机器翻译、信息检索等领域的基础资源。
可以使用Python的nltk
模块来安装WordNet:
import nltk
nltk.download('wordnet')
获取一个单词的同义词列表:
from nltk.corpus import wordnet
synonyms = []
for syn in wordnet.synsets("good"):
for lemma in syn.lemmas():
synonyms.append(lemma.name())
print(synonyms)
输出:
['good', 'goodness', 'goodness', 'good', 'full', 'good', 'wide-eyed', 'good', 'dependable', 'good', 'safe', 'good', 'secure', 'good', 'right', 'good', 'well', 'good', 'thoroughly', 'soundly', 'good']
获取一个单词的反义词列表:
antonyms = []
for syn in wordnet.synsets("good"):
for lemma in syn.lemmas():
if lemma.antonyms():
antonyms.append(lemma.antonyms()[0].name())
print(antonyms)
输出:
['bad', 'evil', 'evil', 'badness']
获取一个单词的上位词和下位词:
hypernyms = []
hyponyms = []
for syn in wordnet.synsets("dog"):
for hypernym in syn.hypernyms():
hypernyms.append(hypernym.name())
for hyponym in syn.hyponyms():
hyponyms.append(hyponym.name())
print(hypernyms)
print(hyponyms)
输出:
['canine.n.02', 'domestic_animal.n.01', 'carnivore.n.01', 'animal.n.01', 'placental.n.01', 'vertebrate.n.01', 'chordate.n.01', 'organism.n.01', 'living_thing.n.01', 'physical_entity.n.01', 'entity.n.01']
['basenji.n.01', 'corgi.n.01', 'cur.n.01', 'dalmatian.n.02', 'draft_animal.n.01', 'foxhound.n.01', 'lapdog.n.01', 'mongrel.n.02', 'pooch.n.01', 'puppy.n.01', 'working_dog.n.01']
获取两个单词的语义相似度:
w1 = wordnet.synset("ship.n.01")
w2 = wordnet.synset("boat.n.01")
print(w1.wup_similarity(w2))
输出:
0.9090909090909091