summaryrefslogtreecommitdiff
path: root/python/src/lattice.pxi
diff options
context:
space:
mode:
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