# 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 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 and size self.words = words[:] # Creates a copy of the list self.size = len(self.words) def get_count(self, word): """Return the count of the given word""" return self.words.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_with_words = [(self.get_count(w),w) for w in set(self.words)] scores_with_words.sort(reverse=True) return scores_with_words[0:k] def total_words(self): """Return the total number of words in the file""" return 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=5 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)