WordNet পাইথনের Natural Language Toolkit-র একটি অংশ। এটি ইংরেজি নাউন, অ্যাডজেক্টিভ, অ্যাডভার্ব এবং ভার্বের একটি বড় শব্দ ডেটাবেস। এগুলো কিছু কগনিটিভ সিনোনিমের সেটে গ্রুপ করা হয়েছে, যাকে synsets বলা হয়।
WordNet ব্যবহার করতে, প্রথমে NLTK মডিউল ইনস্টল করতে হবে, তারপর WordNet প্যাকেজ ডাউনলোড করতে হবে।
$ sudo pip3 install nltk
$ python3
>>> import nltk
>>> nltk.download('wordnet')
WordNet-এ কিছু শব্দের গ্রুপ রয়েছে যাদের অর্থ একই।
প্রথম উদাহরণে, আমরা দেখব WordNet কীভাবে একটি শব্দের অর্থ এবং অন্যান্য ডিটেইলস ফেরত দেয়। কখনও কখনও, যদি কিছু উদাহরণ উপলব্ধ থাকে, তবে সেটাও প্রদান করতে পারে।
উদাহরণ কোড
from nltk.corpus import wordnet # NLTK থেকে 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()))
আউটপুট
$ python3 322a.word_info.py 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 # NLTK থেকে 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))
আউটপুট
$ python3 322b.syn_ant.py 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 # NLTK থেকে 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)))
আউটপুট
$ python3 322c.compare.py Similarity: 0.6666666666666666 Similarity: 0.09090909090909091 $