1. เรียนรู้
  2. /
  3. Courses
  4. /
  5. Python으로 배우는 소프트웨어 공학 원칙

Connected

Exercises

자식 클래스에 기능 추가하기

방금 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()로 완성하여 #로 시작하는 단어만 남도록 하세요.