我正在尝试使用nltk的wordnet获取所有同义词或类似单词,但它没有返回。

我在做:

>>> from nltk.corpus import wordnet as wn
>>> wn.synsets('swap')
[Synset('barter.n.01'), Synset('trade.v.04'), Synset('swap.v.02')]


我也尝试做(从stackoverflow页面之一):

>>> for ss in wn.synsets('swap'):
    for sim in ss.similar_tos():
        print('     {}'.format(sim))


但是我并没有得到所有的同义词。我不想在wordnet中添加同义词。
我期望它返回交换,交换,替代等。

如何实现呢?

谢谢

阿比

最佳答案

要使用wordnet获取同义词,只需执行以下操作:

>>> from nltk.corpus import wordnet as wn
>>> for synset in wn.synsets('swap'):
    for lemma in synset.lemmas():
        print lemma.name(),

barter swap swop trade trade swap swop switch swap  # note the overlap between the synsets


要获得您提到的某些单词,您可能还必须包括上位词:

>>> for synset in wn.synsets('swap'):
    for hypernym in synset.hypernyms():
        for ss in hypernym.lemmas():  # now you need to iterate through each synset returned by synset.hypernyms()
            print ss.name(),

exchange interchange exchange change interchange travel go move locomote  # again, some overlap

07-28 07:57