summaryrefslogtreecommitdiff
path: root/python/src/hypergraph.pxi
diff options
context:
space:
mode:
authorVictor Chahuneau <vchahune@cs.cmu.edu>2012-07-21 01:22:53 -0400
committerVictor Chahuneau <vchahune@cs.cmu.edu>2012-07-21 01:22:53 -0400
commit9a5e10322c82916a3c3fdfa0489ed1999bc988c5 (patch)
tree09de995eae349427d640ed3e392a75e0b9823461 /python/src/hypergraph.pxi
parent5b47c6aa65cd73e208dd941ec75229af5f9c0c67 (diff)
[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)
Diffstat (limited to 'python/src/hypergraph.pxi')
-rw-r--r--python/src/hypergraph.pxi28
1 files changed, 21 insertions, 7 deletions
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):