From a861980460220a31592bdb2e951a55faeebb8c6d Mon Sep 17 00:00:00 2001 From: Wilker Aziz Date: Wed, 17 Sep 2014 15:54:42 +0100 Subject: scaling weights, return dot and fmap from sampling, iterate over the terminal spans in a rule --- python/cdec/sa/rule.pxi | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'python/cdec/sa/rule.pxi') diff --git a/python/cdec/sa/rule.pxi b/python/cdec/sa/rule.pxi index 7fde3e06..af8fabea 100644 --- a/python/cdec/sa/rule.pxi +++ b/python/cdec/sa/rule.pxi @@ -154,6 +154,25 @@ cdef class Phrase: property words: def __get__(self): return [sym_tostring(w) for w in self if not sym_isvar(w)] + + def iterspans(self, nt_flag = False, to_str = True): + """iterate over (terminal and nonterminal) spans, it generates pairs of the kind (terminal span, !nt_flag) or (nonterminal, nt_flag) + Example: iterspans(False, False) --> ([-1], False), ([2,3], True), ([-1], False), ([4], True), ([-1], False) + for the sequence -1 2 3 -1 4 -1""" + span = [] + cdef int i, s + get_sym = lambda sym : sym_tostring(sym) if to_str else lambda sym : sym + for i from 0 <= i < self.n: + s = self.syms[i] + if sym_isvar(s): + if len(span): + yield (span, not nt_flag) + span = [] + yield ([get_sym(s)], nt_flag) + else: + span.append(get_sym(s)) + if len(span): + yield (span, not nt_flag) cdef class Rule: -- cgit v1.2.3