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

Connected

道练习

使用您类的功能

您已经在 Document 类的 __init__ 方法中添加了额外功能,可以为用户自动预处理文本。在本练习中,您将扮演其中一位用户,来体会这项工作的价值。

Document 类(如下所示)已加载到您的环境中(包含您最新的更新)。

class Document:
  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 with Counter
  def _count_words(self):
    return Counter(self.tokens)

说明

100 XP
  • 使用已加载到您环境中的 datacamp_tweets 数据集创建一个新的 Document 实例。datacamp_tweets 对象是一个包含数百条由 DataCamp 与其用户撰写的推文的长字符串。
  • 打印 datacamp_doc 的前 5 个 tokens。
  • 打印在 Document.__init__ 方法中由非公开方法 _count_words() 自动计算得到的最常见的前 5 个单词。