2

我正在使用 WordNet 3.0。我想知道如何在 WordNet 层次结构中找到两个同义词集之间的语义关系。如,给定两个词作为输入,我想找到它们之间的关系,即它们是否是同义词、下位词-上位词等。

是否有 python 或 perl 模块来实现这一点?

4

3 回答 3

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 数据集并寻找它们的节点之间的最短路径,这里讨论的问题。

于 2013-07-16T09:01:35.617 回答
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')]
于 2013-09-18T19:00:42.043 回答
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
于 2020-06-15T07:44:20.300 回答