Exemple #1
0
 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
Exemple #2
0
    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
Exemple #3
0
 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)
Exemple #5
0
    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)
Exemple #6
0
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
Exemple #7
0
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
Exemple #8
0
 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)), [])
Exemple #12
0
 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)
Exemple #13
0
 def dictAdd(self, a, b):
     result = dict(a)
     for k, v in b:
         result[k] = result.get(k, 0) + v
     return sorted(result.items())
Exemple #14
0
 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)), [])