चाइल्ड क्लास में फ़ंक्शनैलिटी जोड़ना
आपने अभी-अभी एक SocialMedia क्लास लिखी है जो Document से इनहेरिट करती है। अभी के लिए, SocialMedia क्लास में Document से अलग कोई फ़ंक्शनैलिटी नहीं है। इस अभ्यास में, आप SocialMedia में ऐसी फीचर्स जोड़ेंगे ताकि इसे सोशल मीडिया डेटा के साथ उपयोग के लिए विशेष रूप से तैयार किया जा सके।
संदर्भ के लिए, नीचे Document की डेफिनिशन दी गई है।
class Document:
# Initialize a new Document instance
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
def _count_words(self):
# Use collections.Counter to count the document's tokens
return Counter(self.tokens)
यह अभ्यास पाठ्यक्रम का हिस्सा है
Python में Software Engineering Principles
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# Define a SocialMedia class that is a child of the `Document class`
class SocialMedia(Document):
def __init__(self, text):
Document.__init__(self, text)
self.hashtag_counts = self._count_hashtags()
def _count_hashtags(self):
# Filter attribute so only words starting with '#' remain
return ____