Using your child class
Thanks to the power of inheritance you were able to create a feature-rich, SocialMedia class based on its parent, Document. Let's see some of these features in action.
Below is the full definition of SocialMedia for reference. Additionally, SocialMedia has been added to __init__.py for ease of use.
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='@')
This exercise is part of the course
Software Engineering Principles in Python
Exercise instructions
importyourtext_analyzercustom package.- Define
dc_tweetsas an instance ofSocialMediawith the preloadeddatacamp_tweetsobject as thetext. printthe5most_commonmentioned users in the data using the appropriatedc_tweetsattribute.- Use
text_analyzer'splot_counter()method to plot the most used hashtags in the data using the appropriatedc_tweetsattribute.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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.____)