WordNet Python के नेचुरल लैंग्वेज टूलकिट का हिस्सा है। यह अंग्रेजी संज्ञा, विशेषण, क्रियाविशेषण और क्रियाओं का एक बड़ा शब्द डेटाबेस है। इन्हें संज्ञानात्मक समानार्थियों के कुछ सेट में समूहित किया गया है, जिन्हें synsets कहा जाता है।
WordNet का उपयोग करने के लिए, सबसे पहले हमें NLTK मॉड्यूल इंस्टॉल करना होगा, फिर WordNet पैकेज डाउनलोड करना होगा।
$ sudo pip3 install nltk
$ python3
>>> import nltk
>>> nltk.download('wordnet')
WordNet में, शब्दों के कुछ समूह हैं, जिनका अर्थ समान है।
पहले उदाहरण में, हम देखेंगे कि WordNet किसी शब्द का अर्थ और अन्य विवरण कैसे लौटाता है। कभी-कभी, यदि कुछ उदाहरण उपलब्ध हैं, तो यह उन्हें भी प्रदान कर सकता है।
उदाहरण कोड - शब्द की जानकारी प्राप्त करना
from nltk.corpus import wordnet
synset = wordnet.synsets("Travel")
print('Word and Type: ' + synset[0].name())
print('Synonym of Travel is: ' + synset[0].lemmas()[0].name())
print('The meaning of the word: ' + synset[0].definition())
print('Example of Travel: ' + str(synset[0].examples()))
आउटपुट
Word and Type: travel.n.01
Synonym of Travel is: travel
The meaning of the word: the act of going from one place to another
Example of Travel: ['he enjoyed selling but he hated the travel']
पिछले उदाहरण में, हम कुछ शब्दों के बारे में विस्तृत जानकारी प्राप्त कर रहे हैं। यहां हम देखेंगे कि WordNet किसी दिए गए शब्द के समानार्थी और विलोम शब्द कैसे भेज सकता है।
उदाहरण कोड - समानार्थी और विलोम शब्द प्राप्त करना
import nltk
from nltk.corpus import wordnet
syn = list()
ant = list()
for synset in wordnet.synsets("Worse"):
for lemma in synset.lemmas():
syn.append(lemma.name()) # समानार्थी जोड़ें
if lemma.antonyms(): # जब विलोम उपलब्ध हों, उन्हें सूची में जोड़ें
ant.append(lemma.antonyms()[0].name())
print('Synonyms: ' + str(syn))
print('Antonyms: ' + str(ant))
आउटपुट
Synonyms: ['worse', 'worse', 'worse', 'worsened', 'bad', 'bad', 'big', 'bad', 'tough', 'bad', 'spoiled', 'spoilt', 'regretful', 'sorry', 'bad', 'bad', 'uncollectible', 'bad', 'bad', 'bad', 'risky', 'high-risk', 'speculative', 'bad', 'unfit', 'unsound', 'bad', 'bad', 'bad', 'forged', 'bad', 'defective', 'worse']
Antonyms: ['better', 'better', 'good', 'unregretful']
NLTK WordNet में एक और शानदार फीचर है, जिसका उपयोग करके हम जांच सकते हैं कि दो शब्द लगभग समान हैं या नहीं। यह शब्दों की जोड़ी से समानता अनुपात लौटाएगा।
उदाहरण कोड - शब्द समानता जांचना
import nltk
from nltk.corpus import wordnet
first_word = wordnet.synset("Travel.v.01")
second_word = wordnet.synset("Walk.v.01")
print('Similarity: ' + str(first_word.wup_similarity(second_word)))
first_word = wordnet.synset("Good.n.01")
second_word = wordnet.synset("zebra.n.01")
print('Similarity: ' + str(first_word.wup_similarity(second_word)))
आउटपुट
Similarity: 0.6666666666666666
Similarity: 0.09090909090909091