From bc2992ba96cd7af83da8522bdeb6e5dd94a5a11b Mon Sep 17 00:00:00 2001 From: Chris Dyer Date: Tue, 7 Aug 2012 23:22:44 -0400 Subject: sample trees from hypergraphs --- python/src/hypergraph.pxd | 4 ++++ python/src/hypergraph.pxi | 12 ++++++++++++ 2 files changed, 16 insertions(+) (limited to 'python/src') diff --git a/python/src/hypergraph.pxd b/python/src/hypergraph.pxd index 886660bf..abd6759c 100644 --- a/python/src/hypergraph.pxd +++ b/python/src/hypergraph.pxd @@ -76,6 +76,10 @@ cdef extern from "decoder/hg_sampler.h" namespace "HypergraphSampler": unsigned n, MT19937* rng, vector[Hypothesis]* hypos) + void sample_trees(Hypergraph& hg, + unsigned n, + MT19937* rng, + vector[string]* trees) cdef extern from "decoder/csplit.h" namespace "CompoundSplit": int GetFullWordEdgeIndex(Hypergraph& forest) diff --git a/python/src/hypergraph.pxi b/python/src/hypergraph.pxi index b210f440..62dd5bb1 100644 --- a/python/src/hypergraph.pxi +++ b/python/src/hypergraph.pxi @@ -85,6 +85,18 @@ cdef class Hypergraph: finally: del hypos + def sample_trees(self, unsigned n): + cdef vector[string]* trees = new vector[string]() + if self.rng == NULL: + self.rng = new MT19937() + hypergraph.sample_trees(self.hg[0], n, self.rng, trees) + cdef unsigned k + try: + for k in range(trees.size()): + yield unicode(trees[0][k].c_str(), 'utf8') + finally: + del trees + def intersect(self, Lattice lat): return hypergraph.Intersect(lat.lattice[0], self.hg) -- cgit v1.2.3