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

Connected

अभ्यास

अपने child class का उपयोग

Inheritance की ताकत की बदौलत आप अपने parent Document पर आधारित एक फीचर-समृद्ध SocialMedia class बना पाए. आइए, इन फीचर्स में से कुछ को कार्रवाई में देखते हैं.

संदर्भ के लिए नीचे SocialMedia की पूरी परिभाषा दी गई है. साथ ही, उपयोग में आसानी के लिए SocialMedia को __init__.py में जोड़ दिया गया है.

class SocialMedia(Document):
    def __init__(self, text):
        Document.__init__(self, text)
        self.hashtag_counts = self._count_hashtags()
        self.mention_counts = self._count_mentions()

    def _count_hashtags(self):
        # Filter attribute so only words starting with '#' remain
        return filter_word_counts(self.word_counts, first_char='#')      

    def _count_mentions(self):
        # Filter attribute so only words starting with '@' remain
        return filter_word_counts(self.word_counts, first_char='@')

निर्देश

100 XP
  • अपने कस्टम पैकेज text_analyzer को import करें.
  • प्रीलोडेड datacamp_tweets ऑब्जेक्ट को text के रूप में देकर SocialMedia का एक इंस्टेंस बनाइए और उसे dc_tweets नाम दीजिए.
  • उचित dc_tweets attribute का उपयोग करके डेटा में mention किए गए यूज़र्स के 5 most_common परिणाम print करें.
  • सबसे अधिक उपयोग किए गए hashtags को plot करने के लिए text_analyzer के plot_counter() मेथड का उपयोग करें, और इसके लिए उपयुक्त dc_tweets attribute पास करें.