From 9a5e10322c82916a3c3fdfa0489ed1999bc988c5 Mon Sep 17 00:00:00 2001 From: Victor Chahuneau Date: Sat, 21 Jul 2012 01:22:53 -0400 Subject: [python] Support for grammars - Translation rules can now be create programatically - Grammars = list of translation rules can be used for translation - Feature expectations on the hypergraph (inside_outside) --- python/src/hypergraph.pxi | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'python/src/hypergraph.pxi') diff --git a/python/src/hypergraph.pxi b/python/src/hypergraph.pxi index 2e2c04a2..86751cc9 100644 --- a/python/src/hypergraph.pxi +++ b/python/src/hypergraph.pxi @@ -109,8 +109,6 @@ cdef class Hypergraph: else: raise TypeError('cannot reweight hypergraph with %s' % type(weights)) - # TODO get feature expectations, get partition function ("inside" score) - property edges: def __get__(self): cdef unsigned i @@ -127,19 +125,35 @@ cdef class Hypergraph: def __get__(self): return HypergraphNode().init(self.hg, self.hg.GoalNode()) - -include "trule.pxi" + property npaths: + def __get__(self): + return self.hg.NumberOfPaths() + + def inside_outside(self): + cdef FastSparseVector[LogVal[double]]* result = new FastSparseVector[LogVal[double]]() + cdef LogVal[double] z = hypergraph.InsideOutside(self.hg[0], result) + result[0] /= z + cdef SparseVector vector = SparseVector() + vector.vector = new FastSparseVector[double]() + cdef FastSparseVector[LogVal[double]].const_iterator* it = new FastSparseVector[LogVal[double]].const_iterator(result[0], False) + cdef unsigned i + for i in range(result.size()): + vector.vector.set_value(it[0].ptr().first, log(it[0].ptr().second)) + pinc(it[0]) # ++it + del it + del result + return vector cdef class HypergraphEdge: cdef hypergraph.Hypergraph* hg cdef hypergraph.HypergraphEdge* edge - cdef public TRule trule + cdef public BaseTRule trule cdef init(self, hypergraph.Hypergraph* hg, unsigned i): self.hg = hg self.edge = &hg.edges_[i] - self.trule = TRule() - self.trule.rule = self.edge.rule_.get() + self.trule = BaseTRule() + self.trule.rule = new shared_ptr[grammar.TRule](self.edge.rule_) return self def __len__(self): -- cgit v1.2.3