1. Learn
  2. /
  3. Cursuri
  4. /
  5. Python 中的软件工程原理

Connected

exercițiu

为子类添加功能

您刚刚编写了一个继承自 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)

Instrucțiuni 1/2

undefined XP
    1
    2
  • 会话中已加载函数 filter_word_counts()。请使用 help() 查看其正确用法。
  • 使用 filter_word_counts() 完成 _count_hashtags 方法,使只保留以 # 开头的 word_counts。