Natural language 22_wordnet with NLTK

Source: Internet
Author: User
Tags nltk

https://www.pythonprogramming.net/wordnet-nltk-tutorial/?completed=/nltk-corpus-corpora-tutorial/

Wordnet with NLTK




WordNet is a lexical database for the Chinese language, which was created by Princeton, and are part of the NLTK C Orpus.

You can use WordNet alongside the NLTK module to find the meanings of words, synonyms, antonyms, and More. Let ' s cover some examples.

first, you ' re going to need to import wordnet:

From nltk.  Import wordnet   

then, we ' re going to use the term "program" to find synsets like So:

= WordNet.  Synsets("program")     

An example of a synset:

Print(syns[0].  Name())      

plan.n.01

Just the Word:

Print(syns[0].  Lemmas() [0].  Name())          

plan

Definition of that first synset:

Print(syns[0].  Definition())      

a series of steps to be carried out or goals to be accomplished

Examples of the word in use:

Print(syns[0].  Examples())      

[‘they drew up a six-step plan‘, ‘they discussed plans for a new bond issue‘]

Next, How might we discern synonyms and antonyms to a word? The lemmas is synonyms, and then you can use. antonyms to find the antonyms to the Lemmas. As such, we can populate some lists like:

Synonyms= []Antonyms= []ForSynInchWordnet.Synsets("good"): ForLInchSyn.Lemmas():Synonyms.Append(Lname ())  if l antonyms ():  Antonyms. (l. Antonyms () [0]. Name () print (set< Span class= "pun" > (synonymsprint (set (antonyms   
{' beneficial ', ' just ', ' upright ', ' thoroughly ', ' In_force ', ' well ', ' skilful ', ' skillful ', ' sound ', ' unspoiled ', ' expert ', ' Proficient ', ' in_effect ', ' honorable ', ' adept ', ' secure ', ' commodity ', ' estimable ', ' soundly ', ' right ', ' respectable ' ', ' good ', ' serious ', ' ripe ', ' salutary ', ' dear ', ' practiced ', ' goodness ', ' safe ', ' effective ', ' unspoilt ', ' dependable ', ' undecomposed ', ' honest ', ' full ', ' near ', ' Trade_good '} {' evil ', ' evilness ', ' bad ', ' badness ', ' ill '}

As can see, we got many more synonyms than antonyms, since we just looked up the antonym for the first lemma Could easily balance this buy also doing the exact same process for the term "bad."

Next, we can also easily use WordNet to compare the similarity of both words and their tenses, by incorporating the Wu and Palmer method for semantic related-ness.

Let's compare the noun of "ship" and "boat:"

= WordNet.  Synset(' ship.n.01 ')= WordNet.  Synset(' boat.n.01 ')print(W1.  Wup_similarity(W2))            

0.9090909090909091

= WordNet.  Synset(' ship.n.01 ')= WordNet.  Synset(' car.n.01 ')print(W1.  Wup_similarity(W2))            

0.6956521739130435

= WordNet.  Synset(' ship.n.01 ')= WordNet.  Synset(' cat.n.01 ')print(W1.  Wup_similarity(W2))            

0.38095238095238093

Next, we ' re going to pick things up a bit and begin to cover the topic of Text Classification.

Natural language 22_wordnet with NLTK

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.