시작하기무료로 시작하기

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

방금 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)

이 연습은 강의의 일부입니다

Python으로 배우는 소프트웨어 공학 원칙

강의 보기

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Define a SocialMedia class that is a child of the `Document class`
class SocialMedia(Document):
    def __init__(self, text):
        Document.__init__(self, text)
        self.hashtag_counts = self._count_hashtags()
        
    def _count_hashtags(self):
        # Filter attribute so only words starting with '#' remain
        return ____
코드 편집 및 실행