summaryrefslogtreecommitdiff
path: root/python/cdec/sa/rule.pxi
diff options
context:
space:
mode:
authorChris Dyer <redpony@gmail.com>2014-09-17 12:39:47 -0400
committerChris Dyer <redpony@gmail.com>2014-09-17 12:39:47 -0400
commitc20decbd6b9be8f1a7d567cd72300b4e9582b55c (patch)
tree79c844304f2aa3f42b44236ba755200f4f87b7f6 /python/cdec/sa/rule.pxi
parent3822a2063e36b6ced948e5c22910a373c6c691b2 (diff)
parenta861980460220a31592bdb2e951a55faeebb8c6d (diff)
Merge pull request #51 from wilkeraziz/master
a few minor changes to pycdec
Diffstat (limited to 'python/cdec/sa/rule.pxi')
-rw-r--r--python/cdec/sa/rule.pxi19
1 files changed, 19 insertions, 0 deletions
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: