1. Learn
  2. /
  3. कोर्स
  4. /
  5. Python में Software Engineering Principles

Connected

अभ्यास

चाइल्ड क्लास में फ़ंक्शनैलिटी जोड़ना

आपने अभी-अभी एक 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)

निर्देश 1/2

undefined XP
    1
    2
  • आपके सेशन में filter_word_counts() फंक्शन लोड किया गया है। इसकी सही उपयोगिता देखने के लिए help() का उपयोग करें।
  • _count_hashtags मेथड को filter_word_counts() का उपयोग करके पूरा करें ताकि केवल वे word_counts बचें जो # से शुरू होते हैं।