शुरू करेंमुफ़्त में शुरू करें

अपने 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='@')

यह अभ्यास पाठ्यक्रम का हिस्सा है

Python में Software Engineering Principles

पाठ्यक्रम देखें

अभ्यास निर्देश

  • अपने कस्टम पैकेज 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 पास करें.

इंटरैक्टिव व्यावहारिक अभ्यास

इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।

# Import custom text_analyzer package
import ____

# Create a SocialMedia instance with datacamp_tweets
dc_tweets = ____(text=datacamp_tweets)

# Print the top five most mentioned users
print(dc_tweets.____.most_common(5))

# Plot the most used hashtags
text_analyzer.____(dc_tweets.____)
कोड संपादित करें और चलाएँ