Bag-of-Words प्रतिनिधित्व

Python में Natural Language Processing (NLP)

Fouad Trad

Machine Learning Engineer

NLP वर्कफ़्लो रिकैप

पूरा वर्कफ़्लो डायग्राम, जिसमें अध्याय 3 और 4 transformers लाइब्रेरीज़ पर केंद्रित हैं.

Python में Natural Language Processing (NLP)

Bag-of-Words (BoW)

  • टेक्स्ट को numbers में बदलने की बुनियादी तकनीक
  • हर शब्द कितनी बार आता है, गिनकर टेक्स्ट दर्शाएँ
  • शब्दों को एक बैग में डालें और गिनें
  • व्याकरण और क्रम नज़रअंदाज़ करता है

एक इमेज जिसमें टेक्स्ट के शब्द बैग में डाले जाते हैं और फिर हर शब्द की आवृत्ति गिनी जाती है.

Python में Natural Language Processing (NLP)

BoW उदाहरण

दो वाक्य दिखाने वाली इमेज: 'I love NLP' और 'I love machine learning.'

Python में Natural Language Processing (NLP)

BoW उदाहरण

वाक्यों से निकले vocabulary को दिखाने वाली इमेज: I, love, NLP, machine, और learning.

  • सभी यूनिक शब्दों की vocabulary बनाएँ
Python में Natural Language Processing (NLP)

BoW उदाहरण

हर वाक्य के लिए फीचर वेक्टर दिखाने वाली इमेज, जहाँ परिभाषित vocabulary के अनुसार शब्द-गणना की गई है.

  • सभी यूनिक शब्दों की vocabulary बनाएँ
  • vocabulary के हर शब्द की आवृत्ति गिनें
Python में Natural Language Processing (NLP)

कोड के साथ BoW

reviews = ["I loved the movie. It was amazing!",
           "The movie was okay.",
           "I hated the movie. It was boring."]

def preprocess(text):
text = text.lower()
tokens = word_tokenize(text)
tokens = [word for word in tokens if word not in string.punctuation]
return " ".join(tokens)
cleaned_reviews = [preprocess(review) for review in reviews]
print(cleaned_reviews)
['i loved the movie it was amazing', 
 'the movie was okay', 
 'i hated the movie it was boring']
Python में Natural Language Processing (NLP)

कोड के साथ BoW

from sklearn.feature_extraction.text import CountVectorizer


vectorizer = CountVectorizer()
vectorizer.fit(cleaned_reviews)
print(vectorizer.get_feature_names_out())
['amazing' 'boring' 'hated' 'it' 'loved' 'movie' 'okay' 'the' 'was']
Python में Natural Language Processing (NLP)

BoW आउटपुट

X = vectorizer.transform(cleaned_reviews)

# OR X = vectorizer.fit_transform(cleaned_reviews)
print(X)
<Compressed Sparse Row sparse matrix of dtype 'int64'
    with 16 stored elements and shape (3, 9)>

Sparse matrix: टेबल का ज़्यादातर हिस्सा zeros से भरा होता है

Python में Natural Language Processing (NLP)

BoW आउटपुट

print(X.toarray())
[[1 0 0 1 1 1 0 1 1]
 [0 0 0 0 0 1 1 1 1]
 [0 1 1 1 0 1 0 1 1]]
print(vectorizer.get_feature_names_out())
['amazing' 'boring' 'hated' 'it' 'loved' 'movie' 'okay' 'the' 'was']
Python में Natural Language Processing (NLP)

शब्द फ़्रीक्वेंसी

import numpy as np

word_counts = np.sum(X.toarray(), axis=0)
words = vectorizer.get_feature_names_out()
import matplotlib.pyplot as plt

plt.bar(words, word_counts)
plt.title("Word Frequencies in Movie Reviews")
plt.xlabel("Words") plt.ylabel("Frequency") plt.show()

बार प्लॉट जो शब्द और उनकी फ़्रीक्वेंसी दिखाता है, जहाँ stop words सबसे अधिक हैं.

Python में Natural Language Processing (NLP)

अभ्यास करते हैं!

Python में Natural Language Processing (NLP)

Preparing Video For Download...