Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

nltk - All synonyms for word in python?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

Sadly, the size of Wordnet is very limited so there are very few lemma_names available for each senses.

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:

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 meaning of 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...