কম্পিউটার টিউটোরিয়াল

পাইথনে NLTK দিয়ে পার্ট অফ স্পিচ ট্যাগিং ও স্টপ ওয়ার্ডস রিমুভাল

ন্যাচারেল ল্যাঙ্গুয়েজ প্রসেসিং (NLP)-এর মূল উদ্দেশ্য হলো মেশিন মানুষের হস্তক্ষেপ ছাড়া টেক্সট বুঝতে বা বিশ্লেষণ করতে পারে। টেক্সট প্রসেস করার সময় কম্পিউটারকে অপ্রয়োজনীয় বা কম গুরুত্বপূর্ণ শব্দ (স্টপ ওয়ার্ডস) ফিল্টার আউট করতে হয়। NLTK-এ এই অপ্রয়োজনীয় শব্দগুলোকেই স্টপ ওয়ার্ডস বলা হয়।

প্রয়োজনীয় লাইব্রেরি ইনস্টলেশন

প্রথমে nltk লাইব্রেরি ইন্সটল করুন:

$pip install nltk

এই স্টপ ওয়ার্ডস রিমুভ করলে ডাটাবেসে জায়গা বাঁচে এবং প্রসেসিং টাইম কমে। আপনি চাইলে নিজের স্টপ ওয়ার্ডস লিস্ট বানাতে পারেন। ডিফল্টভাবে NLTK-তে কিছু স্টপ ওয়ার্ডস থাকে, যা অ্যাক্সেস করা যায়:


>>> import nltk
>>> from nltk.corpus import stopwords

NLTK স্টপ ওয়ার্ডস লিস্ট


>>> set(stopwords.words('english'))
{'not', 'other', 'shan', "hadn't", 'she', 'did', 'through', 'and', 'does', "that'll", "weren't", 'your', "should've", "hasn't", 'myself', 'should', 'because', 'wasn', 'what', 'to', 'this', 'was', 'more', 'y', 'again', "needn't", 'into', 'above', 'themselves', 'd', "won't", 'during', 'haven', 'both', "shan't", 'their', 'on', 'hadn', 'up', 'once', 'its', 'against', 'before', 't', 'while', 'needn', 'doing', "don't", 'yourselves', 'until', 'is', 'all', 's', 'will', "you've", 'being', 'under', 'they', 'ours', "wouldn't", 'of', "didn", 'below', 'just', 'ma', 'yours', "you'll", 'mightn', 'where', 'are', 'that', 'those', 'most', 'them', 'if', 'you', "shouldn't", 'off', 'for', 'her', 'such', 'now', 'than', 're', 'no', 'm', 'or', "aren't", 'further', 'here', "wasn't", 'after', "haven't", 'my', 'himself', 'at', 'had', 'yourself', 'by', 'weren', 'only', 'have', 'we', 'do', 'same', "isn't", 'herself', 'll', 'down', 'then', 'why', 'own', 'him', 'so', 'having', 'nor', 'isn', 'few', 'how', 'each', 'there', 'with', 'couldn', 'about', 'very', 'am', 'me', "didn't", "doesn't", 'which', "she's", 'doesn', 'were', 'he', 'in', "mightn't", 'when', 'our', 'who', 'his', "couldn't", 'the', "you'd", 'be', 'hers', 'hasn', 'between', 'it', 'mustn', 'but', 'out', 'can', "wouldn't", 'ourselves', 'whom', 'been', 'these', 'aren', 'over', 'itself', 'a', 'i', 'too', 'theirs', 'some', "you're", 'as', 'won', "it's", 'from', 'o', 'don', 'any', 've', 'ain', 'has', 'an', "mustn't", 'shouldn'}

স্টপ ওয়ার্ডস রিমুভ করার সম্পূর্ণ প্রোগ্রাম


from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize

example_sent = "Python is a powerful high-level, object-oriented programming language created by Guido van Rossum." \
"It has simple easy-to-use syntax, making it the perfect language for someone trying to learn computer programming for the first time." \
"This is a comprehensive guide on how to get started in Python, why you should learn it and how you can learn it. However, if you knowledge " \
"of other programming languages and want to quickly get started with Python."

stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(example_sent)

filtered_sentence = [w for w in word_tokens if not w in stop_words]

# অথবা লুপ দিয়ে
filtered_sentence = []
for w in word_tokens:
    if w not in stop_words:
        filtered_sentence.append(w)

print(word_tokens)
print(filtered_sentence)

আউটপুট

ফিল্টার ছাড়া টেক্সট (স্টপ ওয়ার্ডস সহ)


['Python', 'is', 'a', 'powerful', 'high-level', ',', 'object-oriented', 'programming', 'language', 'created', 'by', 'Guido', 'van', 'Rossum.It', 'has', 'simple', 'easy-to-use', 'syntax', ',', 'making', 'it', 'the', 'perfect', 'language', 'for', 'someone', 'trying', 'to', 'learn', 'computer', 'programming', 'for', 'the', 'first', 'time.This', 'is', 'a', 'comprehensive', 'guide', 'on', 'how', 'to', 'get', 'started', 'in', 'Python', ',', 'why', 'you', 'should', 'learn', 'it', 'and', 'how', 'you', 'can', 'learn', 'it', '.', 'However', ',', 'if', 'you', 'knowledge', 'of', 'other', 'programming', 'languages', 'and', 'want', 'to', 'quickly', 'get', 'started', 'with', 'Python', '.']

ফিল্টার করা টেক্সট (স্টপ ওয়ার্ডস বাদ)


['Python', 'powerful', 'high-level', ',', 'object-oriented', 'programming', 'language', 'created', 'Guido', 'van', 'Rossum.It', 'simple', 'easy-to-use', 'syntax', ',', 'making', 'perfect', 'language', 'someone', 'trying', 'learn', 'computer', 'programming', 'first', 'time.This', 'comprehensive', 'guide', 'get', 'started', 'Python', ',', 'learn', 'learn', '.', 'However', ',', 'knowledge', 'programming', 'languages', 'want', 'quickly', 'get', 'started', 'Python', '.']
  1. পাইথনে স্ট্রিং স্প্লিট করে কমা দিয়ে জয়েন করার প্রোগ্রাম

  2. পাইথন প্রোগ্রাম - রেগুলার এক্সপ্রেশন ব্যবহার করে স্ট্রিং একটি সাবস্ট্রিং দিয়ে শুরু হয়েছে কিনা তা যাচাই করুন

  3. পাইথন ব্যবহার করে সর্বোচ্চ সম্ভাবনার পথ খুঁজে বের করার প্রোগ্রাম

  4. Python-এ pylab ব্যবহার করে একটি ফিগার রিমোটলি সেভ করার পদ্ধতি