अपने 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_tweetsattribute का उपयोग करके डेटा में mention किए गए यूज़र्स के5most_commonपरिणामprintकरें. - सबसे अधिक उपयोग किए गए hashtags को plot करने के लिए
text_analyzerकेplot_counter()मेथड का उपयोग करें, और इसके लिए उपयुक्तdc_tweetsattribute पास करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
# 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.____)