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

Connected

exercițiu

使用您的子类

借助继承的强大能力,您基于父类 Document 创建了一个功能丰富的 SocialMedia 类。现在让我们来看一看其中的一些功能。

下面给出了 SocialMedia 的完整定义供参考。此外,SocialMedia 已添加到 __init__.py 中,方便直接使用。

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='@')

Instrucțiuni

100 XP
  • import 您的自定义包 text_analyzer。
  • 将 dc_tweets 定义为 SocialMedia 的一个实例,text 参数使用预加载的 datacamp_tweets 对象。
  • 使用合适的 dc_tweets 属性,print 数据中被提及最频繁的 5 个用户(most_common)。
  • 使用 text_analyzer 的 plot_counter() 方法,并结合合适的 dc_tweets 属性,绘制数据中最常用的话题标签。