From 9619c91e3eea2e5eb13d42e02d39730189ebc577 Mon Sep 17 00:00:00 2001 From: Victor Chahuneau Date: Sun, 1 Jul 2012 00:38:30 -0400 Subject: [python] MT evaluation --- python/src/hypergraph.pxi | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'python/src/hypergraph.pxi') diff --git a/python/src/hypergraph.pxi b/python/src/hypergraph.pxi index c226d105..ce78b729 100644 --- a/python/src/hypergraph.pxi +++ b/python/src/hypergraph.pxi @@ -53,6 +53,19 @@ cdef class Hypergraph: yield tree.decode('utf8') del derivations + def kbest_features(self, size): + cdef kb.KBestDerivations[FastSparseVector[weight_t], kb.FeatureVectorTraversal]* derivations = new kb.KBestDerivations[FastSparseVector[weight_t], kb.FeatureVectorTraversal](self.hg[0], size) + cdef kb.KBestDerivations[FastSparseVector[weight_t], kb.FeatureVectorTraversal].Derivation* derivation + cdef SparseVector fmap + cdef unsigned k + for k in range(size): + derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) + if not derivation: break + fmap = SparseVector() + fmap.vector = new FastSparseVector[weight_t](derivation._yield) + yield fmap + del derivations + def sample(self, unsigned n): cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() if self.rng == NULL: @@ -87,6 +100,6 @@ cdef class Hypergraph: elif isinstance(weights, DenseVector): self.hg.Reweight(( weights).vector[0]) else: - raise ValueError('cannot reweight hypergraph with %s' % type(weights)) + raise TypeError('cannot reweight hypergraph with %s' % type(weights)) # TODO get feature expectations, get partition function ("inside" score) -- cgit v1.2.3