def analyze(self, filename=None, text=None): # analyze a new document using the stored values # if there is a filename given, create a new Document object if filename != None: doc = Document(None, filename) words = doc.tokenize() # otherwise, analyze the given text elif text != None: words = Util.tokenize(text) # if both are None, return error else: print "Analyzer requires a filename or text to analyze. Please try again." return # store dict of log value sums log_sums = {} # for every heuristic... for key in self.log_values: # initialize a value to 0 current_sum = 0.0 # iterate over words for word in words: current_sum += self.log_values[key].get(word) # store new sum log_sums[key] = current_sum # calculate largest log sum; this can be improved by doing this inside above loop # for clarity, we will add an extra loop here # track largest sum largest = -1.0 # track largest key largest_heuristic = "" # iterate through all the keys for key in log_sums: # if the new value is larger... if log_sums[key] > largest: # update values largest = log_sums[key] largest_heuristic = key # return best key return largest_heuristic