summaryrefslogtreecommitdiff
path: root/python/src/trule.pxi
diff options
context:
space:
mode:
Diffstat (limited to 'python/src/trule.pxi')
-rw-r--r--python/src/trule.pxi55
1 files changed, 55 insertions, 0 deletions
diff --git a/python/src/trule.pxi b/python/src/trule.pxi
new file mode 100644
index 00000000..6168014d
--- /dev/null
+++ b/python/src/trule.pxi
@@ -0,0 +1,55 @@
+def _phrase(phrase):
+ return ' '.join('[%s,%d]' % w if isinstance(w, tuple) else w.encode('utf8') for w in phrase)
+
+cdef class TRule:
+ cdef hypergraph.TRule* rule
+
+ property arity:
+ def __get__(self):
+ return self.rule.arity_
+
+ property f:
+ def __get__(self):
+ cdef vector[WordID]* f = &self.rule.f_
+ cdef WordID w
+ cdef words = []
+ cdef unsigned i
+ cdef int idx = 0
+ for i in range(f.size()):
+ w = f[0][i]
+ if w < 0:
+ idx += 1
+ words.append((TDConvert(-w), idx))
+ else:
+ words.append(unicode(TDConvert(w), encoding='utf8'))
+ return words
+
+ property e:
+ def __get__(self):
+ cdef vector[WordID]* e = &self.rule.e_
+ cdef WordID w
+ cdef words = []
+ cdef unsigned i
+ cdef int idx = 0
+ for i in range(e.size()):
+ w = e[0][i]
+ if w < 1:
+ idx += 1
+ words.append((TDConvert(1-w), idx))
+ else:
+ words.append(unicode(TDConvert(w), encoding='utf8'))
+ return words
+
+ property scores:
+ def __get__(self):
+ cdef SparseVector scores = SparseVector()
+ scores.vector = new FastSparseVector[double](self.rule.scores_)
+ return scores
+
+ property lhs:
+ def __get__(self):
+ return TDConvert(-self.rule.lhs_)
+
+ def __str__(self):
+ scores = ' '.join('%s=%s' % feat for feat in self.scores)
+ return '[%s] ||| %s ||| %s ||| %s' % (self.lhs, _phrase(self.f), _phrase(self.e), scores)