1. 学ぶ
  2. /
  3. コース
  4. /
  5. Pythonで学ぶSoftware Engineeringの原則

Connected

演習

子クラスに機能を追加する

あなたは、Document から機能を継承する SocialMedia クラスを書いたところです。現時点では、SocialMedia クラスは Document と違う機能を持っていません。この演習では、ソーシャルメディアのデータで使いやすいように、SocialMedia に機能を追加して特化させます。

参考として、Document の定義を以下に示します。

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)

指示1 / 2

undefined XP
    1
    2
  • 関数 filter_word_counts() はセッションに読み込まれています。正しい使い方は help() で確認してください。
  • _count_hashtags メソッドを filter_word_counts() を使って完成させ、# で始まる単語だけが残るようにしてください。