# Class Example - WordCounts # Internal Data Structure: list of words # # Many extra comments added for explanation purposes. # Not necessarily an example of good style. # Modified to take a list of words instead of filename as input # to the constructor so this code can be cut and pasted into # the Python Tutor. class WordCounts: """Represents the words in a file.""" # Internal representation: # variable words_list is a list of the words in the file def __init__(self, words): """Create a WordCounts object from the given word list""" # This is a constructor, gets called when we do: # var_name = WordCounts(list_of_words) # WordCount objects will have two data attributes (aka "fields") # These are created here: words_list and size self.words_list = words[:] # Creates a copy of the list self.size = len(self.words_list) def get_count(self, word): """Return the count of the given word""" return self.words_list.count(word) # count is a function on a list def topk(self, k=10): """Return a list of the top k most frequent words in order""" scores_and_words = [(self.get_count(w), w) for w in set(self.words_list)] scores_and_words.sort(reverse=True) return scores_and_words[0:k] def total_words(self): """Return the total number of words in the file""" return self.size # --------------------------------------------------------- # Sample client program # # The EXACT same sample program is used with both versions # of the WordCount class because the client interface is # the same for both versions of the classes. words = ["the", "lazy", "brown", "fox", "fox", "the", "the"] words2 = ["the", "lazy", "brown", "fox", "fox", "hippo", "hippo", "hippo"] # Create two instances of the class WordCount # wc and wc2 are both objects of type WordCount wc = WordCounts(words) wc2 = WordCounts(words2) # print("type(wc) is", type(wc)) # print("type(wc2) is", type(wc2)) # print("type(WordCounts) is:", type(WordCounts)) k = 2 print("wc top", k, ":", wc.topk(k)) print("wc2 top", k, ":", wc2.topk(k)) word = "the" print("wc of", word, ":", wc.get_count(word)) print("wc2 of", word, ":", wc2.get_count(word)) # A few more commands: # List attributes of the class # print("dir on WordCounts class:", dir(WordCounts)) # List attributes of an object # print("dir on wc object:", dir(wc)) # Get docstring for the class and all its methods # help(WordCounts) # help(dir)