summaryrefslogtreecommitdiff
path: root/python/src/trule.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
commit06f90d83a1feafad301d365a4a437e44f68be45b (patch)
tree24128de1cb5a4767151f9380c46104a26121535d /python/src/trule.pxi
parentc4c9c2febd5af552ecddc215758e32b88013fbc7 (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/trule.pxi')
-rw-r--r--python/src/trule.pxi55
1 files changed, 0 insertions, 55 deletions
diff --git a/python/src/trule.pxi b/python/src/trule.pxi
deleted file mode 100644
index 6168014d..00000000
--- a/python/src/trule.pxi
+++ /dev/null
@@ -1,55 +0,0 @@
-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)