1. Learn
  2. /
  3. Courses
  4. /
  5. Software Engineering Principles in Python

Connected

Exercise

Adding functionality to a child class

You've just written a SocialMedia class that inherits functionality from Document. As of now, the SocialMedia class doesn't have any functionality different from Document. In this exercise, you will build features into SocialMedia to specialize it for use with Social Media data.

For reference, the definition of Document can be seen below.

class Document:
    # Initialize a new Document instance
    def __init__(self, text):
        self.text = text
        # Pre tokenize the document with non-public tokenize method
        self.tokens = self._tokenize()
        # Pre tokenize the document with non-public count_words
        self.word_counts = self._count_words()

    def _tokenize(self):
        return tokenize(self.text)

    # Non-public method to tally document's word counts
    def _count_words(self):
        # Use collections.Counter to count the document's tokens
        return Counter(self.tokens)

Instructions 1/2

undefined XP
    1
    2
  • The function filter_word_counts() has been loaded in your session. Use help() to see its proper usage.
  • Finish the _count_hashtags method using filter_word_counts() so that only words_counts starting with # remain.