def sort_candidates(self, candidates):
        if len(candidates) == 0:
            return None
        # automatically creates nested dict when they don't exist
        cleaned = defaultdict(dict)
        for item, count in candidates.iteritems():
            cleaned[self.clean(item)][item] = count
        results = {}
        for item, options in cleaned.iteritems():
            selected_option, max_count, total_count = None, 0, 0
            for option, count in options.iteritems():
                total_count += count
                if count > max_count:
                    selected_option, max_count = option, count
            results[selected_option] = total_count

        results = sorted(results.iteritems(), key=itemgetter(1), reverse=True)
        total = sum(count for item, count in results)
        # trim to first 10 items
        return [(item, count/float(total)) for item, count in results][:10]
Takes a dict with frequencies {'a':2, 'b':4, 'c':1} and sorts them.
dataset/ETHPy150Open jcelliott/inquire/inquire/extraction/extractors.py/BaseExtractor.sort_candidates