diff options
author | Patrick Simianer <simianer@cl.uni-heidelberg.de> | 2012-07-08 14:26:51 +0200 |
---|---|---|
committer | Patrick Simianer <simianer@cl.uni-heidelberg.de> | 2012-07-08 14:26:51 +0200 |
commit | c139ce495861bb341e1b86a85ad4559f9ad53c14 (patch) | |
tree | 1071839ee458f21f169ce06fc536fefe07e4c65d /python/src/hypergraph.pxi | |
parent | 3a94ac22e5c60aa205f2b3dadf81b0666500e0c3 (diff) | |
parent | d01e5b66d3010d61b9b56301fd7f302dd4ea5bc8 (diff) |
Merge branch 'master' of github.com:pks/cdec-dtrain
Diffstat (limited to 'python/src/hypergraph.pxi')
-rw-r--r-- | python/src/hypergraph.pxi | 59 |
1 files changed, 41 insertions, 18 deletions
diff --git a/python/src/hypergraph.pxi b/python/src/hypergraph.pxi index c226d105..9d09722e 100644 --- a/python/src/hypergraph.pxi +++ b/python/src/hypergraph.pxi @@ -32,26 +32,45 @@ cdef class Hypergraph: def kbest(self, size): cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal](self.hg[0], size) cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal].Derivation* derivation - cdef str sentence + cdef bytes sentence cdef unsigned k - for k in range(size): - derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) - if not derivation: break - sentence = GetString(derivation._yield).c_str() - yield sentence.decode('utf8') - del derivations + try: + for k in range(size): + derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) + if not derivation: break + sentence = GetString(derivation._yield).c_str() + yield sentence.decode('utf8') + finally: + del derivations def kbest_tree(self, size): cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ETreeTraversal](self.hg[0], size) cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal].Derivation* derivation cdef str tree cdef unsigned k - for k in range(size): - derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) - if not derivation: break - tree = GetString(derivation._yield).c_str() - yield tree.decode('utf8') - del derivations + try: + for k in range(size): + derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) + if not derivation: break + tree = GetString(derivation._yield).c_str() + yield tree.decode('utf8') + finally: + 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 + try: + 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 + finally: + del derivations def sample(self, unsigned n): cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() @@ -60,10 +79,12 @@ cdef class Hypergraph: hypergraph.sample_hypotheses(self.hg[0], n, self.rng, hypos) cdef str sentence cdef unsigned k - for k in range(hypos.size()): - sentence = GetString(hypos[0][k].words).c_str() - yield sentence.decode('utf8') - del hypos + try: + for k in range(hypos.size()): + sentence = GetString(hypos[0][k].words).c_str() + yield sentence.decode('utf8') + finally: + del hypos # TODO richer k-best/sample output (feature vectors, trees?) @@ -76,6 +97,8 @@ cdef class Hypergraph: preserve_mask = new hypergraph.EdgeMask(self.hg.edges_.size()) preserve_mask[0][hypergraph.GetFullWordEdgeIndex(self.hg[0])] = True self.hg.PruneInsideOutside(beam_alpha, density, preserve_mask, False, 1, False) + if preserve_mask: + del preserve_mask def lattice(self): # TODO direct hg -> lattice conversion in cdec cdef str plf = hypergraph.AsPLF(self.hg[0], True).c_str() @@ -87,6 +110,6 @@ cdef class Hypergraph: elif isinstance(weights, DenseVector): self.hg.Reweight((<DenseVector> 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) |