summaryrefslogtreecommitdiff
path: root/python/src/lattice.pxi
diff options
context:
space:
mode:
authorVictor Chahuneau <vchahune@cs.cmu.edu>2012-08-14 22:50:37 -0400
committerVictor Chahuneau <vchahune@cs.cmu.edu>2012-08-14 22:50:37 -0400
commit9c9213239263e8e8de2f154068cc3ad44e0c2100 (patch)
treea9ee2f722e4dc5705ae9f90f6fb3b67a278c5fd9 /python/src/lattice.pxi
parent0823824b5fa1504b6b2c48328aa8fc8468017cba (diff)
[cdec.sa] Explicit feature names in grammar extractor output
+ sparse features in extractor + hg.intersect(string) + basestring = str|unicode
Diffstat (limited to 'python/src/lattice.pxi')
-rw-r--r--python/src/lattice.pxi22
1 files changed, 17 insertions, 5 deletions
diff --git a/python/src/lattice.pxi b/python/src/lattice.pxi
index 385a40be..57e340d2 100644
--- a/python/src/lattice.pxi
+++ b/python/src/lattice.pxi
@@ -3,18 +3,20 @@ cimport lattice
cdef class Lattice:
cdef lattice.Lattice* lattice
- def __cinit__(self, inp):
+ def __cinit__(self):
+ self.lattice = new lattice.Lattice()
+
+ def __init__(self, inp):
if isinstance(inp, tuple):
- self.lattice = new lattice.Lattice(len(inp))
+ self.lattice.resize(len(inp))
for i, arcs in enumerate(inp):
self[i] = arcs
else:
if isinstance(inp, unicode):
inp = inp.encode('utf8')
if not isinstance(inp, str):
- raise TypeError('Cannot create lattice from %s' % type(inp))
- self.lattice = new lattice.Lattice()
- lattice.ConvertTextToLattice(string(<char *>inp), self.lattice)
+ raise TypeError('cannot create lattice from %s' % type(inp))
+ lattice.ConvertTextOrPLF(string(<char *>inp), self.lattice)
def __dealloc__(self):
del self.lattice
@@ -49,6 +51,9 @@ cdef class Lattice:
def __str__(self):
return str(hypergraph.AsPLF(self.lattice[0], True).c_str())
+ def __unicode__(self):
+ return unicode(str(self), 'utf8')
+
def __iter__(self):
cdef unsigned i
for i in range(len(self)):
@@ -65,3 +70,10 @@ cdef class Lattice:
yield '%d [shape=doublecircle]' % len(self)
yield '}'
return '\n'.join(lines()).encode('utf8')
+
+ def as_hypergraph(self):
+ cdef Hypergraph result = Hypergraph.__new__(Hypergraph)
+ result.hg = new hypergraph.Hypergraph()
+ cdef bytes plf = str(self)
+ hypergraph.ReadFromPLF(string(plf), result.hg)
+ return result