python中单词的所有同义词?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19348973/
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
All synonyms for word in python?
提问by user2834165
The code to get the synonyms of a word in python is say:
在python中获取单词同义词的代码是:
from nltk.corpus import wordnet
dog = wordnet.synset('dog.n.01')
print dog.lemma_names
>>['dog', 'domestic_dog', 'Canis_familiaris']
However dog.n.02 gives different words. For any words i can't know how many words there may be. How can i return all of the synonyms for a word?
但是 dog.n.02 给出了不同的词。对于任何单词,我不知道可能有多少个单词。如何返回一个单词的所有同义词?
回答by Udo Klein
回答by arturomp
Note this other answer:
注意这个其他答案:
>>> 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')]
Keep in mind that in your code you were trying to get the lemmas, but that's one level too deep for what you want. The synset level is about meaning, while the lemma level gives you words.In other words:
请记住,在您的代码中,您试图获得引理,但这对于您想要的来说太深了。同义词级别是关于意义的,而引理级别为您提供单词。换句话说:
In WordNet (and I'm speaking of English WordNet here, though I think the ones in other langauges are similarly organized) a lemma has senses. Specifically, a lemma (that is, a base word form that is indexed in WordNet) has exactly as many senses as the number of synsets that it participates in. Conversely, and as you say, synsets contain one more more lemmas, which means that multiple lemmas (words) can represent the same sense, or meaning.
在 WordNet(我在这里说的是英语 WordNet,尽管我认为其他语言中的 WordNet 的组织方式类似)中的引理具有意义。具体来说,一个引理(即在 WordNet 中编入索引的基本词形式)与它参与的同义词集的数量完全一样多。 相反,正如你所说,同义词集包含另外一个引理,这意味着多个引理(词)可以表示相同的含义或含义。
Also have a look at the NLTK's WordNet how tofor a few more ways of exploring around a meaning or a word.
还可以查看NLTK 的 WordNet how to了解更多探索含义或单词的方法。
回答by alvas
Using wn.synset('dog.n.1').lemma_names
is the correct way to access the synonyms of a sense. It's because a word has many senses and it's more appropriate to list synonyms of a particular meaning/sense.To enumerate words with similar meanings, possibly you can also look at the hyponyms.
Usingwn.synset('dog.n.1').lemma_names
是访问某种意义的同义词的正确方法。这是因为一个词有很多含义,列出特定含义/含义的同义词更合适。要列举具有相似含义的单词,您可能还可以查看下义词。
Sadly, the size of Wordnet is very limited so there are very few lemma_names available for each senses.
遗憾的是,Wordnet 的大小非常有限,因此每种感官可用的 lemma_name 非常少。
Using Wordnet as a dictionary/thesarus is not very apt per se, because it was developed as an inventory of sense/meaning rather than a inventory of words. However you can use access the a particular sense and several (not a lot) related words to the sense. One can use Wordnet as a:
使用 Wordnet 作为字典/同义词库本身并不是很合适,因为它是作为意义/意义的清单而不是单词清单开发的。但是,您可以使用 access 特定意义和几个(不是很多)与该意义相关的词。可以将 Wordnet 用作:
Dictionary:given a word, what are the different meaning of the word
字典:给定一个词,这个词有什么不同的意思
for i,j in enumerate(wn.synsets('dog')):
print "Meaning",i, "NLTK ID:", j.name
print "Definition:",j.definition
Thesarus:given a word, what are the different words for each meaningof the word
同义词库:给定一个词,该词的每个含义有哪些不同的词
for i,j in enumerate(wn.synsets('dog')):
print "Meaning",i, "NLTK ID:", j.name
print "Definition:",j.definition
print "Synonyms:", ", ".join(j.lemma_names)
print
Ontology:given a word, what are the hyponyms (i.e. sub-types) and hypernyms (i.e. super-types).
本体:给定一个词,什么是下位词(即子类型)和上位词(即超类型)。
for i,j in enumerate(wn.synsets('dog')):
print "Meaning",i, "NLTK ID:", j.name
print "Hypernyms:", ", ".join(list(chain(*[l.lemma_names for l in j.hypernyms()])))
print "Hyponyms:", ", ".join(list(chain(*[l.lemma_names for l in j.hyponyms()])))
print
[Ontology Output]
【本体输出】
Meaning 0 NLTK ID: dog.n.01
Hypernyms words domestic_animal, domesticated_animal, canine, canid
Hyponyms puppy, Great_Pyrenees, basenji, Newfoundland, Newfoundland_dog, lapdog, poodle, poodle_dog, Leonberg, toy_dog, toy, spitz, pooch, doggie, doggy, barker, bow-wow, cur, mongrel, mutt, Mexican_hairless, hunting_dog, working_dog, dalmatian, coach_dog, carriage_dog, pug, pug-dog, corgi, Welsh_corgi, griffon, Brussels_griffon, Belgian_griffon
Meaning 1 NLTK ID: frump.n.01
Hypernyms: unpleasant_woman, disagreeable_woman
Hyponyms:
Meaning 2 NLTK ID: dog.n.03
Hypernyms: chap, fellow, feller, fella, lad, gent, blighter, cuss, bloke
Hyponyms:
Meaning 3 NLTK ID: cad.n.01
Hypernyms: villain, scoundrel
Hyponyms: perisher