Začněte nyníZačněte zdarma

Přidání funkcionality do podřízené třídy

Právě jsi napsal/a třídu SocialMedia, která dědí funkcionalitu od třídy Document. Zatím se SocialMedia nijak neliší od Document. V tomto cvičení do ní přidáš vlastnosti, které ji přizpůsobí pro práci s daty ze sociálních sítí.

Pro přehled se níže nachází definice třídy 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)

Toto cvičení je součástí kurzu

Principy softwarového inženýrství v Pythonu

Zobrazit kurz

Interaktivní cvičení na vyzkoušení si v praxi

Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.

# 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 ____
Upravit a spustit kód