def probabilities(self): t = sum(itervalues(self.__weights)) return dict((e, float(w) / t) for e, w in iteritems(self.__weights))
def sorted(self): return sorted(iteritems(self.__weights), key=lambda w: (-w[1], w[0]))
def mergedWith(self, other): weights = dict(self.__weights) for element, weight in iteritems(other.__weights): weights[element] = weights.get(element, 0) + weight return SoftElement(weights)
def pairs(self): return iteritems(self.__weights)
def __call__(self, firstElement, secondElement): score = 0.0 for a, p in iteritems(firstElement.probabilities()): for b, q in iteritems(secondElement.probabilities()): score += p * q * self.scoring(a, b) return score