我试图使用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中添加同义词。 我期待它返回交换,交换,替代等。
如何实现这一目标?
由于
ABHI
答案 0 :(得分:2)
要使用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