Skip to content

Instantly share code, notes, and snippets.

@brydavis
Last active June 4, 2019 20:14
Show Gist options
  • Save brydavis/455877a73cec7802786e3cf54bf31786 to your computer and use it in GitHub Desktop.
Save brydavis/455877a73cec7802786e3cf54bf31786 to your computer and use it in GitHub Desktop.
class Tokenizer:
"""Tokenize text"""
def __init__(self, text):
print('Start Tokenizer.__init__()')
self.tokens = text.split()
print('End Tokenizer.__init__()')
def describe(self):
return self.tokens
class WordCounter(Tokenizer):
"""Count words in text"""
def __init__(self, text):
print('Start WordCounter.__init__()')
super().__init__(text)
self.word_count = len(self.tokens)
print('End WordCounter.__init__()')
def describe(self):
return self.tokens, self.word_count
class Vocabulary(Tokenizer):
"""Find unique words in text"""
def __init__(self, text):
print('Start init Vocabulary.__init__()')
super().__init__(text)
self.vocab = set(self.tokens)
print('End init Vocabulary.__init__()')
def describe(self):
return self.tokens, self.vocab
class TextDescriber(WordCounter, Vocabulary):
"""Describe text with multiple metrics"""
def __init__(self, text):
print('Start init TextDescriber.__init__()')
super().__init__(text)
print('End init TextDescriber.__init__()')
def describe(self):
return self.tokens, self.vocab, self.word_count
td = TextDescriber('row row row your boat')
print('--------')
for d in td.describe():
print(d)
def text_describer(text):
tokens = text.split()
word_count = len(tokens)
vocab = set(tokens)
return tokens, vocab, word_count
tokens, vocab, word_count = text_describer('row row row your boat')
print('--------')
print(tokens)
print(vocab)
print(word_count)
@brydavis
Copy link
Author

brydavis commented Jun 4, 2019

premature optimization is the root of all evil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment