কম্পিউটার

কিভাবে পাইথনে NLTK WordNet থেকে প্রতিশব্দ/বিরুদ্ধ শব্দ পেতে হয়


ওয়ার্ডনেট পাইথনের প্রাকৃতিক ভাষা টুলকিটের একটি অংশ। এটি ইংরেজি Nouns, Adjectives, Adverbs এবং Verbs এর একটি বড় শব্দ ডাটাবেস। এগুলিকে কিছু জ্ঞানীয় প্রতিশব্দের সেটে বিভক্ত করা হয়, যেগুলিকে সিনসেট বলা হয় .

Wordnet ব্যবহার করার জন্য, প্রথমে আমাদের NLTK মডিউল ইনস্টল করতে হবে, তারপর WordNet প্যাকেজটি ডাউনলোড করতে হবে।

$ sudo pip3 install nltk
$ python3
>>> import nltk
>>>nltk.download('wordnet')

ওয়ার্ডনেটে, শব্দের কিছু গ্রুপ আছে, যার অর্থ একই।

প্রথম উদাহরণে, আমরা দেখব কিভাবে wordnet একটি শব্দের অর্থ এবং অন্যান্য বিবরণ প্রদান করে। কখনও কখনও, যদি কিছু উদাহরণ পাওয়া যায়, এটি তাও প্রদান করতে পারে।

উদাহরণ কোড

from nltk.corpus import wordnet   #Import wordnet from the NLTK
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   #Import wordnet from the NLTK
syn = list()
ant = list()
for synset in wordnet.synsets("Worse"):
   for lemma in synset.lemmas():
      syn.append(lemma.name())    #add the synonyms
      if lemma.antonyms():    #When antonyms are available, add them into the list
      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']
$

এনএলটিকে ওয়ার্ডনেটের আরেকটি দুর্দান্ত বৈশিষ্ট্য রয়েছে, এটি ব্যবহার করে আমরা দুটি শব্দ প্রায় সমান কিনা তা পরীক্ষা করতে পারি। এটি একটি জোড়া শব্দ থেকে সাদৃশ্য অনুপাত ফিরিয়ে দেবে।

উদাহরণ কোড

import nltk
from nltk.corpus import wordnet     #Import wordnet from the NLTK
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
$

  1. পাইথন 3 এ টিকিন্টার ফাইলিয়ালগ থেকে কীভাবে একটি স্ট্রিং পাবেন?

  2. Tkinter/Python এ একটি পপআপ ডায়ালগ কিভাবে পাবেন?

  3. Python Tkinter এ একটি চেকবক্স থেকে কিভাবে ইনপুট পেতে হয়?

  4. পাইথনের একটি লেবেল থেকে পাঠ্য কীভাবে সরানো যায়?