def __iter__(self): it = iter(self.rawIter()) (history, predicted), value = it.next() values = {predicted: value} for (h, p), v in it: if h == history: values[p] = values.get(p, 0) + v elif h > history: for predicted, value in sorted(values.iteritems()): yield (history, predicted), value history = h values = {p: v} else: raise ValueError('sequence not ordered', history, h) for predicted, value in sorted(values.iteritems()): yield (history, predicted), value
def __iter__(self): it = iter(self.rawIter()) (history, predicted), value = it.next() values = { predicted: value } for (h, p), v in it: if h == history: values[p] = values.get(p, 0) + v elif h > history: for predicted, value in sorted(values.iteritems()): yield (history, predicted), value history = h values = { p: v } else: raise ValueError('sequence not ordered', history, h) for predicted, value in sorted(values.iteritems()): yield (history, predicted), value
def get_free_vars(self): if not self.nested: return () free = self.frees | (self.uses - (self.globals | set(self.defs))) if not free: return () return sorted(list(free))
def testIter(self): sv = sparse(self.example) ref = sorted(self.example) for i, item in enumerate(sv): self.failUnlessEqual(item, ref[i]) sl = list(sv) self.failUnlessEqual(sl, ref)
def countsOfCounts(counts, group = None): histogram = {} counts = assertIsConsolidated(counts) for gram, count in counts: cat = group and group(count) or count try: histogram[cat] += 1 except KeyError: histogram[cat] = 1 result = sorted(histogram.items()) return result
def countsOfCounts(counts, group=None): histogram = {} counts = assertIsConsolidated(counts) for gram, count in counts: cat = group and group(count) or count try: histogram[cat] += 1 except KeyError: histogram[cat] = 1 result = sorted(histogram.items()) return result
def sizeTemplates(self): result = set() for i in range(1, self.size() + 1): left, right = self.symbol(i) result.add((len(left), len(right))) return sorted(result)
def dictAdd(self, a, b): result = dict(a) for k, v in b: result[k] = result.get(k, 0) + v return sorted(result.items())
def testThreshold(self): for t in [0.5, 1.0, 1.5, 2.0, 2.5]: sv = sparse(self.example).threshold(t) self.failUnlessEqual(list(sv), sorted(filter(lambda x: x[1]>=t, self.example)))
def joined(m): if m + 1 < M: return outerJoin(sorted(probabilities(m)), sorted(backOffs(m + 1))) else: return outerJoin(sorted(probabilities(m)), [])
def testThreshold(self): for t in [0.5, 1.0, 1.5, 2.0, 2.5]: sv = sparse(self.example).threshold(t) self.failUnlessEqual( list(sv), sorted(filter(lambda x: x[1] >= t, self.example)))
def testThreshold(self): for t in [0.5, 1.0, 1.5, 2.0, 2.5]: sv = sparse(self.example).threshold(t) self.assertEqual(list(sv), sorted([x for x in self.example if x[1] >= t]))
def joined(m): if m+1 < M: return outerJoin(sorted(probabilities(m)), sorted(backOffs(m+1))) else: return outerJoin(sorted(probabilities(m)), [])