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
import
yourtext_analyzer
custom package.- Define
dc_tweets
as an instance ofSocialMedia
with the preloadeddatacamp_tweets
object as thetext
. print
the5
most_common
mentioned users in the data using the appropriatedc_tweets
attribute.- Use
text_analyzer
'splot_counter()
method to plot the most used hashtags in the data using the appropriatedc_tweets
attribute.
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.____)