我正在使用WordNet 3.0。 我想知道如何在WordNet层次结构中找到两个同义词之间的语义关系。 如同在两个单词作为输入时,我想找到它们之间的关系,即它们是否是同义词,hyponym-hypernym等。
是否有python或perl模块来实现这一目标?
答案 0 :(得分:5)
NLTK是Python最常用的NLP库。 所描述的操作类似于:
from nltk.corpus import wordnet
house = wordnet.synset('house.n.01')
station = wordnet.synset('station.n.01')
然后您可以这样使用类Synset的方法:
taxonomy_distance = house.shortest_path_distance(station)
发现它们是否是同义词:
common_lemmas = len(set(house.lemma_names).intersection(set(station.lemma_names)))
将返回两组中常见引物的数量。
您还可以使用像Neo4j这样的图形数据库来加载wordnet数据集并查找其节点之间的最短路径,这是一个问题here。
答案 1 :(得分:1)
使用Python NLTK:
>>> from nltk.corpus import wordnet as wn
>>>
>>> synset1 = wn.synset('adornment.n.01')
>>> synset2 = wn.synset('jewelry.n.01')
>>>
>>> synset1.lowest_common_hypernyms(synset2)
[Synset('adornment.n.01')]
>>>
>>> synset1.hyponyms()
[Synset('frill.n.03'), Synset('rosette.n.01'), Synset('frontlet.n.01'), Synset('cordon.n.03'), Synset('fob.n.02'), Synset('beauty_spot.n.01'), Synset('sequin.n.01'), Synset('war_paint.n.01'), Synset('boutonniere.n.01'), Synset('trimming.n.02'), Synset('pendant.n.01'), Synset('pompon.n.01'), Synset('band.n.04'), Synset('bangle.n.02'), Synset('jewelry.n.01'), Synset('epaulet.n.01'), Synset('circlet.n.02'), Synset('frog.n.03'), Synset('tassel.n.01'), Synset('plume.n.02'), Synset('pectoral.n.02')]
>>> synset2.hypernyms()
[Synset('adornment.n.01')]
获取同义词:
>>> def lemmas_in_all_synsets(keyword):
... lemmas=[]
... for synset in wn.synsets(keyword):
... for lemma in synset.lemmas:
... lemmas.append(lemma)
... return lemmas
...
>>> lemmas_in_all_synsets('station')
[Lemma('station.n.01.station'), Lemma('place.n.10.place'), Lemma('place.n.10.sta
tion'), Lemma('station.n.03.station'), Lemma('post.n.01.post'), Lemma('post.n.01
.station'), Lemma('station.n.05.station'), Lemma('station.v.01.station'), Lemma(
'station.v.01.post'), Lemma('station.v.01.send'), Lemma('station.v.01.place')]
答案 2 :(得分:0)
def synonyms(x):
from nltk.corpus import wordnet as wn
synonyms_x = []
for syn in wn.synsets(x):
for l in syn.lemmas():
synonyms_x.append(l.name())
#to remove duplicates from the list
synonyms_x = list(dict.fromkeys(synonyms_x))
return synonyms_x