From 501c0419200234f4ff6b8c3d9f9602596a96c28e Mon Sep 17 00:00:00 2001 From: Victor Chahuneau Date: Tue, 16 Oct 2012 01:07:11 -0400 Subject: [python] Fix string problems + Documentation. - use bytes instead of char* - add some basic docstrings to functions/constructors --- python/src/lattice.pxi | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'python/src/lattice.pxi') 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(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(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 -- cgit v1.2.3