如何从 nltk WordNet Python 中获取同义词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19258652/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get synonyms from nltk WordNet Python
提问by user2758113
WordNet is great, but I'm having a hard time getting synonyms in nltk. If you search similar to for the word 'small' like here, it shows all of the synonyms.
WordNet 很棒,但我很难在 nltk 中获得同义词。如果您搜索类似于此处的单词“小” ,它会显示所有同义词。
Basically I just need to know the following:
wn.synsets('word')[i].option()
Where option can be hypernyms and antonyms, but what is the option for getting synonyms?
基本上我只需要知道以下内容:
wn.synsets('word')[i].option()
哪里可以选择上位词和反义词,但是获取同义词的选项是什么?
采纳答案by Francis Bond
If you want the synonyms in the synset (aka the lemmas that make up the set), you can get them with lemma_names()
:
如果您想要同义词集中的同义词(也就是组成集合的引理),您可以使用以下命令获取它们lemma_names()
:
>>> for ss in wn.synsets('small'):
>>> print(ss.name(), ss.lemma_names())
small.n.01 ['small']
small.n.02 ['small']
small.a.01 ['small', 'little']
minor.s.10 ['minor', 'modest', 'small', 'small-scale', 'pocket-size', 'pocket-sized']
little.s.03 ['little', 'small']
small.s.04 ['small']
humble.s.01 ['humble', 'low', 'lowly', 'modest', 'small']
...
回答by abarnert
You've already got the synonyms. That's what a Synset
is.
你已经有了同义词。这就是a Synset
。
>>> wn.synsets('small')
[Synset('small.n.01'),
Synset('small.n.02'),
Synset('small.a.01'),
Synset('minor.s.10'),
Synset('little.s.03'),
Synset('small.s.04'),
Synset('humble.s.01'),
Synset('little.s.07'),
Synset('little.s.05'),
Synset('small.s.08'),
Synset('modest.s.02'),
Synset('belittled.s.01'),
Synset('small.r.01')]
That's the same list of top-level entries that the web interface gave you.
这与 Web 界面为您提供的顶级条目列表相同。
If you also want the "similar to" list, that's not the same thing as the synonyms. For that, you call similar_tos()
on each Synset
.
如果您还想要“类似于”列表,那么这与同义词不同。为此,您调用similar_tos()
每个Synset
.
So, to show the same information as the website, start with something like this:
因此,要显示与网站相同的信息,请从以下内容开始:
for ss in wn.synsets('small'):
print(ss)
for sim in ss.similar_tos():
print(' {}'.format(sim))
Of course the website is also printing the part of speech (sim.pos
), list of lemmas (sim.lemma_names
), definition (sim.definition
), and examples (sim.examples
) for each synset at both levels. and it's grouping them by parts of speech, and it's added in links to other things that you can follow, and so forth. But that should be enough to get you started.
当然,该网站还在两个级别打印每个同义词集的词性 ( sim.pos
)、引理列表 ( sim.lemma_names
)、定义 ( sim.definition
) 和示例 ( sim.examples
)。它按词性对它们进行分组,并添加到您可以关注的其他内容的链接中,等等。但这应该足以让你开始。
回答by Kasramvd
You can use wordnet.synset
and lemmasin order to get all the synonyms:
您可以使用wordnet.synset
和引理来获取所有同义词:
example :
例子 :
from itertools import chain
from nltk.corpus import wordnet
synonyms = wordnet.synsets(text)
lemmas = set(chain.from_iterable([word.lemma_names() for word in synonyms]))
Demo:
演示:
>>> synonyms = wordnet.synsets('change')
>>> set(chain.from_iterable([word.lemma_names() for word in synonyms]))
set([u'interchange', u'convert', u'variety', u'vary', u'exchange', u'modify', u'alteration', u'switch', u'commute', u'shift', u'modification', u'deepen', u'transfer', u'alter', u'change'])
回答by Oliver Nina
This worked for me
这对我有用
wordnet.synsets('change')[0].hypernyms()[0].lemma_names()
wordnet.synsets('change')[0].hypernyms()[0].lemma_names()
回答by Siraj Alam
Simplest program to print the synonyms of a given word
打印给定单词同义词的最简单程序
from nltk.corpus import wordnet
for syn in wordnet.synsets("good"):
for name in syn.lemma_names():
print(name)
回答by Syauqi Haris
I've code Thesaurus Lookup for Synonym recently, I used this function :
我最近编写了同义词的同义词查找代码,我使用了这个功能:
def find_synonyms(keyword) :
synonyms = []
for synset in wordnet.synsets(keyword):
for lemma in synset.lemmas():
synonyms.append(lemma.name())
return str(synonyms)
But if you prefer to host your own Dictionary, you might interested with my project on offline synonym dictionary lookup on my github page :
但是,如果您更喜欢托管自己的字典,您可能会对我的 github 页面上的离线同义词字典查找项目感兴趣:
https://github.com/syauqiex/offline_english_synonym_dictionary
https://github.com/syauqiex/offline_english_synonym_dictionary