summaryrefslogtreecommitdiff
path: root/python/src/lattice.pxi
diff options
context:
space:
mode:
authorChris Dyer <cdyer@cab.ark.cs.cmu.edu>2012-10-16 01:43:01 -0400
committerChris Dyer <cdyer@cab.ark.cs.cmu.edu>2012-10-16 01:43:01 -0400
commitdaaa8aaed96f23d74d86b77e436883cd7c451717 (patch)
treeb8542a6462748527bd2f795d00478915b4bbee46 /python/src/lattice.pxi
parentb98f7e8de2c35937c227236ddb986df4879edb5f (diff)
parent62bf03f0ec79b658c9208f0ae8976397e64a7dee (diff)
Merge branch 'master' of github.com:redpony/cdec
Diffstat (limited to 'python/src/lattice.pxi')
-rw-r--r--python/src/lattice.pxi19
1 files changed, 10 insertions, 9 deletions
diff --git a/python/src/lattice.pxi b/python/src/lattice.pxi
index 57e340d2..8000b61e 100644
--- a/python/src/lattice.pxi
+++ b/python/src/lattice.pxi
@@ -7,16 +7,16 @@ cdef class Lattice:
self.lattice = new lattice.Lattice()
def __init__(self, inp):
+ """Lattice(tuple) -> Lattice from node list.
+ Lattice(string) -> Lattice from PLF representation."""
if isinstance(inp, tuple):
self.lattice.resize(len(inp))
for i, arcs in enumerate(inp):
self[i] = arcs
+ elif isinstance(inp, basestring):
+ lattice.ConvertTextOrPLF(as_str(inp), self.lattice)
else:
- if isinstance(inp, unicode):
- inp = inp.encode('utf8')
- if not isinstance(inp, str):
- raise TypeError('cannot create lattice from %s' % type(inp))
- lattice.ConvertTextOrPLF(string(<char *>inp), self.lattice)
+ raise TypeError('cannot create lattice from %s' % type(inp))
def __dealloc__(self):
del self.lattice
@@ -39,9 +39,8 @@ cdef class Lattice:
raise IndexError('lattice index out of range')
cdef lattice.LatticeArc* arc
for (label, cost, dist2next) in arcs:
- if isinstance(label, unicode):
- label = label.encode('utf8')
- arc = new lattice.LatticeArc(TDConvert(<char *>label), cost, dist2next)
+ label_str = as_str(label)
+ arc = new lattice.LatticeArc(TDConvert(label_str), cost, dist2next)
self.lattice[0][index].push_back(arc[0])
del arc
@@ -60,6 +59,7 @@ cdef class Lattice:
yield self[i]
def todot(self):
+ """lattice.todot() -> Representation of the lattice in GraphViz dot format."""
def lines():
yield 'digraph lattice {'
yield 'rankdir = LR;'
@@ -72,8 +72,9 @@ cdef class Lattice:
return '\n'.join(lines()).encode('utf8')
def as_hypergraph(self):
+ """lattice.as_hypergraph() -> Hypergraph representation of the lattice."""
cdef Hypergraph result = Hypergraph.__new__(Hypergraph)
result.hg = new hypergraph.Hypergraph()
cdef bytes plf = str(self)
- hypergraph.ReadFromPLF(string(plf), result.hg)
+ hypergraph.ReadFromPLF(plf, result.hg)
return result