1. Nauka
  2. /
  3. Kursy
  4. /
  5. Zasady inżynierii oprogramowania w Pythonie

Connected

ćwiczenie

Dodawanie funkcjonalności do klasy potomnej

Właśnie napisano klasę SocialMedia, która dziedziczy funkcjonalność po klasie Document. Na razie SocialMedia nie różni się niczym od Document. W tym ćwiczeniu rozbudujesz ją o funkcje przydatne przy pracy z danymi z mediów społecznościowych.

Definicja klasy Document znajduje się poniżej.

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)

Instrukcje 1/2

undefined XP
    1
    2
  • Funkcja filter_word_counts() jest już załadowana w sesji. Użyj help(), aby sprawdzić, jak jej poprawnie używać.
  • Uzupełnij metodę _count_hashtags za pomocą filter_word_counts() tak, aby w word_counts pozostały tylko słowa zaczynające się od #.