Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Python

पायथन में NLTK का उपयोग करके स्टॉप वर्ड्स के साथ पार्ट ऑफ स्पीच टैगिंग


प्राकृतिक भाषा प्रसंस्करण के पीछे मुख्य विचार यह है कि मशीन कम से कम कुछ स्तर पर मानव हस्तक्षेप के बिना विश्लेषण या प्रसंस्करण का कुछ रूप कर सकती है जैसे टेक्स्ट के कुछ हिस्से को समझना या यह कहने की कोशिश करना कि टेक्स्ट क्या कहना चाहता है।

टेक्स्ट को प्रोसेस करने की कोशिश करते समय, कंप्यूटर को टेक्स्ट से बेकार या कम महत्वपूर्ण डेटा (शब्दों) को फ़िल्टर करने की आवश्यकता होती है। NLTK में, बेकार शब्द (डेटा) को स्टॉप वर्ड्स कहा जाता है।

आवश्यक लाइब्रेरी इंस्टॉल करना

सबसे पहले आपको nltk लाइब्रेरी की आवश्यकता है, बस अपने टर्मिनल में नीचे कमांड चलाएं:

$pip install nltk

तो हम इन स्टॉप वर्ड्स को हटाने जा रहे हैं, ताकि वे हमारे डेटाबेस में जगह न लें या मूल्यवान प्रसंस्करण समय न लें।

आप उन शब्दों की अपनी सूची बना सकते हैं जिन्हें आप स्टॉप वर्ड्स मान सकते हैं। डिफ़ॉल्ट रूप से, 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", '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. - पायथन प्रोग्राम - रेगेक्स का उपयोग करके जांचें कि स्ट्रिंग किसी सबस्ट्रिंग से शुरू होती है या नहीं

    जब यह जांचना हो कि कोई स्ट्रिंग किसी विशिष्ट सबस्ट्रिंग से शुरू होती है या नहीं, नियमित अभिव्यक्ति की सहायता से, एक विधि परिभाषित की जाती है जो स्ट्रिंग के माध्यम से पुनरावृति करती है और search विधि का उपयोग करके जांचती है कि कोई स्ट्रिंग किसी विशिष्ट सबस्ट्रिंग से शुरू होती है या नहीं। उदाहरण नीचे

  1. पायथन का उपयोग करके अधिकतम संभावना वाले पथ को खोजने का प्रोग्राम पायथन का उपयोग करके अधिकतम संभावना वाले पथ को खोजने का प्रोग्राम

    मान लीजिए हमारे पास n नोड्स वाला एक अनिर्देशित भारित ग्राफ है (नोड्स 0 से क्रमांकित हैं)। यह ग्राफ एज लिस्ट के रूप में दिया गया है, प्रत्येक एज e के लिए, उस एज को पार करने की सफलता की संभावना probability[e] है। हमारे पास प्रारंभ और अंत नोड्स भी हैं, हमें प्रारंभ से अंत तक जाने के लिए अधिकतम सफलता स

  1. Python में pylab का उपयोग करके चित्र को दूरस्थ रूप से कैसे सहेजें? Python में pylab का उपयोग करके चित्र को दूरस्थ रूप से कैसे सहेजें?

    pyplot पैकेज के savefig मेथड का उपयोग करके, हम चित्र को दूरस्थ रूप से सहेज सकते हैं, बस चित्र का स्थान निर्दिष्ट करके। चरण अलग बैकएंड का उपयोग करने के लिए, matplotlib.use(Agg) मेथड का उपयोग करके सेट करें। plot() मेथड का उपयोग करके लाइन प्लॉट करें। savefig() मेथड का उपयोग करके, हम छवि को दूरस्थ रूप