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