अपनी class की functionality का उपयोग
अब आपने अपनी Document class के __init__ मेथड में अतिरिक्त functionality जोड़ दी है, जो आपके users के लिए text को अपने-आप प्रोसेस करती है. इस अभ्यास में, आप उन्हीं users में से एक की तरह काम करेंगे ताकि अपनी मेहनत के फायदे देख सकें.
Document class (नीचे कॉपी की गई) आपके environment में लोड कर दी गई है (आपके नए अपडेट्स सहित).
class Document:
def __init__(self, text):
self.text = text
# pre tokenize the document with non-public tokenize method
self.tokens = self._tokenize()
# pre tokenize the document with non-public count_words
self.word_counts = self._count_words()
def _tokenize(self):
return tokenize(self.text)
# non-public method to tally document's word counts with Counter
def _count_words(self):
return Counter(self.tokens)
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Software Engineering Principles
अभ्यास निर्देश
- अपने environment में लोड किए गए
datacamp_tweetsडेटा सेट से एक नयाDocumentinstance बनाएँ.datacamp_tweetsऑब्जेक्ट एक सिंगल string है जिसमें DataCamp और उसके users द्वारा लिखे गए सैकड़ों tweets हैं. datacamp_docके पहले 5tokensप्रिंट करें.Document.__init__मेथड में non-public_count_words()मेथड द्वारा अपने-आप निकाले गए सबसे सामान्य 5 शब्द प्रिंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# create a new document instance from datacamp_tweets
datacamp_doc = ____(____)
# print the first 5 tokens from datacamp_doc
print(____.____[:5])
# print the top 5 most used words in datacamp_doc
print(____.____.most_common(5))