diff options
author | Victor Chahuneau <vchahune@cs.cmu.edu> | 2012-06-23 11:59:48 -0400 |
---|---|---|
committer | Victor Chahuneau <vchahune@cs.cmu.edu> | 2012-06-23 11:59:48 -0400 |
commit | b738e349be490c24d3604c224f44fc54e16d3d7b (patch) | |
tree | 5d435257ef3c0023daa2211eb7260c470dbb6cdc /python/src | |
parent | 0b27ea3f91d0ad2f2ed718839d308db3d1baf5ae (diff) |
Support for sparse/dense vectors in the python extension
- SparseVector, DenseVector
- improved Lattice
- Lattice translation
- Hypergraph reweighting, pruning
Diffstat (limited to 'python/src')
-rw-r--r-- | python/src/_cdec.cpp | 6687 | ||||
-rw-r--r-- | python/src/_cdec.pyx | 135 | ||||
-rw-r--r-- | python/src/decoder.pxd | 12 | ||||
-rw-r--r-- | python/src/hypergraph.pxd | 68 | ||||
-rw-r--r-- | python/src/hypergraph.pxi | 92 | ||||
-rw-r--r-- | python/src/kbest.pxd | 6 | ||||
-rw-r--r-- | python/src/lattice.pxd | 9 | ||||
-rw-r--r-- | python/src/lattice.pxi | 56 | ||||
-rw-r--r-- | python/src/utils.pxd | 55 | ||||
-rw-r--r-- | python/src/vectors.pxi | 101 |
10 files changed, 5378 insertions, 1843 deletions
diff --git a/python/src/_cdec.cpp b/python/src/_cdec.cpp index 55470c89..0b9b9911 100644 --- a/python/src/_cdec.cpp +++ b/python/src/_cdec.cpp @@ -1,4 +1,4 @@ -/* Generated by Cython 0.16 on Thu Jun 14 09:24:35 2012 */ +/* Generated by Cython 0.16 on Sat Jun 23 11:57:21 2012 */ #define PY_SSIZE_T_CLEAN #include "Python.h" @@ -272,11 +272,12 @@ #define __PYX_HAVE_API___cdec #include <string> #include <vector> +#include <utility> #include <iostream> #include "utils/weights.h" #include "utils/logval.h" -#include "utils/prob.h" #include "utils/wordid.h" +#include "utils/small_vector.h" #include "utils/sparse_vector.h" #include "utils/tdict.cc" #include "utils/verbose.h" @@ -289,6 +290,7 @@ #include "decoder/hg_io.h" #include "decoder/hg_intersect.h" #include "decoder/hg_sampler.h" +#include "decoder/csplit.h" #include "decoder/ff_register.h" #include "decoder/decoder.h" #include "observer.h" @@ -376,46 +378,32 @@ static const char *__pyx_filename; static const char *__pyx_f[] = { "_cdec.pyx", + "vectors.pxi", + "hypergraph.pxi", + "lattice.pxi", }; /*--- Type declarations ---*/ -struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree; -struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample; -struct __pyx_obj_5_cdec_Weights; +struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample; +struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__; +struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree; struct __pyx_obj_5_cdec_Decoder; +struct __pyx_obj_5_cdec_SparseVector; struct __pyx_obj_5_cdec_Hypergraph; struct __pyx_obj_5_cdec_Lattice; +struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__; struct __pyx_obj_5_cdec___pyx_scope_struct____iter__; -struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest; +struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest; +struct __pyx_obj_5_cdec_DenseVector; -/* "_cdec.pyx":105 +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":56 * del derivations * - * def kbest_tree(self, size): # <<<<<<<<<<<<<< - * assert (self.hg != NULL) - * cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ETreeTraversal](self.hg[0], size) - */ -struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree { - PyObject_HEAD - KBest::KBestDerivations<std::vector<WordID>,ETreeTraversal>::Derivation *__pyx_v_derivation; - KBest::KBestDerivations<std::vector<WordID>,ETreeTraversal> *__pyx_v_derivations; - unsigned int __pyx_v_k; - struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self; - PyObject *__pyx_v_sentence; - PyObject *__pyx_v_size; - unsigned int __pyx_t_0; - long __pyx_t_1; -}; - - -/* "_cdec.pyx":122 - * hypergraph.Intersect(lat.lattice[0], self.hg) - * * def sample(self, unsigned n): # <<<<<<<<<<<<<< - * assert (self.hg != NULL) * cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() + * if self.rng == NULL: */ -struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample { +struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample { PyObject_HEAD std::vector<HypergraphSampler::Hypothesis> *__pyx_v_hypos; unsigned int __pyx_v_k; @@ -427,35 +415,73 @@ struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample { }; -/* "_cdec.pyx":15 - * pass +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":43 + * self.vector.set_value(fid, value) * - * cdef class Weights: # <<<<<<<<<<<<<< - * cdef vector[weight_t]* weights + * def __iter__(self): # <<<<<<<<<<<<<< + * cdef FastSparseVector[weight_t].const_iterator* it = new FastSparseVector[weight_t].const_iterator(self.vector[0], False) + * cdef str fname + */ +struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ { + PyObject_HEAD + PyObject *__pyx_v_fname; + size_t __pyx_v_i; + FastSparseVector<weight_t>::const_iterator *__pyx_v_it; + struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self; + size_t __pyx_t_0; + size_t __pyx_t_1; +}; + + +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":44 + * del derivations * + * def kbest_tree(self, size): # <<<<<<<<<<<<<< + * cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ETreeTraversal](self.hg[0], size) + * cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal].Derivation* derivation */ -struct __pyx_obj_5_cdec_Weights { +struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree { PyObject_HEAD - std::vector<weight_t> *weights; + KBest::KBestDerivations<std::vector<WordID>,ETreeTraversal>::Derivation *__pyx_v_derivation; + KBest::KBestDerivations<std::vector<WordID>,ETreeTraversal> *__pyx_v_derivations; + unsigned int __pyx_v_k; + struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self; + PyObject *__pyx_v_size; + PyObject *__pyx_v_tree; + unsigned int __pyx_t_0; + long __pyx_t_1; }; -/* "_cdec.pyx":38 - * yield FDConvert(fid).c_str(), self.weights[0][fid] +/* "_cdec.pyx":15 + * pass * * cdef class Decoder: # <<<<<<<<<<<<<< * cdef decoder.Decoder* dec - * cdef public Weights weights + * cdef public DenseVector weights */ struct __pyx_obj_5_cdec_Decoder { PyObject_HEAD Decoder *dec; - struct __pyx_obj_5_cdec_Weights *weights; + struct __pyx_obj_5_cdec_DenseVector *weights; }; -/* "_cdec.pyx":71 - * return hg +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":32 + * return sparse + * + * cdef class SparseVector: # <<<<<<<<<<<<<< + * cdef FastSparseVector[weight_t]* vector + * + */ +struct __pyx_obj_5_cdec_SparseVector { + PyObject_HEAD + FastSparseVector<weight_t> *vector; +}; + + +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":4 + * cimport kbest as kb * * cdef class Hypergraph: # <<<<<<<<<<<<<< * cdef hypergraph.Hypergraph* hg @@ -468,8 +494,8 @@ struct __pyx_obj_5_cdec_Hypergraph { }; -/* "_cdec.pyx":139 - * # TODO: inside-outside pruning +/* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":3 + * cimport lattice * * cdef class Lattice: # <<<<<<<<<<<<<< * cdef lattice.Lattice* lattice @@ -481,41 +507,70 @@ struct __pyx_obj_5_cdec_Lattice { }; -/* "_cdec.pyx":33 - * self.weights[0][fid] = value +/* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":50 + * return hypergraph.AsPLF(self.lattice[0], True).c_str() + * + * def __iter__(self): # <<<<<<<<<<<<<< + * cdef unsigned i + * for i in range(len(self)): + */ +struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ { + PyObject_HEAD + unsigned int __pyx_v_i; + struct __pyx_obj_5_cdec_Lattice *__pyx_v_self; + Py_ssize_t __pyx_t_0; + unsigned int __pyx_t_1; +}; + + +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":18 + * self.vector[0][fid] = value * * def __iter__(self): # <<<<<<<<<<<<<< * cdef unsigned fid - * for fid in range(1, self.weights.size()): + * for fid in range(1, self.vector.size()): */ struct __pyx_obj_5_cdec___pyx_scope_struct____iter__ { PyObject_HEAD unsigned int __pyx_v_fid; - struct __pyx_obj_5_cdec_Weights *__pyx_v_self; + struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self; size_t __pyx_t_0; unsigned int __pyx_t_1; }; -/* "_cdec.pyx":92 - * return tree.decode('utf8') +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":32 + * return fmap * * def kbest(self, size): # <<<<<<<<<<<<<< - * assert (self.hg != NULL) * cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal](self.hg[0], size) + * cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal].Derivation* derivation */ -struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest { +struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest { PyObject_HEAD KBest::KBestDerivations<std::vector<WordID>,ESentenceTraversal>::Derivation *__pyx_v_derivation; KBest::KBestDerivations<std::vector<WordID>,ESentenceTraversal> *__pyx_v_derivations; unsigned int __pyx_v_k; struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self; + PyObject *__pyx_v_sentence; PyObject *__pyx_v_size; - PyObject *__pyx_v_tree; unsigned int __pyx_t_0; long __pyx_t_1; }; + +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":3 + * from cython.operator cimport preincrement as pinc + * + * cdef class DenseVector: # <<<<<<<<<<<<<< + * cdef vector[weight_t]* vector + * + */ +struct __pyx_obj_5_cdec_DenseVector { + PyObject_HEAD + std::vector<weight_t> *vector; +}; + #ifndef CYTHON_REFNANNY #define CYTHON_REFNANNY 0 #endif @@ -572,6 +627,14 @@ struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest { static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name); /*proto*/ +static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ +static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ + +static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/ + +static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, + const char *name, int exact); /*proto*/ + static void __Pyx_RaiseDoubleKeywordsError(const char* func_name, PyObject* kw_name); /*proto*/ static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \ @@ -581,13 +644,12 @@ static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[], \ static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact, Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); /*proto*/ -static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, - const char *name, int exact); /*proto*/ - -static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb); /*proto*/ -static CYTHON_INLINE void __Pyx_ErrFetch(PyObject **type, PyObject **value, PyObject **tb); /*proto*/ - -static void __Pyx_Raise(PyObject *type, PyObject *value, PyObject *tb, PyObject *cause); /*proto*/ +static CYTHON_INLINE int __Pyx_NegateNonNeg(int b) { + return unlikely(b < 0) ? b : !b; +} +static CYTHON_INLINE PyObject* __Pyx_PyBoolOrNull_FromLong(long b) { + return unlikely(b < 0) ? NULL : __Pyx_PyBool_FromLong(b); +} static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index); @@ -595,6 +657,78 @@ static CYTHON_INLINE void __Pyx_RaiseTooManyValuesError(Py_ssize_t expected); static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected); /*proto*/ +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Generic(PyObject *o, PyObject* j) { + PyObject *r; + if (!j) return NULL; + r = PyObject_GetItem(o, j); + Py_DECREF(j); + return r; +} +#define __Pyx_GetItemInt_List(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \ + __Pyx_GetItemInt_List_Fast(o, i) : \ + __Pyx_GetItemInt_Generic(o, to_py_func(i))) +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_List_Fast(PyObject *o, Py_ssize_t i) { + if (likely(o != Py_None)) { + if (likely((0 <= i) & (i < PyList_GET_SIZE(o)))) { + PyObject *r = PyList_GET_ITEM(o, i); + Py_INCREF(r); + return r; + } + else if ((-PyList_GET_SIZE(o) <= i) & (i < 0)) { + PyObject *r = PyList_GET_ITEM(o, PyList_GET_SIZE(o) + i); + Py_INCREF(r); + return r; + } + } + return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); +} +#define __Pyx_GetItemInt_Tuple(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \ + __Pyx_GetItemInt_Tuple_Fast(o, i) : \ + __Pyx_GetItemInt_Generic(o, to_py_func(i))) +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Tuple_Fast(PyObject *o, Py_ssize_t i) { + if (likely(o != Py_None)) { + if (likely((0 <= i) & (i < PyTuple_GET_SIZE(o)))) { + PyObject *r = PyTuple_GET_ITEM(o, i); + Py_INCREF(r); + return r; + } + else if ((-PyTuple_GET_SIZE(o) <= i) & (i < 0)) { + PyObject *r = PyTuple_GET_ITEM(o, PyTuple_GET_SIZE(o) + i); + Py_INCREF(r); + return r; + } + } + return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); +} +#define __Pyx_GetItemInt(o, i, size, to_py_func) (((size) <= sizeof(Py_ssize_t)) ? \ + __Pyx_GetItemInt_Fast(o, i) : \ + __Pyx_GetItemInt_Generic(o, to_py_func(i))) +static CYTHON_INLINE PyObject *__Pyx_GetItemInt_Fast(PyObject *o, Py_ssize_t i) { + if (PyList_CheckExact(o)) { + Py_ssize_t n = (likely(i >= 0)) ? i : i + PyList_GET_SIZE(o); + if (likely((n >= 0) & (n < PyList_GET_SIZE(o)))) { + PyObject *r = PyList_GET_ITEM(o, n); + Py_INCREF(r); + return r; + } + } + else if (PyTuple_CheckExact(o)) { + Py_ssize_t n = (likely(i >= 0)) ? i : i + PyTuple_GET_SIZE(o); + if (likely((n >= 0) & (n < PyTuple_GET_SIZE(o)))) { + PyObject *r = PyTuple_GET_ITEM(o, n); + Py_INCREF(r); + return r; + } + } + else if (likely(i >= 0)) { + PySequenceMethods *m = Py_TYPE(o)->tp_as_sequence; + if (likely(m && m->sq_item)) { + return m->sq_item(o, i); + } + } + return __Pyx_GetItemInt_Generic(o, PyInt_FromSsize_t(i)); +} + static double __Pyx__PyObject_AsDouble(PyObject* obj); /* proto */ #define __Pyx_PyObject_AsDouble(obj) \ ((likely(PyFloat_CheckExact(obj))) ? \ @@ -693,6 +827,10 @@ static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/ /* Module declarations from 'libcpp.vector' */ +/* Module declarations from 'libcpp.utility' */ + +/* Module declarations from 'libcpp.pair' */ + /* Module declarations from 'utils' */ /* Module declarations from 'lattice' */ @@ -704,14 +842,17 @@ static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/ /* Module declarations from 'kbest' */ /* Module declarations from '_cdec' */ -static PyTypeObject *__pyx_ptype_5_cdec_Weights = 0; -static PyTypeObject *__pyx_ptype_5_cdec_Decoder = 0; +static PyTypeObject *__pyx_ptype_5_cdec_DenseVector = 0; +static PyTypeObject *__pyx_ptype_5_cdec_SparseVector = 0; static PyTypeObject *__pyx_ptype_5_cdec_Hypergraph = 0; static PyTypeObject *__pyx_ptype_5_cdec_Lattice = 0; +static PyTypeObject *__pyx_ptype_5_cdec_Decoder = 0; static PyTypeObject *__pyx_ptype_5_cdec___pyx_scope_struct____iter__ = 0; -static PyTypeObject *__pyx_ptype_5_cdec___pyx_scope_struct_1_kbest = 0; -static PyTypeObject *__pyx_ptype_5_cdec___pyx_scope_struct_2_kbest_tree = 0; -static PyTypeObject *__pyx_ptype_5_cdec___pyx_scope_struct_3_sample = 0; +static PyTypeObject *__pyx_ptype_5_cdec___pyx_scope_struct_1___iter__ = 0; +static PyTypeObject *__pyx_ptype_5_cdec___pyx_scope_struct_2_kbest = 0; +static PyTypeObject *__pyx_ptype_5_cdec___pyx_scope_struct_3_kbest_tree = 0; +static PyTypeObject *__pyx_ptype_5_cdec___pyx_scope_struct_4_sample = 0; +static PyTypeObject *__pyx_ptype_5_cdec___pyx_scope_struct_5___iter__ = 0; #define __Pyx_MODULE_NAME "_cdec" int __pyx_module_is_main__cdec = 0; @@ -719,12 +860,53 @@ int __pyx_module_is_main__cdec = 0; static PyObject *__pyx_builtin_Exception; static PyObject *__pyx_builtin_KeyError; static PyObject *__pyx_builtin_range; -static PyObject *__pyx_builtin_open; +static PyObject *__pyx_builtin_ValueError; +static PyObject *__pyx_builtin_NotImplemented; static PyObject *__pyx_builtin_eval; -static int __pyx_pf_5_cdec_7Weights___cinit__(struct __pyx_obj_5_cdec_Weights *__pyx_v_self, struct __pyx_obj_5_cdec_Decoder *__pyx_v_decoder); /* proto */ -static PyObject *__pyx_pf_5_cdec_7Weights_2__getitem__(struct __pyx_obj_5_cdec_Weights *__pyx_v_self, char *__pyx_v_fname); /* proto */ -static int __pyx_pf_5_cdec_7Weights_4__setitem__(struct __pyx_obj_5_cdec_Weights *__pyx_v_self, char *__pyx_v_fname, float __pyx_v_value); /* proto */ -static PyObject *__pyx_pf_5_cdec_7Weights_6__iter__(struct __pyx_obj_5_cdec_Weights *__pyx_v_self); /* proto */ +static PyObject *__pyx_builtin_enumerate; +static PyObject *__pyx_builtin_TypeError; +static PyObject *__pyx_builtin_IndexError; +static PyObject *__pyx_builtin_open; +static PyObject *__pyx_pf_5_cdec_11DenseVector___getitem__(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self, char *__pyx_v_fname); /* proto */ +static int __pyx_pf_5_cdec_11DenseVector_2__setitem__(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self, char *__pyx_v_fname, float __pyx_v_value); /* proto */ +static PyObject *__pyx_pf_5_cdec_11DenseVector_4__iter__(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_11DenseVector_7dot(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other); /* proto */ +static PyObject *__pyx_pf_5_cdec_11DenseVector_9tosparse(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_12SparseVector___getitem__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, char *__pyx_v_fname); /* proto */ +static int __pyx_pf_5_cdec_12SparseVector_2__setitem__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, char *__pyx_v_fname, float __pyx_v_value); /* proto */ +static PyObject *__pyx_pf_5_cdec_12SparseVector_4__iter__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_12SparseVector_7dot(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, PyObject *__pyx_v_other); /* proto */ +static PyObject *__pyx_pf_5_cdec_12SparseVector_9todense(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_12SparseVector_11__richcmp__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other, int __pyx_v_op); /* proto */ +static Py_ssize_t __pyx_pf_5_cdec_12SparseVector_13__len__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self); /* proto */ +static int __pyx_pf_5_cdec_12SparseVector_15__contains__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, char *__pyx_v_fname); /* proto */ +static PyObject *__pyx_pf_5_cdec_12SparseVector_17__iadd__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other); /* proto */ +static PyObject *__pyx_pf_5_cdec_12SparseVector_19__isub__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other); /* proto */ +static PyObject *__pyx_pf_5_cdec_12SparseVector_21__imul__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, float __pyx_v_scalar); /* proto */ +#if PY_MAJOR_VERSION < 3 +static PyObject *__pyx_pf_5_cdec_12SparseVector_23__idiv__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, float __pyx_v_scalar); /* proto */ +#endif +static PyObject *__pyx_pf_5_cdec_12SparseVector_25__add__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other); /* proto */ +static PyObject *__pyx_pf_5_cdec_12SparseVector_27__sub__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other); /* proto */ +static void __pyx_pf_5_cdec_10Hypergraph___dealloc__(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_2viterbi(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_4viterbi_tree(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_6viterbi_source_tree(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_8viterbi_features(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_10kbest(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_size); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_13kbest_tree(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_size); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_16sample(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, unsigned int __pyx_v_n); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_19intersect(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, struct __pyx_obj_5_cdec_Lattice *__pyx_v_lat); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_21prune(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_beam_alpha, PyObject *__pyx_v_density, PyObject *__pyx_v_kwargs); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_23lattice(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_10Hypergraph_25reweight(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_weights); /* proto */ +static int __pyx_pf_5_cdec_7Lattice___init__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self, PyObject *__pyx_v_inp); /* proto */ +static PyObject *__pyx_pf_5_cdec_7Lattice_2__getitem__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self, int __pyx_v_index); /* proto */ +static int __pyx_pf_5_cdec_7Lattice_4__setitem__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self, int __pyx_v_index, PyObject *__pyx_v_arcs); /* proto */ +static Py_ssize_t __pyx_pf_5_cdec_7Lattice_6__len__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_7Lattice_8__str__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self); /* proto */ +static PyObject *__pyx_pf_5_cdec_7Lattice_10__iter__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self); /* proto */ +static void __pyx_pf_5_cdec_7Lattice_13__dealloc__(CYTHON_UNUSED struct __pyx_obj_5_cdec_Lattice *__pyx_v_self); /* proto */ static int __pyx_pf_5_cdec_7Decoder___cinit__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, char *__pyx_v_config); /* proto */ static void __pyx_pf_5_cdec_7Decoder_2__dealloc__(CYTHON_UNUSED struct __pyx_obj_5_cdec_Decoder *__pyx_v_self); /* proto */ static PyObject *__pyx_pf_5_cdec_7Decoder_4read_weights(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, PyObject *__pyx_v_cfg); /* proto */ @@ -732,17 +914,16 @@ static PyObject *__pyx_pf_5_cdec_7Decoder_6translate(struct __pyx_obj_5_cdec_Dec static PyObject *__pyx_pf_5_cdec_7Decoder_7weights___get__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self); /* proto */ static int __pyx_pf_5_cdec_7Decoder_7weights_2__set__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, PyObject *__pyx_v_value); /* proto */ static int __pyx_pf_5_cdec_7Decoder_7weights_4__del__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self); /* proto */ -static void __pyx_pf_5_cdec_10Hypergraph___dealloc__(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self); /* proto */ -static PyObject *__pyx_pf_5_cdec_10Hypergraph_2viterbi(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self); /* proto */ -static PyObject *__pyx_pf_5_cdec_10Hypergraph_4viterbi_tree(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self); /* proto */ -static PyObject *__pyx_pf_5_cdec_10Hypergraph_6kbest(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_size); /* proto */ -static PyObject *__pyx_pf_5_cdec_10Hypergraph_9kbest_tree(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_size); /* proto */ -static PyObject *__pyx_pf_5_cdec_10Hypergraph_12intersect(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, struct __pyx_obj_5_cdec_Lattice *__pyx_v_lat); /* proto */ -static PyObject *__pyx_pf_5_cdec_10Hypergraph_14sample(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, unsigned int __pyx_v_n); /* proto */ -static int __pyx_pf_5_cdec_7Lattice___init__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self, PyObject *__pyx_v_plf_tuple); /* proto */ -static PyObject *__pyx_pf_5_cdec_7Lattice_2__str__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self); /* proto */ -static PyObject *__pyx_pf_5_cdec_7Lattice_4__iter__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self); /* proto */ -static void __pyx_pf_5_cdec_7Lattice_6__dealloc__(CYTHON_UNUSED struct __pyx_obj_5_cdec_Lattice *__pyx_v_self); /* proto */ +static char __pyx_k_1[] = "cannot take the dot product of %s and SparseVector"; +static char __pyx_k_2[] = "comparison not implemented for SparseVector"; +static char __pyx_k_10[] = "csplit_preserve_full_word"; +static char __pyx_k_11[] = "cannot reweight hypergraph with %s"; +static char __pyx_k_13[] = "Cannot create lattice from %s"; +static char __pyx_k_14[] = "lattice index out of range"; +static char __pyx_k_21[] = "Cannot translate input type %s"; +static char __pyx_k__dot[] = "dot"; +static char __pyx_k__inp[] = "inp"; +static char __pyx_k__plf[] = "plf"; static char __pyx_k__eval[] = "eval"; static char __pyx_k__open[] = "open"; static char __pyx_k__self[] = "self"; @@ -754,7 +935,7 @@ static char __pyx_k__strip[] = "strip"; static char __pyx_k__config[] = "config"; static char __pyx_k__decode[] = "decode"; static char __pyx_k__encode[] = "encode"; -static char __pyx_k__decoder[] = "decoder"; +static char __pyx_k__density[] = "density"; static char __pyx_k__grammar[] = "grammar"; static char __pyx_k__KeyError[] = "KeyError"; static char __pyx_k____exit__[] = "__exit__"; @@ -762,149 +943,98 @@ static char __pyx_k____main__[] = "__main__"; static char __pyx_k____test__[] = "__test__"; static char __pyx_k__sentence[] = "sentence"; static char __pyx_k__Exception[] = "Exception"; +static char __pyx_k__TypeError[] = "TypeError"; static char __pyx_k____enter__[] = "__enter__"; -static char __pyx_k__plf_tuple[] = "plf_tuple"; +static char __pyx_k__enumerate[] = "enumerate"; +static char __pyx_k__IndexError[] = "IndexError"; +static char __pyx_k__ValueError[] = "ValueError"; +static char __pyx_k__beam_alpha[] = "beam_alpha"; static char __pyx_k__ParseFailed[] = "ParseFailed"; +static char __pyx_k__NotImplemented[] = "NotImplemented"; +static PyObject *__pyx_kp_s_1; +static PyObject *__pyx_n_s_10; +static PyObject *__pyx_kp_s_11; +static PyObject *__pyx_kp_s_13; +static PyObject *__pyx_kp_s_14; +static PyObject *__pyx_kp_s_2; +static PyObject *__pyx_kp_s_21; static PyObject *__pyx_n_s__Exception; +static PyObject *__pyx_n_s__IndexError; static PyObject *__pyx_n_s__KeyError; +static PyObject *__pyx_n_s__NotImplemented; static PyObject *__pyx_n_s__ParseFailed; +static PyObject *__pyx_n_s__TypeError; +static PyObject *__pyx_n_s__ValueError; static PyObject *__pyx_n_s____enter__; static PyObject *__pyx_n_s____exit__; static PyObject *__pyx_n_s____main__; static PyObject *__pyx_n_s____test__; static PyObject *__pyx_n_s___cdec; +static PyObject *__pyx_n_s__beam_alpha; static PyObject *__pyx_n_s__config; static PyObject *__pyx_n_s__decode; -static PyObject *__pyx_n_s__decoder; +static PyObject *__pyx_n_s__density; +static PyObject *__pyx_n_s__dot; static PyObject *__pyx_n_s__encode; +static PyObject *__pyx_n_s__enumerate; static PyObject *__pyx_n_s__eval; static PyObject *__pyx_n_s__grammar; +static PyObject *__pyx_n_s__inp; static PyObject *__pyx_n_s__open; -static PyObject *__pyx_n_s__plf_tuple; +static PyObject *__pyx_n_s__plf; static PyObject *__pyx_n_s__range; static PyObject *__pyx_n_s__self; static PyObject *__pyx_n_s__sentence; static PyObject *__pyx_n_s__split; static PyObject *__pyx_n_s__strip; static PyObject *__pyx_n_s__utf8; -static PyObject *__pyx_k_tuple_1; -static PyObject *__pyx_k_tuple_2; +static PyObject *__pyx_int_0; +static PyObject *__pyx_int_1; static PyObject *__pyx_k_tuple_3; static PyObject *__pyx_k_tuple_4; static PyObject *__pyx_k_tuple_5; static PyObject *__pyx_k_tuple_6; static PyObject *__pyx_k_tuple_7; +static PyObject *__pyx_k_tuple_8; +static PyObject *__pyx_k_tuple_9; +static PyObject *__pyx_k_tuple_12; +static PyObject *__pyx_k_tuple_15; +static PyObject *__pyx_k_tuple_16; +static PyObject *__pyx_k_tuple_17; +static PyObject *__pyx_k_tuple_18; +static PyObject *__pyx_k_tuple_19; +static PyObject *__pyx_k_tuple_20; /* Python wrapper */ -static int __pyx_pw_5_cdec_7Weights_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static int __pyx_pw_5_cdec_7Weights_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { - struct __pyx_obj_5_cdec_Decoder *__pyx_v_decoder = 0; - static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__decoder,0}; - int __pyx_r; - __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__cinit__ (wrapper)", 0); - { - PyObject* values[1] = {0}; - if (unlikely(__pyx_kwds)) { - Py_ssize_t kw_args; - const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); - switch (pos_args) { - case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - case 0: break; - default: goto __pyx_L5_argtuple_error; - } - kw_args = PyDict_Size(__pyx_kwds); - switch (pos_args) { - case 0: - values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__decoder); - if (likely(values[0])) kw_args--; - else goto __pyx_L5_argtuple_error; - } - if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L3_error;} - } - } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { - goto __pyx_L5_argtuple_error; - } else { - values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - } - __pyx_v_decoder = ((struct __pyx_obj_5_cdec_Decoder *)values[0]); - } - goto __pyx_L4_argument_unpacking_done; - __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L3_error;} - __pyx_L3_error:; - __Pyx_AddTraceback("_cdec.Weights.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename); - __Pyx_RefNannyFinishContext(); - return -1; - __pyx_L4_argument_unpacking_done:; - if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_decoder), __pyx_ptype_5_cdec_Decoder, 1, "decoder", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_r = __pyx_pf_5_cdec_7Weights___cinit__(((struct __pyx_obj_5_cdec_Weights *)__pyx_v_self), __pyx_v_decoder); - goto __pyx_L0; - __pyx_L1_error:; - __pyx_r = -1; - __pyx_L0:; - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -/* "_cdec.pyx":18 - * cdef vector[weight_t]* weights - * - * def __cinit__(self, Decoder decoder): # <<<<<<<<<<<<<< - * self.weights = &decoder.dec.CurrentWeightVector() - * - */ - -static int __pyx_pf_5_cdec_7Weights___cinit__(struct __pyx_obj_5_cdec_Weights *__pyx_v_self, struct __pyx_obj_5_cdec_Decoder *__pyx_v_decoder) { - int __pyx_r; - __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__cinit__", 0); - - /* "_cdec.pyx":19 - * - * def __cinit__(self, Decoder decoder): - * self.weights = &decoder.dec.CurrentWeightVector() # <<<<<<<<<<<<<< - * - * def __getitem__(self, char* fname): - */ - __pyx_v_self->weights = (&__pyx_v_decoder->dec->CurrentWeightVector()); - - __pyx_r = 0; - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -/* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_7Weights_3__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname); /*proto*/ -static PyObject *__pyx_pw_5_cdec_7Weights_3__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname) { +static PyObject *__pyx_pw_5_cdec_11DenseVector_1__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname); /*proto*/ +static PyObject *__pyx_pw_5_cdec_11DenseVector_1__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname) { char *__pyx_v_fname; PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("__getitem__ (wrapper)", 0); assert(__pyx_arg_fname); { - __pyx_v_fname = PyBytes_AsString(__pyx_arg_fname); if (unlikely((!__pyx_v_fname) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_fname = PyBytes_AsString(__pyx_arg_fname); if (unlikely((!__pyx_v_fname) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;} } goto __pyx_L4_argument_unpacking_done; __pyx_L3_error:; - __Pyx_AddTraceback("_cdec.Weights.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_AddTraceback("_cdec.DenseVector.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); return NULL; __pyx_L4_argument_unpacking_done:; - __pyx_r = __pyx_pf_5_cdec_7Weights_2__getitem__(((struct __pyx_obj_5_cdec_Weights *)__pyx_v_self), ((char *)__pyx_v_fname)); + __pyx_r = __pyx_pf_5_cdec_11DenseVector___getitem__(((struct __pyx_obj_5_cdec_DenseVector *)__pyx_v_self), ((char *)__pyx_v_fname)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":21 - * self.weights = &decoder.dec.CurrentWeightVector() +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":6 + * cdef vector[weight_t]* vector * * def __getitem__(self, char* fname): # <<<<<<<<<<<<<< * cdef unsigned fid = FDConvert(fname) - * if fid <= self.weights.size(): + * if fid <= self.vector.size(): */ -static PyObject *__pyx_pf_5_cdec_7Weights_2__getitem__(struct __pyx_obj_5_cdec_Weights *__pyx_v_self, char *__pyx_v_fname) { +static PyObject *__pyx_pf_5_cdec_11DenseVector___getitem__(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self, char *__pyx_v_fname) { unsigned int __pyx_v_fid; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations @@ -916,34 +1046,34 @@ static PyObject *__pyx_pf_5_cdec_7Weights_2__getitem__(struct __pyx_obj_5_cdec_W int __pyx_clineno = 0; __Pyx_RefNannySetupContext("__getitem__", 0); - /* "_cdec.pyx":22 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":7 * * def __getitem__(self, char* fname): * cdef unsigned fid = FDConvert(fname) # <<<<<<<<<<<<<< - * if fid <= self.weights.size(): - * return self.weights[0][fid] + * if fid <= self.vector.size(): + * return self.vector[0][fid] */ __pyx_v_fid = FD::Convert(__pyx_v_fname); - /* "_cdec.pyx":23 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":8 * def __getitem__(self, char* fname): * cdef unsigned fid = FDConvert(fname) - * if fid <= self.weights.size(): # <<<<<<<<<<<<<< - * return self.weights[0][fid] + * if fid <= self.vector.size(): # <<<<<<<<<<<<<< + * return self.vector[0][fid] * raise KeyError(fname) */ - __pyx_t_1 = (__pyx_v_fid <= __pyx_v_self->weights->size()); + __pyx_t_1 = (__pyx_v_fid <= __pyx_v_self->vector->size()); if (__pyx_t_1) { - /* "_cdec.pyx":24 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":9 * cdef unsigned fid = FDConvert(fname) - * if fid <= self.weights.size(): - * return self.weights[0][fid] # <<<<<<<<<<<<<< + * if fid <= self.vector.size(): + * return self.vector[0][fid] # <<<<<<<<<<<<<< * raise KeyError(fname) * */ __Pyx_XDECREF(__pyx_r); - __pyx_t_2 = PyFloat_FromDouble(((__pyx_v_self->weights[0])[__pyx_v_fid])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = PyFloat_FromDouble(((__pyx_v_self->vector[0])[__pyx_v_fid])); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_2); __pyx_r = __pyx_t_2; __pyx_t_2 = 0; @@ -952,33 +1082,33 @@ static PyObject *__pyx_pf_5_cdec_7Weights_2__getitem__(struct __pyx_obj_5_cdec_W } __pyx_L3:; - /* "_cdec.pyx":25 - * if fid <= self.weights.size(): - * return self.weights[0][fid] + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":10 + * if fid <= self.vector.size(): + * return self.vector[0][fid] * raise KeyError(fname) # <<<<<<<<<<<<<< * * def __setitem__(self, char* fname, float value): */ - __pyx_t_2 = PyBytes_FromString(__pyx_v_fname); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = PyBytes_FromString(__pyx_v_fname); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_2)); - __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_3); PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_t_2)); __Pyx_GIVEREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; - __pyx_t_2 = PyObject_Call(__pyx_builtin_KeyError, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = PyObject_Call(__pyx_builtin_KeyError, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_2); __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; __Pyx_Raise(__pyx_t_2, 0, 0, 0); __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + {__pyx_filename = __pyx_f[1]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_r = Py_None; __Pyx_INCREF(Py_None); goto __pyx_L0; __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_2); __Pyx_XDECREF(__pyx_t_3); - __Pyx_AddTraceback("_cdec.Weights.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_AddTraceback("_cdec.DenseVector.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_XGIVEREF(__pyx_r); @@ -987,111 +1117,111 @@ static PyObject *__pyx_pf_5_cdec_7Weights_2__getitem__(struct __pyx_obj_5_cdec_W } /* Python wrapper */ -static int __pyx_pw_5_cdec_7Weights_5__setitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname, PyObject *__pyx_arg_value); /*proto*/ -static int __pyx_pw_5_cdec_7Weights_5__setitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname, PyObject *__pyx_arg_value) { +static int __pyx_pw_5_cdec_11DenseVector_3__setitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname, PyObject *__pyx_arg_value); /*proto*/ +static int __pyx_pw_5_cdec_11DenseVector_3__setitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname, PyObject *__pyx_arg_value) { char *__pyx_v_fname; float __pyx_v_value; int __pyx_r; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("__setitem__ (wrapper)", 0); assert(__pyx_arg_fname); { - __pyx_v_fname = PyBytes_AsString(__pyx_arg_fname); if (unlikely((!__pyx_v_fname) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 27; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_fname = PyBytes_AsString(__pyx_arg_fname); if (unlikely((!__pyx_v_fname) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L3_error;} } assert(__pyx_arg_value); { - __pyx_v_value = __pyx_PyFloat_AsFloat(__pyx_arg_value); if (unlikely((__pyx_v_value == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 27; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_value = __pyx_PyFloat_AsFloat(__pyx_arg_value); if (unlikely((__pyx_v_value == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L3_error;} } goto __pyx_L4_argument_unpacking_done; __pyx_L3_error:; - __Pyx_AddTraceback("_cdec.Weights.__setitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_AddTraceback("_cdec.DenseVector.__setitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); return -1; __pyx_L4_argument_unpacking_done:; - __pyx_r = __pyx_pf_5_cdec_7Weights_4__setitem__(((struct __pyx_obj_5_cdec_Weights *)__pyx_v_self), ((char *)__pyx_v_fname), ((float)__pyx_v_value)); + __pyx_r = __pyx_pf_5_cdec_11DenseVector_2__setitem__(((struct __pyx_obj_5_cdec_DenseVector *)__pyx_v_self), ((char *)__pyx_v_fname), ((float)__pyx_v_value)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":27 +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":12 * raise KeyError(fname) * * def __setitem__(self, char* fname, float value): # <<<<<<<<<<<<<< * cdef unsigned fid = FDConvert(<char *>fname) - * if self.weights.size() <= fid: + * if self.vector.size() <= fid: */ -static int __pyx_pf_5_cdec_7Weights_4__setitem__(struct __pyx_obj_5_cdec_Weights *__pyx_v_self, char *__pyx_v_fname, float __pyx_v_value) { +static int __pyx_pf_5_cdec_11DenseVector_2__setitem__(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self, char *__pyx_v_fname, float __pyx_v_value) { unsigned int __pyx_v_fid; int __pyx_r; __Pyx_RefNannyDeclarations int __pyx_t_1; __Pyx_RefNannySetupContext("__setitem__", 0); - /* "_cdec.pyx":28 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":13 * * def __setitem__(self, char* fname, float value): * cdef unsigned fid = FDConvert(<char *>fname) # <<<<<<<<<<<<<< - * if self.weights.size() <= fid: - * self.weights.resize(fid + 1) + * if self.vector.size() <= fid: + * self.vector.resize(fid + 1) */ __pyx_v_fid = FD::Convert(((char *)__pyx_v_fname)); - /* "_cdec.pyx":29 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":14 * def __setitem__(self, char* fname, float value): * cdef unsigned fid = FDConvert(<char *>fname) - * if self.weights.size() <= fid: # <<<<<<<<<<<<<< - * self.weights.resize(fid + 1) - * self.weights[0][fid] = value + * if self.vector.size() <= fid: # <<<<<<<<<<<<<< + * self.vector.resize(fid + 1) + * self.vector[0][fid] = value */ - __pyx_t_1 = (__pyx_v_self->weights->size() <= __pyx_v_fid); + __pyx_t_1 = (__pyx_v_self->vector->size() <= __pyx_v_fid); if (__pyx_t_1) { - /* "_cdec.pyx":30 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":15 * cdef unsigned fid = FDConvert(<char *>fname) - * if self.weights.size() <= fid: - * self.weights.resize(fid + 1) # <<<<<<<<<<<<<< - * self.weights[0][fid] = value + * if self.vector.size() <= fid: + * self.vector.resize(fid + 1) # <<<<<<<<<<<<<< + * self.vector[0][fid] = value * */ - __pyx_v_self->weights->resize((__pyx_v_fid + 1)); + __pyx_v_self->vector->resize((__pyx_v_fid + 1)); goto __pyx_L3; } __pyx_L3:; - /* "_cdec.pyx":31 - * if self.weights.size() <= fid: - * self.weights.resize(fid + 1) - * self.weights[0][fid] = value # <<<<<<<<<<<<<< + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":16 + * if self.vector.size() <= fid: + * self.vector.resize(fid + 1) + * self.vector[0][fid] = value # <<<<<<<<<<<<<< * * def __iter__(self): */ - ((__pyx_v_self->weights[0])[__pyx_v_fid]) = __pyx_v_value; + ((__pyx_v_self->vector[0])[__pyx_v_fid]) = __pyx_v_value; __pyx_r = 0; __Pyx_RefNannyFinishContext(); return __pyx_r; } -static PyObject *__pyx_gb_5_cdec_7Weights_8generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ +static PyObject *__pyx_gb_5_cdec_11DenseVector_6generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ /* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_7Weights_7__iter__(PyObject *__pyx_v_self); /*proto*/ -static PyObject *__pyx_pw_5_cdec_7Weights_7__iter__(PyObject *__pyx_v_self) { +static PyObject *__pyx_pw_5_cdec_11DenseVector_5__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pw_5_cdec_11DenseVector_5__iter__(PyObject *__pyx_v_self) { PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0); - __pyx_r = __pyx_pf_5_cdec_7Weights_6__iter__(((struct __pyx_obj_5_cdec_Weights *)__pyx_v_self)); + __pyx_r = __pyx_pf_5_cdec_11DenseVector_4__iter__(((struct __pyx_obj_5_cdec_DenseVector *)__pyx_v_self)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":33 - * self.weights[0][fid] = value +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":18 + * self.vector[0][fid] = value * * def __iter__(self): # <<<<<<<<<<<<<< * cdef unsigned fid - * for fid in range(1, self.weights.size()): + * for fid in range(1, self.vector.size()): */ -static PyObject *__pyx_pf_5_cdec_7Weights_6__iter__(struct __pyx_obj_5_cdec_Weights *__pyx_v_self) { +static PyObject *__pyx_pf_5_cdec_11DenseVector_4__iter__(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self) { struct __pyx_obj_5_cdec___pyx_scope_struct____iter__ *__pyx_cur_scope; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations @@ -1109,7 +1239,7 @@ static PyObject *__pyx_pf_5_cdec_7Weights_6__iter__(struct __pyx_obj_5_cdec_Weig __Pyx_INCREF((PyObject *)__pyx_cur_scope->__pyx_v_self); __Pyx_GIVEREF((PyObject *)__pyx_cur_scope->__pyx_v_self); { - __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_7Weights_8generator, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_11DenseVector_6generator, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_DECREF(__pyx_cur_scope); __Pyx_RefNannyFinishContext(); return (PyObject *) gen; @@ -1118,7 +1248,7 @@ static PyObject *__pyx_pf_5_cdec_7Weights_6__iter__(struct __pyx_obj_5_cdec_Weig __pyx_r = Py_None; __Pyx_INCREF(Py_None); goto __pyx_L0; __pyx_L1_error:; - __Pyx_AddTraceback("_cdec.Weights.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_AddTraceback("_cdec.DenseVector.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_DECREF(((PyObject *)__pyx_cur_scope)); @@ -1127,7 +1257,7 @@ static PyObject *__pyx_pf_5_cdec_7Weights_6__iter__(struct __pyx_obj_5_cdec_Weig return __pyx_r; } -static PyObject *__pyx_gb_5_cdec_7Weights_8generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ +static PyObject *__pyx_gb_5_cdec_11DenseVector_6generator(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ { struct __pyx_obj_5_cdec___pyx_scope_struct____iter__ *__pyx_cur_scope = ((struct __pyx_obj_5_cdec___pyx_scope_struct____iter__ *)__pyx_generator->closure); PyObject *__pyx_r = NULL; @@ -1146,31 +1276,31 @@ static PyObject *__pyx_gb_5_cdec_7Weights_8generator(__pyx_GeneratorObject *__py return NULL; } __pyx_L3_first_run:; - if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - /* "_cdec.pyx":35 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":20 * def __iter__(self): * cdef unsigned fid - * for fid in range(1, self.weights.size()): # <<<<<<<<<<<<<< - * yield FDConvert(fid).c_str(), self.weights[0][fid] + * for fid in range(1, self.vector.size()): # <<<<<<<<<<<<<< + * yield FDConvert(fid).c_str(), self.vector[0][fid] * */ - __pyx_t_1 = __pyx_cur_scope->__pyx_v_self->weights->size(); + __pyx_t_1 = __pyx_cur_scope->__pyx_v_self->vector->size(); for (__pyx_t_2 = 1; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) { __pyx_cur_scope->__pyx_v_fid = __pyx_t_2; - /* "_cdec.pyx":36 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":21 * cdef unsigned fid - * for fid in range(1, self.weights.size()): - * yield FDConvert(fid).c_str(), self.weights[0][fid] # <<<<<<<<<<<<<< + * for fid in range(1, self.vector.size()): + * yield FDConvert(fid).c_str(), self.vector[0][fid] # <<<<<<<<<<<<<< * - * cdef class Decoder: + * def dot(self, SparseVector other): */ - __pyx_t_3 = PyBytes_FromString(FD::Convert(__pyx_cur_scope->__pyx_v_fid).c_str()); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = PyBytes_FromString(FD::Convert(__pyx_cur_scope->__pyx_v_fid).c_str()); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_3)); - __pyx_t_4 = PyFloat_FromDouble(((__pyx_cur_scope->__pyx_v_self->weights[0])[__pyx_cur_scope->__pyx_v_fid])); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_4 = PyFloat_FromDouble(((__pyx_cur_scope->__pyx_v_self->vector[0])[__pyx_cur_scope->__pyx_v_fid])); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_4); - __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_5 = PyTuple_New(2); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_5); PyTuple_SET_ITEM(__pyx_t_5, 0, ((PyObject *)__pyx_t_3)); __Pyx_GIVEREF(((PyObject *)__pyx_t_3)); @@ -1190,7 +1320,7 @@ static PyObject *__pyx_gb_5_cdec_7Weights_8generator(__pyx_GeneratorObject *__py __pyx_L6_resume_from_yield:; __pyx_t_1 = __pyx_cur_scope->__pyx_t_0; __pyx_t_2 = __pyx_cur_scope->__pyx_t_1; - if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} } PyErr_SetNone(PyExc_StopIteration); goto __pyx_L0; @@ -1207,551 +1337,678 @@ static PyObject *__pyx_gb_5_cdec_7Weights_8generator(__pyx_GeneratorObject *__py } /* Python wrapper */ -static int __pyx_pw_5_cdec_7Decoder_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static int __pyx_pw_5_cdec_7Decoder_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { - char *__pyx_v_config; - static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__config,0}; - int __pyx_r; +static PyObject *__pyx_pw_5_cdec_11DenseVector_8dot(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/ +static PyObject *__pyx_pw_5_cdec_11DenseVector_8dot(PyObject *__pyx_v_self, PyObject *__pyx_v_other) { + PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__cinit__ (wrapper)", 0); - { - PyObject* values[1] = {0}; - if (unlikely(__pyx_kwds)) { - Py_ssize_t kw_args; - const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); - switch (pos_args) { - case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - case 0: break; - default: goto __pyx_L5_argtuple_error; - } - kw_args = PyDict_Size(__pyx_kwds); - switch (pos_args) { - case 0: - values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__config); - if (likely(values[0])) kw_args--; - else goto __pyx_L5_argtuple_error; - } - if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L3_error;} - } - } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { - goto __pyx_L5_argtuple_error; - } else { - values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - } - __pyx_v_config = PyBytes_AsString(values[0]); if (unlikely((!__pyx_v_config) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L3_error;} - } - goto __pyx_L4_argument_unpacking_done; - __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L3_error;} - __pyx_L3_error:; - __Pyx_AddTraceback("_cdec.Decoder.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename); - __Pyx_RefNannyFinishContext(); - return -1; - __pyx_L4_argument_unpacking_done:; - __pyx_r = __pyx_pf_5_cdec_7Decoder___cinit__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self), __pyx_v_config); + __Pyx_RefNannySetupContext("dot (wrapper)", 0); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_5_cdec_SparseVector, 1, "other", 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 23; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_r = __pyx_pf_5_cdec_11DenseVector_7dot(((struct __pyx_obj_5_cdec_DenseVector *)__pyx_v_self), ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_other)); + goto __pyx_L0; + __pyx_L1_error:; + __pyx_r = NULL; + __pyx_L0:; __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":42 - * cdef public Weights weights +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":23 + * yield FDConvert(fid).c_str(), self.vector[0][fid] + * + * def dot(self, SparseVector other): # <<<<<<<<<<<<<< + * return other.dot(self) * - * def __cinit__(self, char* config): # <<<<<<<<<<<<<< - * decoder.register_feature_functions() - * cdef istringstream* config_stream = new istringstream(config) */ -static int __pyx_pf_5_cdec_7Decoder___cinit__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, char *__pyx_v_config) { - std::istringstream *__pyx_v_config_stream; - int __pyx_r; +static PyObject *__pyx_pf_5_cdec_11DenseVector_7dot(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other) { + PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations PyObject *__pyx_t_1 = NULL; PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; - __Pyx_RefNannySetupContext("__cinit__", 0); + __Pyx_RefNannySetupContext("dot", 0); - /* "_cdec.pyx":43 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":24 * - * def __cinit__(self, char* config): - * decoder.register_feature_functions() # <<<<<<<<<<<<<< - * cdef istringstream* config_stream = new istringstream(config) - * self.dec = new decoder.Decoder(config_stream) + * def dot(self, SparseVector other): + * return other.dot(self) # <<<<<<<<<<<<<< + * + * def tosparse(self): */ - register_feature_functions(); + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_other), __pyx_n_s__dot); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyTuple_New(1); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_INCREF(((PyObject *)__pyx_v_self)); + PyTuple_SET_ITEM(__pyx_t_2, 0, ((PyObject *)__pyx_v_self)); + __Pyx_GIVEREF(((PyObject *)__pyx_v_self)); + __pyx_t_3 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_t_2), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(((PyObject *)__pyx_t_2)); __pyx_t_2 = 0; + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L0; - /* "_cdec.pyx":44 - * def __cinit__(self, char* config): - * decoder.register_feature_functions() - * cdef istringstream* config_stream = new istringstream(config) # <<<<<<<<<<<<<< - * self.dec = new decoder.Decoder(config_stream) - * del config_stream + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_XDECREF(__pyx_t_3); + __Pyx_AddTraceback("_cdec.DenseVector.dot", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_11DenseVector_10tosparse(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static PyObject *__pyx_pw_5_cdec_11DenseVector_10tosparse(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("tosparse (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_11DenseVector_9tosparse(((struct __pyx_obj_5_cdec_DenseVector *)__pyx_v_self)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":26 + * return other.dot(self) + * + * def tosparse(self): # <<<<<<<<<<<<<< + * cdef SparseVector sparse = SparseVector() + * sparse.vector = new FastSparseVector[weight_t]() */ - __pyx_v_config_stream = new std::istringstream(__pyx_v_config); - /* "_cdec.pyx":45 - * decoder.register_feature_functions() - * cdef istringstream* config_stream = new istringstream(config) - * self.dec = new decoder.Decoder(config_stream) # <<<<<<<<<<<<<< - * del config_stream - * self.weights = Weights(self) +static PyObject *__pyx_pf_5_cdec_11DenseVector_9tosparse(struct __pyx_obj_5_cdec_DenseVector *__pyx_v_self) { + struct __pyx_obj_5_cdec_SparseVector *__pyx_v_sparse = 0; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("tosparse", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":27 + * + * def tosparse(self): + * cdef SparseVector sparse = SparseVector() # <<<<<<<<<<<<<< + * sparse.vector = new FastSparseVector[weight_t]() + * InitSparseVector(self.vector[0], sparse.vector) */ - __pyx_v_self->dec = new Decoder(__pyx_v_config_stream); + __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_SparseVector)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 27; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_v_sparse = ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_t_1); + __pyx_t_1 = 0; - /* "_cdec.pyx":46 - * cdef istringstream* config_stream = new istringstream(config) - * self.dec = new decoder.Decoder(config_stream) - * del config_stream # <<<<<<<<<<<<<< - * self.weights = Weights(self) + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":28 + * def tosparse(self): + * cdef SparseVector sparse = SparseVector() + * sparse.vector = new FastSparseVector[weight_t]() # <<<<<<<<<<<<<< + * InitSparseVector(self.vector[0], sparse.vector) + * return sparse + */ + __pyx_v_sparse->vector = new FastSparseVector<weight_t>(); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":29 + * cdef SparseVector sparse = SparseVector() + * sparse.vector = new FastSparseVector[weight_t]() + * InitSparseVector(self.vector[0], sparse.vector) # <<<<<<<<<<<<<< + * return sparse * */ - delete __pyx_v_config_stream; + Weights::InitSparseVector((__pyx_v_self->vector[0]), __pyx_v_sparse->vector); - /* "_cdec.pyx":47 - * self.dec = new decoder.Decoder(config_stream) - * del config_stream - * self.weights = Weights(self) # <<<<<<<<<<<<<< + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":30 + * sparse.vector = new FastSparseVector[weight_t]() + * InitSparseVector(self.vector[0], sparse.vector) + * return sparse # <<<<<<<<<<<<<< * - * def __dealloc__(self): + * cdef class SparseVector: */ - __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_1); - __Pyx_INCREF(((PyObject *)__pyx_v_self)); - PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_self)); - __Pyx_GIVEREF(((PyObject *)__pyx_v_self)); - __pyx_t_2 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_Weights)), ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; - __Pyx_GIVEREF(__pyx_t_2); - __Pyx_GOTREF(__pyx_v_self->weights); - __Pyx_DECREF(((PyObject *)__pyx_v_self->weights)); - __pyx_v_self->weights = ((struct __pyx_obj_5_cdec_Weights *)__pyx_t_2); - __pyx_t_2 = 0; + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_sparse)); + __pyx_r = ((PyObject *)__pyx_v_sparse); + goto __pyx_L0; - __pyx_r = 0; + __pyx_r = Py_None; __Pyx_INCREF(Py_None); goto __pyx_L0; __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); - __Pyx_AddTraceback("_cdec.Decoder.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = -1; + __Pyx_AddTraceback("_cdec.DenseVector.tosparse", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; __pyx_L0:; + __Pyx_XDECREF((PyObject *)__pyx_v_sparse); + __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* Python wrapper */ -static void __pyx_pw_5_cdec_7Decoder_3__dealloc__(PyObject *__pyx_v_self); /*proto*/ -static void __pyx_pw_5_cdec_7Decoder_3__dealloc__(PyObject *__pyx_v_self) { +static PyObject *__pyx_pw_5_cdec_12SparseVector_1__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_1__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname) { + char *__pyx_v_fname; + PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0); - __pyx_pf_5_cdec_7Decoder_2__dealloc__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self)); + __Pyx_RefNannySetupContext("__getitem__ (wrapper)", 0); + assert(__pyx_arg_fname); { + __pyx_v_fname = PyBytes_AsString(__pyx_arg_fname); if (unlikely((!__pyx_v_fname) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 35; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L3_error:; + __Pyx_AddTraceback("_cdec.SparseVector.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_r = __pyx_pf_5_cdec_12SparseVector___getitem__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((char *)__pyx_v_fname)); __Pyx_RefNannyFinishContext(); + return __pyx_r; } -/* "_cdec.pyx":49 - * self.weights = Weights(self) +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":35 + * cdef FastSparseVector[weight_t]* vector * - * def __dealloc__(self): # <<<<<<<<<<<<<< - * del self.dec + * def __getitem__(self, char* fname): # <<<<<<<<<<<<<< + * cdef unsigned fid = FDConvert(fname) + * return self.vector.value(fid) + */ + +static PyObject *__pyx_pf_5_cdec_12SparseVector___getitem__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, char *__pyx_v_fname) { + unsigned int __pyx_v_fid; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("__getitem__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":36 + * + * def __getitem__(self, char* fname): + * cdef unsigned fid = FDConvert(fname) # <<<<<<<<<<<<<< + * return self.vector.value(fid) * */ + __pyx_v_fid = FD::Convert(__pyx_v_fname); -static void __pyx_pf_5_cdec_7Decoder_2__dealloc__(CYTHON_UNUSED struct __pyx_obj_5_cdec_Decoder *__pyx_v_self) { + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":37 + * def __getitem__(self, char* fname): + * cdef unsigned fid = FDConvert(fname) + * return self.vector.value(fid) # <<<<<<<<<<<<<< + * + * def __setitem__(self, char* fname, float value): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyFloat_FromDouble(__pyx_v_self->vector->value(__pyx_v_fid)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("_cdec.SparseVector.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static int __pyx_pw_5_cdec_12SparseVector_3__setitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname, PyObject *__pyx_arg_value); /*proto*/ +static int __pyx_pw_5_cdec_12SparseVector_3__setitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname, PyObject *__pyx_arg_value) { + char *__pyx_v_fname; + float __pyx_v_value; + int __pyx_r; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__dealloc__", 0); + __Pyx_RefNannySetupContext("__setitem__ (wrapper)", 0); + assert(__pyx_arg_fname); { + __pyx_v_fname = PyBytes_AsString(__pyx_arg_fname); if (unlikely((!__pyx_v_fname) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 39; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + assert(__pyx_arg_value); { + __pyx_v_value = __pyx_PyFloat_AsFloat(__pyx_arg_value); if (unlikely((__pyx_v_value == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 39; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L3_error:; + __Pyx_AddTraceback("_cdec.SparseVector.__setitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + __pyx_r = __pyx_pf_5_cdec_12SparseVector_2__setitem__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((char *)__pyx_v_fname), ((float)__pyx_v_value)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} - /* "_cdec.pyx":50 +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":39 + * return self.vector.value(fid) * - * def __dealloc__(self): - * del self.dec # <<<<<<<<<<<<<< + * def __setitem__(self, char* fname, float value): # <<<<<<<<<<<<<< + * cdef unsigned fid = FDConvert(<char *>fname) + * self.vector.set_value(fid, value) + */ + +static int __pyx_pf_5_cdec_12SparseVector_2__setitem__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, char *__pyx_v_fname, float __pyx_v_value) { + unsigned int __pyx_v_fid; + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__setitem__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":40 + * + * def __setitem__(self, char* fname, float value): + * cdef unsigned fid = FDConvert(<char *>fname) # <<<<<<<<<<<<<< + * self.vector.set_value(fid, value) * - * def read_weights(self, cfg): */ - delete __pyx_v_self->dec; + __pyx_v_fid = FD::Convert(((char *)__pyx_v_fname)); + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":41 + * def __setitem__(self, char* fname, float value): + * cdef unsigned fid = FDConvert(<char *>fname) + * self.vector.set_value(fid, value) # <<<<<<<<<<<<<< + * + * def __iter__(self): + */ + __pyx_v_self->vector->set_value(__pyx_v_fid, __pyx_v_value); + + __pyx_r = 0; __Pyx_RefNannyFinishContext(); + return __pyx_r; } +static PyObject *__pyx_gb_5_cdec_12SparseVector_6generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ /* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_7Decoder_5read_weights(PyObject *__pyx_v_self, PyObject *__pyx_v_cfg); /*proto*/ -static PyObject *__pyx_pw_5_cdec_7Decoder_5read_weights(PyObject *__pyx_v_self, PyObject *__pyx_v_cfg) { +static PyObject *__pyx_pw_5_cdec_12SparseVector_5__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_5__iter__(PyObject *__pyx_v_self) { PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("read_weights (wrapper)", 0); - __pyx_r = __pyx_pf_5_cdec_7Decoder_4read_weights(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self), ((PyObject *)__pyx_v_cfg)); + __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_12SparseVector_4__iter__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":52 - * del self.dec +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":43 + * self.vector.set_value(fid, value) * - * def read_weights(self, cfg): # <<<<<<<<<<<<<< - * with open(cfg) as fp: - * for line in fp: + * def __iter__(self): # <<<<<<<<<<<<<< + * cdef FastSparseVector[weight_t].const_iterator* it = new FastSparseVector[weight_t].const_iterator(self.vector[0], False) + * cdef str fname */ -static PyObject *__pyx_pf_5_cdec_7Decoder_4read_weights(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, PyObject *__pyx_v_cfg) { - PyObject *__pyx_v_fp = NULL; - PyObject *__pyx_v_line = NULL; - PyObject *__pyx_v_fname = NULL; - PyObject *__pyx_v_value = NULL; +static PyObject *__pyx_pf_5_cdec_12SparseVector_4__iter__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self) { + struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *__pyx_cur_scope; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations - PyObject *__pyx_t_1 = NULL; - PyObject *__pyx_t_2 = NULL; - PyObject *__pyx_t_3 = NULL; - PyObject *__pyx_t_4 = NULL; - PyObject *__pyx_t_5 = NULL; - PyObject *__pyx_t_6 = NULL; - PyObject *__pyx_t_7 = NULL; - Py_ssize_t __pyx_t_8; - PyObject *(*__pyx_t_9)(PyObject *); - PyObject *__pyx_t_10 = NULL; - PyObject *__pyx_t_11 = NULL; - PyObject *(*__pyx_t_12)(PyObject *); - double __pyx_t_13; - int __pyx_t_14; - PyObject *__pyx_t_15 = NULL; - int __pyx_t_16; int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; - __Pyx_RefNannySetupContext("read_weights", 0); + __Pyx_RefNannySetupContext("__iter__", 0); + __pyx_cur_scope = (struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *)__pyx_ptype_5_cdec___pyx_scope_struct_1___iter__->tp_new(__pyx_ptype_5_cdec___pyx_scope_struct_1___iter__, __pyx_empty_tuple, NULL); + if (unlikely(!__pyx_cur_scope)) { + __Pyx_RefNannyFinishContext(); + return NULL; + } + __Pyx_GOTREF(__pyx_cur_scope); + __pyx_cur_scope->__pyx_v_self = __pyx_v_self; + __Pyx_INCREF((PyObject *)__pyx_cur_scope->__pyx_v_self); + __Pyx_GIVEREF((PyObject *)__pyx_cur_scope->__pyx_v_self); + { + __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_12SparseVector_6generator1, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_cur_scope); + __Pyx_RefNannyFinishContext(); + return (PyObject *) gen; + } - /* "_cdec.pyx":53 + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("_cdec.SparseVector.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(((PyObject *)__pyx_cur_scope)); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +static PyObject *__pyx_gb_5_cdec_12SparseVector_6generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ +{ + struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *__pyx_cur_scope = ((struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *)__pyx_generator->closure); + PyObject *__pyx_r = NULL; + size_t __pyx_t_1; + size_t __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + PyObject *__pyx_t_4 = NULL; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("None", 0); + switch (__pyx_generator->resume_label) { + case 0: goto __pyx_L3_first_run; + case 1: goto __pyx_L6_resume_from_yield; + default: /* CPython raises the right error here */ + __Pyx_RefNannyFinishContext(); + return NULL; + } + __pyx_L3_first_run:; + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":44 * - * def read_weights(self, cfg): - * with open(cfg) as fp: # <<<<<<<<<<<<<< - * for line in fp: - * fname, value = line.split() + * def __iter__(self): + * cdef FastSparseVector[weight_t].const_iterator* it = new FastSparseVector[weight_t].const_iterator(self.vector[0], False) # <<<<<<<<<<<<<< + * cdef str fname + * for i in range(self.vector.size()): */ - /*with:*/ { - __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_1); - __Pyx_INCREF(__pyx_v_cfg); - PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_cfg); - __Pyx_GIVEREF(__pyx_v_cfg); - __pyx_t_2 = PyObject_Call(__pyx_builtin_open, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; - __pyx_t_3 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s____exit__); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_cur_scope->__pyx_v_it = new FastSparseVector<weight_t>::const_iterator((__pyx_cur_scope->__pyx_v_self->vector[0]), 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":46 + * cdef FastSparseVector[weight_t].const_iterator* it = new FastSparseVector[weight_t].const_iterator(self.vector[0], False) + * cdef str fname + * for i in range(self.vector.size()): # <<<<<<<<<<<<<< + * fname = FDConvert(it[0].ptr().first).c_str() + * yield (fname, it[0].ptr().second) + */ + __pyx_t_1 = __pyx_cur_scope->__pyx_v_self->vector->size(); + for (__pyx_t_2 = 0; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) { + __pyx_cur_scope->__pyx_v_i = __pyx_t_2; + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":47 + * cdef str fname + * for i in range(self.vector.size()): + * fname = FDConvert(it[0].ptr().first).c_str() # <<<<<<<<<<<<<< + * yield (fname, it[0].ptr().second) + * pinc(it[0]) + */ + __pyx_t_3 = PyBytes_FromString(FD::Convert((__pyx_cur_scope->__pyx_v_it[0]).operator->()->first).c_str()); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_3)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_3))->tp_name), 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 47; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_XGOTREF(((PyObject *)__pyx_cur_scope->__pyx_v_fname)); + __Pyx_XDECREF(((PyObject *)__pyx_cur_scope->__pyx_v_fname)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_3)); + __pyx_cur_scope->__pyx_v_fname = ((PyObject*)__pyx_t_3); + __pyx_t_3 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":48 + * for i in range(self.vector.size()): + * fname = FDConvert(it[0].ptr().first).c_str() + * yield (fname, it[0].ptr().second) # <<<<<<<<<<<<<< + * pinc(it[0]) + * + */ + __pyx_t_3 = PyFloat_FromDouble((__pyx_cur_scope->__pyx_v_it[0]).operator->()->second); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_3); - __pyx_t_1 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s____enter__); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L3_error;} - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_4 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_t_4 = PyTuple_New(2); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_4); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - /*try:*/ { - { - __Pyx_ExceptionSave(&__pyx_t_5, &__pyx_t_6, &__pyx_t_7); - __Pyx_XGOTREF(__pyx_t_5); - __Pyx_XGOTREF(__pyx_t_6); - __Pyx_XGOTREF(__pyx_t_7); - /*try:*/ { - __Pyx_INCREF(__pyx_t_4); - __pyx_v_fp = __pyx_t_4; - __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_INCREF(((PyObject *)__pyx_cur_scope->__pyx_v_fname)); + PyTuple_SET_ITEM(__pyx_t_4, 0, ((PyObject *)__pyx_cur_scope->__pyx_v_fname)); + __Pyx_GIVEREF(((PyObject *)__pyx_cur_scope->__pyx_v_fname)); + PyTuple_SET_ITEM(__pyx_t_4, 1, __pyx_t_3); + __Pyx_GIVEREF(__pyx_t_3); + __pyx_t_3 = 0; + __pyx_r = ((PyObject *)__pyx_t_4); + __pyx_t_4 = 0; + __pyx_cur_scope->__pyx_t_0 = __pyx_t_1; + __pyx_cur_scope->__pyx_t_1 = __pyx_t_2; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + /* return from generator, yielding value */ + __pyx_generator->resume_label = 1; + return __pyx_r; + __pyx_L6_resume_from_yield:; + __pyx_t_1 = __pyx_cur_scope->__pyx_t_0; + __pyx_t_2 = __pyx_cur_scope->__pyx_t_1; + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - /* "_cdec.pyx":54 - * def read_weights(self, cfg): - * with open(cfg) as fp: - * for line in fp: # <<<<<<<<<<<<<< - * fname, value = line.split() - * self.weights[fname.strip()] = float(value) + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":49 + * fname = FDConvert(it[0].ptr().first).c_str() + * yield (fname, it[0].ptr().second) + * pinc(it[0]) # <<<<<<<<<<<<<< + * + * def dot(self, other): */ - if (PyList_CheckExact(__pyx_v_fp) || PyTuple_CheckExact(__pyx_v_fp)) { - __pyx_t_4 = __pyx_v_fp; __Pyx_INCREF(__pyx_t_4); __pyx_t_8 = 0; - __pyx_t_9 = NULL; - } else { - __pyx_t_8 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_v_fp); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __Pyx_GOTREF(__pyx_t_4); - __pyx_t_9 = Py_TYPE(__pyx_t_4)->tp_iternext; - } - for (;;) { - if (!__pyx_t_9 && PyList_CheckExact(__pyx_t_4)) { - if (__pyx_t_8 >= PyList_GET_SIZE(__pyx_t_4)) break; - __pyx_t_2 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_8); __Pyx_INCREF(__pyx_t_2); __pyx_t_8++; - } else if (!__pyx_t_9 && PyTuple_CheckExact(__pyx_t_4)) { - if (__pyx_t_8 >= PyTuple_GET_SIZE(__pyx_t_4)) break; - __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_8); __Pyx_INCREF(__pyx_t_2); __pyx_t_8++; - } else { - __pyx_t_2 = __pyx_t_9(__pyx_t_4); - if (unlikely(!__pyx_t_2)) { - if (PyErr_Occurred()) { - if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) PyErr_Clear(); - else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 54; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - } - break; - } - __Pyx_GOTREF(__pyx_t_2); - } - __Pyx_XDECREF(__pyx_v_line); - __pyx_v_line = __pyx_t_2; - __pyx_t_2 = 0; + (++(__pyx_cur_scope->__pyx_v_it[0])); + } + PyErr_SetNone(PyExc_StopIteration); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_3); + __Pyx_XDECREF(__pyx_t_4); + __Pyx_AddTraceback("__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_L0:; + __Pyx_XDECREF(__pyx_r); + __pyx_generator->resume_label = -1; + __Pyx_RefNannyFinishContext(); + return NULL; +} - /* "_cdec.pyx":55 - * with open(cfg) as fp: - * for line in fp: - * fname, value = line.split() # <<<<<<<<<<<<<< - * self.weights[fname.strip()] = float(value) +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_12SparseVector_8dot(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_8dot(PyObject *__pyx_v_self, PyObject *__pyx_v_other) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("dot (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_12SparseVector_7dot(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((PyObject *)__pyx_v_other)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":51 + * pinc(it[0]) * + * def dot(self, other): # <<<<<<<<<<<<<< + * if isinstance(other, DenseVector): + * return self.vector.dot((<DenseVector> other).vector[0]) */ - __pyx_t_2 = PyObject_GetAttr(__pyx_v_line, __pyx_n_s__split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __Pyx_GOTREF(__pyx_t_2); - __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __Pyx_GOTREF(__pyx_t_1); - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - if ((likely(PyTuple_CheckExact(__pyx_t_1))) || (PyList_CheckExact(__pyx_t_1))) { - PyObject* sequence = __pyx_t_1; - if (likely(PyTuple_CheckExact(sequence))) { - if (unlikely(PyTuple_GET_SIZE(sequence) != 2)) { - if (PyTuple_GET_SIZE(sequence) > 2) __Pyx_RaiseTooManyValuesError(2); - else __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(sequence)); - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - } - __pyx_t_2 = PyTuple_GET_ITEM(sequence, 0); - __pyx_t_10 = PyTuple_GET_ITEM(sequence, 1); - } else { - if (unlikely(PyList_GET_SIZE(sequence) != 2)) { - if (PyList_GET_SIZE(sequence) > 2) __Pyx_RaiseTooManyValuesError(2); - else __Pyx_RaiseNeedMoreValuesError(PyList_GET_SIZE(sequence)); - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - } - __pyx_t_2 = PyList_GET_ITEM(sequence, 0); - __pyx_t_10 = PyList_GET_ITEM(sequence, 1); - } - __Pyx_INCREF(__pyx_t_2); - __Pyx_INCREF(__pyx_t_10); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - } else { - Py_ssize_t index = -1; - __pyx_t_11 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __Pyx_GOTREF(__pyx_t_11); - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext; - index = 0; __pyx_t_2 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_2)) goto __pyx_L18_unpacking_failed; - __Pyx_GOTREF(__pyx_t_2); - index = 1; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L18_unpacking_failed; - __Pyx_GOTREF(__pyx_t_10); - if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; - goto __pyx_L19_unpacking_done; - __pyx_L18_unpacking_failed:; - __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; - if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration)) PyErr_Clear(); - if (!PyErr_Occurred()) __Pyx_RaiseNeedMoreValuesError(index); - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __pyx_L19_unpacking_done:; - } - __Pyx_XDECREF(__pyx_v_fname); - __pyx_v_fname = __pyx_t_2; - __pyx_t_2 = 0; - __Pyx_XDECREF(__pyx_v_value); - __pyx_v_value = __pyx_t_10; - __pyx_t_10 = 0; - /* "_cdec.pyx":56 - * for line in fp: - * fname, value = line.split() - * self.weights[fname.strip()] = float(value) # <<<<<<<<<<<<<< +static PyObject *__pyx_pf_5_cdec_12SparseVector_7dot(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, PyObject *__pyx_v_other) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("dot", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":52 * - * # TODO: list, lattice translation + * def dot(self, other): + * if isinstance(other, DenseVector): # <<<<<<<<<<<<<< + * return self.vector.dot((<DenseVector> other).vector[0]) + * elif isinstance(other, SparseVector): */ - __pyx_t_13 = __Pyx_PyObject_AsDouble(__pyx_v_value); if (unlikely(__pyx_t_13 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __pyx_t_1 = PyFloat_FromDouble(__pyx_t_13); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_10 = PyObject_GetAttr(__pyx_v_fname, __pyx_n_s__strip); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __Pyx_GOTREF(__pyx_t_10); - __pyx_t_2 = PyObject_Call(__pyx_t_10, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; - if (PyObject_SetItem(((PyObject *)__pyx_v_self->weights), __pyx_t_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L7_error;} - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - } - __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; - } - __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0; - __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0; - __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0; - goto __pyx_L14_try_end; - __pyx_L7_error:; - __Pyx_XDECREF(__pyx_t_11); __pyx_t_11 = 0; - __Pyx_XDECREF(__pyx_t_10); __pyx_t_10 = 0; - __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0; - __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0; - __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0; + __pyx_t_1 = ((PyObject *)((PyObject*)__pyx_ptype_5_cdec_DenseVector)); + __Pyx_INCREF(__pyx_t_1); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_other, __pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":53 + * def dot(self, other): + * if isinstance(other, DenseVector): + * return self.vector.dot((<DenseVector> other).vector[0]) # <<<<<<<<<<<<<< + * elif isinstance(other, SparseVector): + * return self.vector.dot((<SparseVector> other).vector[0]) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyFloat_FromDouble(__pyx_v_self->vector->dot((((struct __pyx_obj_5_cdec_DenseVector *)__pyx_v_other)->vector[0]))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + goto __pyx_L3; + } - /* "_cdec.pyx":53 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":54 + * if isinstance(other, DenseVector): + * return self.vector.dot((<DenseVector> other).vector[0]) + * elif isinstance(other, SparseVector): # <<<<<<<<<<<<<< + * return self.vector.dot((<SparseVector> other).vector[0]) + * raise ValueError('cannot take the dot product of %s and SparseVector' % type(other)) + */ + __pyx_t_1 = ((PyObject *)((PyObject*)__pyx_ptype_5_cdec_SparseVector)); + __Pyx_INCREF(__pyx_t_1); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_other, __pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":55 + * return self.vector.dot((<DenseVector> other).vector[0]) + * elif isinstance(other, SparseVector): + * return self.vector.dot((<SparseVector> other).vector[0]) # <<<<<<<<<<<<<< + * raise ValueError('cannot take the dot product of %s and SparseVector' % type(other)) * - * def read_weights(self, cfg): - * with open(cfg) as fp: # <<<<<<<<<<<<<< - * for line in fp: - * fname, value = line.split() */ - /*except:*/ { - __Pyx_AddTraceback("_cdec.Decoder.read_weights", __pyx_clineno, __pyx_lineno, __pyx_filename); - if (__Pyx_GetException(&__pyx_t_4, &__pyx_t_1, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} - __Pyx_GOTREF(__pyx_t_4); - __Pyx_GOTREF(__pyx_t_1); - __Pyx_GOTREF(__pyx_t_2); - __pyx_t_10 = PyTuple_New(3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} - __Pyx_GOTREF(__pyx_t_10); - __Pyx_INCREF(__pyx_t_4); - PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_4); - __Pyx_GIVEREF(__pyx_t_4); - __Pyx_INCREF(__pyx_t_1); - PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_t_1); - __Pyx_GIVEREF(__pyx_t_1); - __Pyx_INCREF(__pyx_t_2); - PyTuple_SET_ITEM(__pyx_t_10, 2, __pyx_t_2); - __Pyx_GIVEREF(__pyx_t_2); - __pyx_t_15 = PyObject_Call(__pyx_t_3, __pyx_t_10, NULL); - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} - __Pyx_GOTREF(__pyx_t_15); - __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_15); - __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0; - if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} - __pyx_t_16 = (!__pyx_t_14); - if (__pyx_t_16) { - __Pyx_GIVEREF(__pyx_t_4); - __Pyx_GIVEREF(__pyx_t_1); - __Pyx_GIVEREF(__pyx_t_2); - __Pyx_ErrRestore(__pyx_t_4, __pyx_t_1, __pyx_t_2); - __pyx_t_4 = 0; __pyx_t_1 = 0; __pyx_t_2 = 0; - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} - goto __pyx_L22; - } - __pyx_L22:; - __Pyx_DECREF(((PyObject *)__pyx_t_10)); __pyx_t_10 = 0; - __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; - __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - goto __pyx_L8_exception_handled; - } - __pyx_L9_except_error:; - __Pyx_XGIVEREF(__pyx_t_5); - __Pyx_XGIVEREF(__pyx_t_6); - __Pyx_XGIVEREF(__pyx_t_7); - __Pyx_ExceptionReset(__pyx_t_5, __pyx_t_6, __pyx_t_7); - goto __pyx_L1_error; - __pyx_L8_exception_handled:; - __Pyx_XGIVEREF(__pyx_t_5); - __Pyx_XGIVEREF(__pyx_t_6); - __Pyx_XGIVEREF(__pyx_t_7); - __Pyx_ExceptionReset(__pyx_t_5, __pyx_t_6, __pyx_t_7); - __pyx_L14_try_end:; - } - } - /*finally:*/ { - if (__pyx_t_3) { - __pyx_t_7 = PyObject_Call(__pyx_t_3, __pyx_k_tuple_1, NULL); - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_7); - __pyx_t_16 = __Pyx_PyObject_IsTrue(__pyx_t_7); - __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; - if (unlikely(__pyx_t_16 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - } - } - goto __pyx_L23; - __pyx_L3_error:; - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - goto __pyx_L1_error; - __pyx_L23:; + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyFloat_FromDouble(__pyx_v_self->vector->dot((((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_other)->vector[0]))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 55; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + goto __pyx_L3; } + __pyx_L3:; + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":56 + * elif isinstance(other, SparseVector): + * return self.vector.dot((<SparseVector> other).vector[0]) + * raise ValueError('cannot take the dot product of %s and SparseVector' % type(other)) # <<<<<<<<<<<<<< + * + * def todense(self): + */ + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_1), ((PyObject *)Py_TYPE(__pyx_v_other))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __Pyx_Raise(__pyx_t_1, 0, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[1]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_r = Py_None; __Pyx_INCREF(Py_None); goto __pyx_L0; __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); - __Pyx_XDECREF(__pyx_t_4); - __Pyx_XDECREF(__pyx_t_10); - __Pyx_XDECREF(__pyx_t_11); - __Pyx_AddTraceback("_cdec.Decoder.read_weights", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_XDECREF(__pyx_t_3); + __Pyx_AddTraceback("_cdec.SparseVector.dot", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; - __Pyx_XDECREF(__pyx_v_fp); - __Pyx_XDECREF(__pyx_v_line); - __Pyx_XDECREF(__pyx_v_fname); - __Pyx_XDECREF(__pyx_v_value); __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_7Decoder_7translate(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ -static PyObject *__pyx_pw_5_cdec_7Decoder_7translate(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { - PyObject *__pyx_v_sentence = 0; - PyObject *__pyx_v_grammar = 0; - static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__sentence,&__pyx_n_s__grammar,0}; +static PyObject *__pyx_pw_5_cdec_12SparseVector_10todense(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_10todense(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("translate (wrapper)", 0); - { - PyObject* values[2] = {0,0}; + __Pyx_RefNannySetupContext("todense (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_12SparseVector_9todense(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} - /* "_cdec.pyx":59 +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":58 + * raise ValueError('cannot take the dot product of %s and SparseVector' % type(other)) * - * # TODO: list, lattice translation - * def translate(self, unicode sentence, grammar=None): # <<<<<<<<<<<<<< - * if grammar: - * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) + * def todense(self): # <<<<<<<<<<<<<< + * cdef DenseVector dense = DenseVector() + * dense.vector = new vector[weight_t]() */ - values[1] = ((PyObject *)Py_None); - if (unlikely(__pyx_kwds)) { - Py_ssize_t kw_args; - const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); - switch (pos_args) { - case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); - case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - case 0: break; - default: goto __pyx_L5_argtuple_error; - } - kw_args = PyDict_Size(__pyx_kwds); - switch (pos_args) { - case 0: - values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sentence); - if (likely(values[0])) kw_args--; - else goto __pyx_L5_argtuple_error; - case 1: - if (kw_args > 0) { - PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__grammar); - if (value) { values[1] = value; kw_args--; } - } - } - if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "translate") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L3_error;} - } - } else { - switch (PyTuple_GET_SIZE(__pyx_args)) { - case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); - case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); - break; - default: goto __pyx_L5_argtuple_error; - } - } - __pyx_v_sentence = ((PyObject*)values[0]); - __pyx_v_grammar = values[1]; - } - goto __pyx_L4_argument_unpacking_done; - __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("translate", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L3_error;} - __pyx_L3_error:; - __Pyx_AddTraceback("_cdec.Decoder.translate", __pyx_clineno, __pyx_lineno, __pyx_filename); + +static PyObject *__pyx_pf_5_cdec_12SparseVector_9todense(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self) { + struct __pyx_obj_5_cdec_DenseVector *__pyx_v_dense = 0; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("todense", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":59 + * + * def todense(self): + * cdef DenseVector dense = DenseVector() # <<<<<<<<<<<<<< + * dense.vector = new vector[weight_t]() + * self.vector.init_vector(dense.vector) + */ + __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_DenseVector)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_v_dense = ((struct __pyx_obj_5_cdec_DenseVector *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":60 + * def todense(self): + * cdef DenseVector dense = DenseVector() + * dense.vector = new vector[weight_t]() # <<<<<<<<<<<<<< + * self.vector.init_vector(dense.vector) + * return dense + */ + __pyx_v_dense->vector = new std::vector<weight_t>(); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":61 + * cdef DenseVector dense = DenseVector() + * dense.vector = new vector[weight_t]() + * self.vector.init_vector(dense.vector) # <<<<<<<<<<<<<< + * return dense + * + */ + __pyx_v_self->vector->init_vector(__pyx_v_dense->vector); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":62 + * dense.vector = new vector[weight_t]() + * self.vector.init_vector(dense.vector) + * return dense # <<<<<<<<<<<<<< + * + * def __richcmp__(SparseVector self, SparseVector other, int op): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_dense)); + __pyx_r = ((PyObject *)__pyx_v_dense); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("_cdec.SparseVector.todense", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XDECREF((PyObject *)__pyx_v_dense); + __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); - return NULL; - __pyx_L4_argument_unpacking_done:; - if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_sentence), (&PyUnicode_Type), 1, "sentence", 1))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 59; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_r = __pyx_pf_5_cdec_7Decoder_6translate(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self), __pyx_v_sentence, __pyx_v_grammar); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_12SparseVector_12__richcmp__(PyObject *__pyx_v_self, PyObject *__pyx_v_other, int __pyx_v_op); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_12__richcmp__(PyObject *__pyx_v_self, PyObject *__pyx_v_other, int __pyx_v_op) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__richcmp__ (wrapper)", 0); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_self), __pyx_ptype_5_cdec_SparseVector, 1, "self", 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_5_cdec_SparseVector, 1, "other", 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_r = __pyx_pf_5_cdec_12SparseVector_11__richcmp__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_other), ((int)__pyx_v_op)); goto __pyx_L0; __pyx_L1_error:; __pyx_r = NULL; @@ -1760,187 +2017,368 @@ static PyObject *__pyx_pw_5_cdec_7Decoder_7translate(PyObject *__pyx_v_self, PyO return __pyx_r; } -static PyObject *__pyx_pf_5_cdec_7Decoder_6translate(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, PyObject *__pyx_v_sentence, PyObject *__pyx_v_grammar) { - PyObject *__pyx_v_inp = NULL; - BasicObserver __pyx_v_observer; - struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_hg = 0; +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":64 + * return dense + * + * def __richcmp__(SparseVector self, SparseVector other, int op): # <<<<<<<<<<<<<< + * if op == 2: # == + * return self.vector[0] == other.vector[0] + */ + +static PyObject *__pyx_pf_5_cdec_12SparseVector_11__richcmp__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other, int __pyx_v_op) { PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations - int __pyx_t_1; - char *__pyx_t_2; - PyObject *__pyx_t_3 = NULL; - PyObject *__pyx_t_4 = NULL; + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; - __Pyx_RefNannySetupContext("translate", 0); + __Pyx_RefNannySetupContext("__richcmp__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":67 + * if op == 2: # == + * return self.vector[0] == other.vector[0] + * elif op == 3: # != # <<<<<<<<<<<<<< + * return not (self == other) + * raise NotImplemented('comparison not implemented for SparseVector') + */ + switch (__pyx_v_op) { - /* "_cdec.pyx":60 - * # TODO: list, lattice translation - * def translate(self, unicode sentence, grammar=None): - * if grammar: # <<<<<<<<<<<<<< - * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) - * inp = sentence.strip().encode('utf8') + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":65 + * + * def __richcmp__(SparseVector self, SparseVector other, int op): + * if op == 2: # == # <<<<<<<<<<<<<< + * return self.vector[0] == other.vector[0] + * elif op == 3: # != */ - __pyx_t_1 = __Pyx_PyObject_IsTrue(__pyx_v_grammar); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 60; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - if (__pyx_t_1) { + case 2: + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":66 + * def __richcmp__(SparseVector self, SparseVector other, int op): + * if op == 2: # == + * return self.vector[0] == other.vector[0] # <<<<<<<<<<<<<< + * elif op == 3: # != + * return not (self == other) + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyBool_FromLong(((__pyx_v_self->vector[0]) == (__pyx_v_other->vector[0]))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + break; + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":67 + * if op == 2: # == + * return self.vector[0] == other.vector[0] + * elif op == 3: # != # <<<<<<<<<<<<<< + * return not (self == other) + * raise NotImplemented('comparison not implemented for SparseVector') + */ + case 3: - /* "_cdec.pyx":61 - * def translate(self, unicode sentence, grammar=None): - * if grammar: - * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) # <<<<<<<<<<<<<< - * inp = sentence.strip().encode('utf8') - * cdef decoder.BasicObserver observer = decoder.BasicObserver() + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":68 + * return self.vector[0] == other.vector[0] + * elif op == 3: # != + * return not (self == other) # <<<<<<<<<<<<<< + * raise NotImplemented('comparison not implemented for SparseVector') + * */ - __pyx_t_2 = PyBytes_AsString(__pyx_v_grammar); if (unlikely((!__pyx_t_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 61; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_v_self->dec->SetSentenceGrammarFromString(std::string(((char *)__pyx_t_2))); - goto __pyx_L3; + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_RichCompare(((PyObject *)__pyx_v_self), ((PyObject *)__pyx_v_other), Py_EQ); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 68; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_t_1); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 68; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = __Pyx_PyBool_FromLong((!__pyx_t_2)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 68; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + break; } - __pyx_L3:; - /* "_cdec.pyx":62 - * if grammar: - * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) - * inp = sentence.strip().encode('utf8') # <<<<<<<<<<<<<< - * cdef decoder.BasicObserver observer = decoder.BasicObserver() - * self.dec.Decode(string(<char *>inp), &observer) + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":69 + * elif op == 3: # != + * return not (self == other) + * raise NotImplemented('comparison not implemented for SparseVector') # <<<<<<<<<<<<<< + * + * def __len__(self): */ - __pyx_t_3 = PyObject_GetAttr(((PyObject *)__pyx_v_sentence), __pyx_n_s__strip); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_3); - __pyx_t_4 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_4); - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - __pyx_t_3 = PyObject_GetAttr(__pyx_t_4, __pyx_n_s__encode); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_3); - __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; - __pyx_t_4 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_k_tuple_2), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_4); - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - __pyx_v_inp = __pyx_t_4; - __pyx_t_4 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_NotImplemented, ((PyObject *)__pyx_k_tuple_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_Raise(__pyx_t_1, 0, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[1]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - /* "_cdec.pyx":63 - * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) - * inp = sentence.strip().encode('utf8') - * cdef decoder.BasicObserver observer = decoder.BasicObserver() # <<<<<<<<<<<<<< - * self.dec.Decode(string(<char *>inp), &observer) - * if observer.hypergraph == NULL: + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("_cdec.SparseVector.__richcmp__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static Py_ssize_t __pyx_pw_5_cdec_12SparseVector_14__len__(PyObject *__pyx_v_self); /*proto*/ +static Py_ssize_t __pyx_pw_5_cdec_12SparseVector_14__len__(PyObject *__pyx_v_self) { + Py_ssize_t __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__len__ (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_12SparseVector_13__len__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":71 + * raise NotImplemented('comparison not implemented for SparseVector') + * + * def __len__(self): # <<<<<<<<<<<<<< + * return self.vector.size() + * */ - __pyx_v_observer = BasicObserver(); - /* "_cdec.pyx":64 - * inp = sentence.strip().encode('utf8') - * cdef decoder.BasicObserver observer = decoder.BasicObserver() - * self.dec.Decode(string(<char *>inp), &observer) # <<<<<<<<<<<<<< - * if observer.hypergraph == NULL: - * raise ParseFailed() +static Py_ssize_t __pyx_pf_5_cdec_12SparseVector_13__len__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self) { + Py_ssize_t __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__len__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":72 + * + * def __len__(self): + * return self.vector.size() # <<<<<<<<<<<<<< + * + * def __contains__(self, char* fname): */ - __pyx_t_2 = PyBytes_AsString(__pyx_v_inp); if (unlikely((!__pyx_t_2) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_v_self->dec->Decode(std::string(((char *)__pyx_t_2)), (&__pyx_v_observer)); + __pyx_r = __pyx_v_self->vector->size(); + goto __pyx_L0; - /* "_cdec.pyx":65 - * cdef decoder.BasicObserver observer = decoder.BasicObserver() - * self.dec.Decode(string(<char *>inp), &observer) - * if observer.hypergraph == NULL: # <<<<<<<<<<<<<< - * raise ParseFailed() - * cdef Hypergraph hg = Hypergraph() + __pyx_r = 0; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static int __pyx_pw_5_cdec_12SparseVector_16__contains__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname); /*proto*/ +static int __pyx_pw_5_cdec_12SparseVector_16__contains__(PyObject *__pyx_v_self, PyObject *__pyx_arg_fname) { + char *__pyx_v_fname; + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__contains__ (wrapper)", 0); + assert(__pyx_arg_fname); { + __pyx_v_fname = PyBytes_AsString(__pyx_arg_fname); if (unlikely((!__pyx_v_fname) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 74; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L3_error:; + __Pyx_AddTraceback("_cdec.SparseVector.__contains__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + __pyx_r = __pyx_pf_5_cdec_12SparseVector_15__contains__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((char *)__pyx_v_fname)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":74 + * return self.vector.size() + * + * def __contains__(self, char* fname): # <<<<<<<<<<<<<< + * return self.vector.nonzero(FDConvert(fname)) + * */ - __pyx_t_1 = (__pyx_v_observer.hypergraph == NULL); - if (__pyx_t_1) { - /* "_cdec.pyx":66 - * self.dec.Decode(string(<char *>inp), &observer) - * if observer.hypergraph == NULL: - * raise ParseFailed() # <<<<<<<<<<<<<< - * cdef Hypergraph hg = Hypergraph() - * hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0]) +static int __pyx_pf_5_cdec_12SparseVector_15__contains__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, char *__pyx_v_fname) { + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__contains__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":75 + * + * def __contains__(self, char* fname): + * return self.vector.nonzero(FDConvert(fname)) # <<<<<<<<<<<<<< + * + * def __iadd__(SparseVector self, SparseVector other): */ - __pyx_t_4 = __Pyx_GetName(__pyx_m, __pyx_n_s__ParseFailed); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_4); - __pyx_t_3 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_3); - __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; - __Pyx_Raise(__pyx_t_3, 0, 0, 0); - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 66; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - goto __pyx_L4; - } - __pyx_L4:; + __pyx_r = __pyx_v_self->vector->nonzero(FD::Convert(__pyx_v_fname)); + goto __pyx_L0; - /* "_cdec.pyx":67 - * if observer.hypergraph == NULL: - * raise ParseFailed() - * cdef Hypergraph hg = Hypergraph() # <<<<<<<<<<<<<< - * hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0]) - * return hg + __pyx_r = 0; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_12SparseVector_18__iadd__(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_18__iadd__(PyObject *__pyx_v_self, PyObject *__pyx_v_other) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__iadd__ (wrapper)", 0); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_5_cdec_SparseVector, 1, "other", 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 77; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_r = __pyx_pf_5_cdec_12SparseVector_17__iadd__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_other)); + goto __pyx_L0; + __pyx_L1_error:; + __pyx_r = NULL; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":77 + * return self.vector.nonzero(FDConvert(fname)) + * + * def __iadd__(SparseVector self, SparseVector other): # <<<<<<<<<<<<<< + * self.vector[0] += other.vector[0] + * return self */ - __pyx_t_3 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_Hypergraph)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 67; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_3); - __pyx_v_hg = ((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_t_3); - __pyx_t_3 = 0; - /* "_cdec.pyx":68 - * raise ParseFailed() - * cdef Hypergraph hg = Hypergraph() - * hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0]) # <<<<<<<<<<<<<< - * return hg +static PyObject *__pyx_pf_5_cdec_12SparseVector_17__iadd__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__iadd__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":78 + * + * def __iadd__(SparseVector self, SparseVector other): + * self.vector[0] += other.vector[0] # <<<<<<<<<<<<<< + * return self * */ - __pyx_v_hg->hg = new Hypergraph((__pyx_v_observer.hypergraph[0])); + (__pyx_v_self->vector[0]) += (__pyx_v_other->vector[0]); - /* "_cdec.pyx":69 - * cdef Hypergraph hg = Hypergraph() - * hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0]) - * return hg # <<<<<<<<<<<<<< + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":79 + * def __iadd__(SparseVector self, SparseVector other): + * self.vector[0] += other.vector[0] + * return self # <<<<<<<<<<<<<< * - * cdef class Hypergraph: + * def __isub__(SparseVector self, SparseVector other): */ __Pyx_XDECREF(__pyx_r); - __Pyx_INCREF(((PyObject *)__pyx_v_hg)); - __pyx_r = ((PyObject *)__pyx_v_hg); + __Pyx_INCREF(((PyObject *)__pyx_v_self)); + __pyx_r = ((PyObject *)__pyx_v_self); goto __pyx_L0; __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_12SparseVector_20__isub__(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_20__isub__(PyObject *__pyx_v_self, PyObject *__pyx_v_other) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__isub__ (wrapper)", 0); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_5_cdec_SparseVector, 1, "other", 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_r = __pyx_pf_5_cdec_12SparseVector_19__isub__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_other)); goto __pyx_L0; __pyx_L1_error:; - __Pyx_XDECREF(__pyx_t_3); - __Pyx_XDECREF(__pyx_t_4); - __Pyx_AddTraceback("_cdec.Decoder.translate", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; - __Pyx_XDECREF(__pyx_v_inp); - __Pyx_XDECREF((PyObject *)__pyx_v_hg); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":81 + * return self + * + * def __isub__(SparseVector self, SparseVector other): # <<<<<<<<<<<<<< + * self.vector[0] -= other.vector[0] + * return self + */ + +static PyObject *__pyx_pf_5_cdec_12SparseVector_19__isub__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__isub__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":82 + * + * def __isub__(SparseVector self, SparseVector other): + * self.vector[0] -= other.vector[0] # <<<<<<<<<<<<<< + * return self + * + */ + (__pyx_v_self->vector[0]) -= (__pyx_v_other->vector[0]); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":83 + * def __isub__(SparseVector self, SparseVector other): + * self.vector[0] -= other.vector[0] + * return self # <<<<<<<<<<<<<< + * + * def __imul__(SparseVector self, float scalar): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_self)); + __pyx_r = ((PyObject *)__pyx_v_self); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_7Decoder_7weights_1__get__(PyObject *__pyx_v_self); /*proto*/ -static PyObject *__pyx_pw_5_cdec_7Decoder_7weights_1__get__(PyObject *__pyx_v_self) { +static PyObject *__pyx_pw_5_cdec_12SparseVector_22__imul__(PyObject *__pyx_v_self, PyObject *__pyx_arg_scalar); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_22__imul__(PyObject *__pyx_v_self, PyObject *__pyx_arg_scalar) { + float __pyx_v_scalar; PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__get__ (wrapper)", 0); - __pyx_r = __pyx_pf_5_cdec_7Decoder_7weights___get__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self)); + __Pyx_RefNannySetupContext("__imul__ (wrapper)", 0); + assert(__pyx_arg_scalar); { + __pyx_v_scalar = __pyx_PyFloat_AsFloat(__pyx_arg_scalar); if (unlikely((__pyx_v_scalar == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 85; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L3_error:; + __Pyx_AddTraceback("_cdec.SparseVector.__imul__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_r = __pyx_pf_5_cdec_12SparseVector_21__imul__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((float)__pyx_v_scalar)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":40 - * cdef class Decoder: - * cdef decoder.Decoder* dec - * cdef public Weights weights # <<<<<<<<<<<<<< +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":85 + * return self * - * def __cinit__(self, char* config): + * def __imul__(SparseVector self, float scalar): # <<<<<<<<<<<<<< + * self.vector[0] *= scalar + * return self */ -static PyObject *__pyx_pf_5_cdec_7Decoder_7weights___get__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self) { +static PyObject *__pyx_pf_5_cdec_12SparseVector_21__imul__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, float __pyx_v_scalar) { PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__get__", 0); + __Pyx_RefNannySetupContext("__imul__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":86 + * + * def __imul__(SparseVector self, float scalar): + * self.vector[0] *= scalar # <<<<<<<<<<<<<< + * return self + * + */ + (__pyx_v_self->vector[0]) *= __pyx_v_scalar; + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":87 + * def __imul__(SparseVector self, float scalar): + * self.vector[0] *= scalar + * return self # <<<<<<<<<<<<<< + * + * def __idiv__(SparseVector self, float scalar): + */ __Pyx_XDECREF(__pyx_r); - __Pyx_INCREF(((PyObject *)__pyx_v_self->weights)); - __pyx_r = ((PyObject *)__pyx_v_self->weights); + __Pyx_INCREF(((PyObject *)__pyx_v_self)); + __pyx_r = ((PyObject *)__pyx_v_self); goto __pyx_L0; __pyx_r = Py_None; __Pyx_INCREF(Py_None); @@ -1951,62 +2389,226 @@ static PyObject *__pyx_pf_5_cdec_7Decoder_7weights___get__(struct __pyx_obj_5_cd } /* Python wrapper */ -static int __pyx_pw_5_cdec_7Decoder_7weights_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_value); /*proto*/ -static int __pyx_pw_5_cdec_7Decoder_7weights_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_value) { - int __pyx_r; +#if PY_MAJOR_VERSION < 3 +static PyObject *__pyx_pw_5_cdec_12SparseVector_24__idiv__(PyObject *__pyx_v_self, PyObject *__pyx_arg_scalar); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_24__idiv__(PyObject *__pyx_v_self, PyObject *__pyx_arg_scalar) { + float __pyx_v_scalar; + PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__set__ (wrapper)", 0); - __pyx_r = __pyx_pf_5_cdec_7Decoder_7weights_2__set__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self), ((PyObject *)__pyx_v_value)); + __Pyx_RefNannySetupContext("__idiv__ (wrapper)", 0); + assert(__pyx_arg_scalar); { + __pyx_v_scalar = __pyx_PyFloat_AsFloat(__pyx_arg_scalar); if (unlikely((__pyx_v_scalar == (float)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 89; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L3_error:; + __Pyx_AddTraceback("_cdec.SparseVector.__idiv__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_r = __pyx_pf_5_cdec_12SparseVector_23__idiv__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((float)__pyx_v_scalar)); __Pyx_RefNannyFinishContext(); return __pyx_r; } +#endif /*!(#if PY_MAJOR_VERSION < 3)*/ -static int __pyx_pf_5_cdec_7Decoder_7weights_2__set__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, PyObject *__pyx_v_value) { - int __pyx_r; +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":89 + * return self + * + * def __idiv__(SparseVector self, float scalar): # <<<<<<<<<<<<<< + * self.vector[0] /= scalar + * return self + */ + +#if PY_MAJOR_VERSION < 3 +static PyObject *__pyx_pf_5_cdec_12SparseVector_23__idiv__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, float __pyx_v_scalar) { + PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__idiv__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":90 + * + * def __idiv__(SparseVector self, float scalar): + * self.vector[0] /= scalar # <<<<<<<<<<<<<< + * return self + * + */ + (__pyx_v_self->vector[0]) /= __pyx_v_scalar; + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":91 + * def __idiv__(SparseVector self, float scalar): + * self.vector[0] /= scalar + * return self # <<<<<<<<<<<<<< + * + * def __add__(SparseVector self, SparseVector other): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_self)); + __pyx_r = ((PyObject *)__pyx_v_self); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} +#endif /*!(#if PY_MAJOR_VERSION < 3)*/ + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_12SparseVector_26__add__(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_26__add__(PyObject *__pyx_v_self, PyObject *__pyx_v_other) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__add__ (wrapper)", 0); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_self), __pyx_ptype_5_cdec_SparseVector, 1, "self", 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 93; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_5_cdec_SparseVector, 1, "other", 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 93; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_r = __pyx_pf_5_cdec_12SparseVector_25__add__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_other)); + goto __pyx_L0; + __pyx_L1_error:; + __pyx_r = NULL; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":93 + * return self + * + * def __add__(SparseVector self, SparseVector other): # <<<<<<<<<<<<<< + * cdef SparseVector result = SparseVector() + * result.vector = new FastSparseVector[weight_t](self.vector[0] + other.vector[0]) + */ + +static PyObject *__pyx_pf_5_cdec_12SparseVector_25__add__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other) { + struct __pyx_obj_5_cdec_SparseVector *__pyx_v_result = 0; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; - __Pyx_RefNannySetupContext("__set__", 0); - if (!(likely(((__pyx_v_value) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_value, __pyx_ptype_5_cdec_Weights))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_INCREF(__pyx_v_value); - __Pyx_GIVEREF(__pyx_v_value); - __Pyx_GOTREF(__pyx_v_self->weights); - __Pyx_DECREF(((PyObject *)__pyx_v_self->weights)); - __pyx_v_self->weights = ((struct __pyx_obj_5_cdec_Weights *)__pyx_v_value); + __Pyx_RefNannySetupContext("__add__", 0); - __pyx_r = 0; + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":94 + * + * def __add__(SparseVector self, SparseVector other): + * cdef SparseVector result = SparseVector() # <<<<<<<<<<<<<< + * result.vector = new FastSparseVector[weight_t](self.vector[0] + other.vector[0]) + * return result + */ + __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_SparseVector)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 94; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_v_result = ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":95 + * def __add__(SparseVector self, SparseVector other): + * cdef SparseVector result = SparseVector() + * result.vector = new FastSparseVector[weight_t](self.vector[0] + other.vector[0]) # <<<<<<<<<<<<<< + * return result + * + */ + __pyx_v_result->vector = new FastSparseVector<weight_t>(((__pyx_v_self->vector[0]) + (__pyx_v_other->vector[0]))); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":96 + * cdef SparseVector result = SparseVector() + * result.vector = new FastSparseVector[weight_t](self.vector[0] + other.vector[0]) + * return result # <<<<<<<<<<<<<< + * + * def __sub__(SparseVector self, SparseVector other): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_result)); + __pyx_r = ((PyObject *)__pyx_v_result); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); goto __pyx_L0; __pyx_L1_error:; - __Pyx_AddTraceback("_cdec.Decoder.weights.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = -1; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("_cdec.SparseVector.__add__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; __pyx_L0:; + __Pyx_XDECREF((PyObject *)__pyx_v_result); + __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* Python wrapper */ -static int __pyx_pw_5_cdec_7Decoder_7weights_5__del__(PyObject *__pyx_v_self); /*proto*/ -static int __pyx_pw_5_cdec_7Decoder_7weights_5__del__(PyObject *__pyx_v_self) { - int __pyx_r; +static PyObject *__pyx_pw_5_cdec_12SparseVector_28__sub__(PyObject *__pyx_v_self, PyObject *__pyx_v_other); /*proto*/ +static PyObject *__pyx_pw_5_cdec_12SparseVector_28__sub__(PyObject *__pyx_v_self, PyObject *__pyx_v_other) { + PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__del__ (wrapper)", 0); - __pyx_r = __pyx_pf_5_cdec_7Decoder_7weights_4__del__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self)); + __Pyx_RefNannySetupContext("__sub__ (wrapper)", 0); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_self), __pyx_ptype_5_cdec_SparseVector, 1, "self", 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 98; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_other), __pyx_ptype_5_cdec_SparseVector, 1, "other", 0))) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 98; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_r = __pyx_pf_5_cdec_12SparseVector_27__sub__(((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_self), ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_other)); + goto __pyx_L0; + __pyx_L1_error:; + __pyx_r = NULL; + __pyx_L0:; __Pyx_RefNannyFinishContext(); return __pyx_r; } -static int __pyx_pf_5_cdec_7Decoder_7weights_4__del__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self) { - int __pyx_r; +/* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":98 + * return result + * + * def __sub__(SparseVector self, SparseVector other): # <<<<<<<<<<<<<< + * cdef SparseVector result = SparseVector() + * result.vector = new FastSparseVector[weight_t](self.vector[0] - other.vector[0]) + */ + +static PyObject *__pyx_pf_5_cdec_12SparseVector_27__sub__(struct __pyx_obj_5_cdec_SparseVector *__pyx_v_self, struct __pyx_obj_5_cdec_SparseVector *__pyx_v_other) { + struct __pyx_obj_5_cdec_SparseVector *__pyx_v_result = 0; + PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("__del__", 0); - __Pyx_INCREF(Py_None); - __Pyx_GIVEREF(Py_None); - __Pyx_GOTREF(__pyx_v_self->weights); - __Pyx_DECREF(((PyObject *)__pyx_v_self->weights)); - __pyx_v_self->weights = ((struct __pyx_obj_5_cdec_Weights *)Py_None); + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("__sub__", 0); - __pyx_r = 0; + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":99 + * + * def __sub__(SparseVector self, SparseVector other): + * cdef SparseVector result = SparseVector() # <<<<<<<<<<<<<< + * result.vector = new FastSparseVector[weight_t](self.vector[0] - other.vector[0]) + * return result + */ + __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_SparseVector)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 99; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_v_result = ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":100 + * def __sub__(SparseVector self, SparseVector other): + * cdef SparseVector result = SparseVector() + * result.vector = new FastSparseVector[weight_t](self.vector[0] - other.vector[0]) # <<<<<<<<<<<<<< + * return result + */ + __pyx_v_result->vector = new FastSparseVector<weight_t>(((__pyx_v_self->vector[0]) - (__pyx_v_other->vector[0]))); + + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":101 + * cdef SparseVector result = SparseVector() + * result.vector = new FastSparseVector[weight_t](self.vector[0] - other.vector[0]) + * return result # <<<<<<<<<<<<<< + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_result)); + __pyx_r = ((PyObject *)__pyx_v_result); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("_cdec.SparseVector.__sub__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XDECREF((PyObject *)__pyx_v_result); + __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } @@ -2020,7 +2622,7 @@ static void __pyx_pw_5_cdec_10Hypergraph_1__dealloc__(PyObject *__pyx_v_self) { __Pyx_RefNannyFinishContext(); } -/* "_cdec.pyx":75 +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":8 * cdef MT19937* rng * * def __dealloc__(self): # <<<<<<<<<<<<<< @@ -2033,7 +2635,7 @@ static void __pyx_pf_5_cdec_10Hypergraph___dealloc__(struct __pyx_obj_5_cdec_Hyp int __pyx_t_1; __Pyx_RefNannySetupContext("__dealloc__", 0); - /* "_cdec.pyx":76 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":9 * * def __dealloc__(self): * del self.hg # <<<<<<<<<<<<<< @@ -2042,7 +2644,7 @@ static void __pyx_pf_5_cdec_10Hypergraph___dealloc__(struct __pyx_obj_5_cdec_Hyp */ delete __pyx_v_self->hg; - /* "_cdec.pyx":77 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":10 * def __dealloc__(self): * del self.hg * if self.rng != NULL: # <<<<<<<<<<<<<< @@ -2052,7 +2654,7 @@ static void __pyx_pf_5_cdec_10Hypergraph___dealloc__(struct __pyx_obj_5_cdec_Hyp __pyx_t_1 = (__pyx_v_self->rng != NULL); if (__pyx_t_1) { - /* "_cdec.pyx":78 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":11 * del self.hg * if self.rng != NULL: * del self.rng # <<<<<<<<<<<<<< @@ -2078,12 +2680,12 @@ static PyObject *__pyx_pw_5_cdec_10Hypergraph_3viterbi(PyObject *__pyx_v_self, C return __pyx_r; } -/* "_cdec.pyx":80 +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":13 * del self.rng * * def viterbi(self): # <<<<<<<<<<<<<< - * assert (self.hg != NULL) * cdef vector[WordID] trans + * hypergraph.ViterbiESentence(self.hg[0], &trans) */ static PyObject *__pyx_pf_5_cdec_10Hypergraph_2viterbi(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self) { @@ -2098,22 +2700,8 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_2viterbi(struct __pyx_obj_5_cdec_H int __pyx_clineno = 0; __Pyx_RefNannySetupContext("viterbi", 0); - /* "_cdec.pyx":81 - * + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":15 * def viterbi(self): - * assert (self.hg != NULL) # <<<<<<<<<<<<<< - * cdef vector[WordID] trans - * hypergraph.ViterbiESentence(self.hg[0], &trans) - */ - #ifndef CYTHON_WITHOUT_ASSERTIONS - if (unlikely(!(__pyx_v_self->hg != NULL))) { - PyErr_SetNone(PyExc_AssertionError); - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - } - #endif - - /* "_cdec.pyx":83 - * assert (self.hg != NULL) * cdef vector[WordID] trans * hypergraph.ViterbiESentence(self.hg[0], &trans) # <<<<<<<<<<<<<< * cdef str sentence = GetString(trans).c_str() @@ -2121,20 +2709,20 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_2viterbi(struct __pyx_obj_5_cdec_H */ ViterbiESentence((__pyx_v_self->hg[0]), (&__pyx_v_trans)); - /* "_cdec.pyx":84 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":16 * cdef vector[WordID] trans * hypergraph.ViterbiESentence(self.hg[0], &trans) * cdef str sentence = GetString(trans).c_str() # <<<<<<<<<<<<<< * return sentence.decode('utf8') * */ - __pyx_t_1 = PyBytes_FromString(TD::GetString(__pyx_v_trans).c_str()); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 84; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = PyBytes_FromString(TD::GetString(__pyx_v_trans).c_str()); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_1)); - if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_1)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_1))->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 84; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_1)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_1))->tp_name), 0))) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 16; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_v_sentence = ((PyObject*)__pyx_t_1); __pyx_t_1 = 0; - /* "_cdec.pyx":85 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":17 * hypergraph.ViterbiESentence(self.hg[0], &trans) * cdef str sentence = GetString(trans).c_str() * return sentence.decode('utf8') # <<<<<<<<<<<<<< @@ -2142,9 +2730,9 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_2viterbi(struct __pyx_obj_5_cdec_H * def viterbi_tree(self): */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_sentence), __pyx_n_s__decode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 85; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_sentence), __pyx_n_s__decode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); - __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_k_tuple_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 85; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_k_tuple_4), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_2); __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; __pyx_r = __pyx_t_2; @@ -2176,12 +2764,12 @@ static PyObject *__pyx_pw_5_cdec_10Hypergraph_5viterbi_tree(PyObject *__pyx_v_se return __pyx_r; } -/* "_cdec.pyx":87 +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":19 * return sentence.decode('utf8') * * def viterbi_tree(self): # <<<<<<<<<<<<<< - * assert (self.hg != NULL) * cdef str tree = hypergraph.ViterbiETree(self.hg[0]).c_str() + * return tree.decode('utf8') */ static PyObject *__pyx_pf_5_cdec_10Hypergraph_4viterbi_tree(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self) { @@ -2195,44 +2783,104 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_4viterbi_tree(struct __pyx_obj_5_c int __pyx_clineno = 0; __Pyx_RefNannySetupContext("viterbi_tree", 0); - /* "_cdec.pyx":88 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":20 * * def viterbi_tree(self): - * assert (self.hg != NULL) # <<<<<<<<<<<<<< - * cdef str tree = hypergraph.ViterbiETree(self.hg[0]).c_str() + * cdef str tree = hypergraph.ViterbiETree(self.hg[0]).c_str() # <<<<<<<<<<<<<< * return tree.decode('utf8') + * */ - #ifndef CYTHON_WITHOUT_ASSERTIONS - if (unlikely(!(__pyx_v_self->hg != NULL))) { - PyErr_SetNone(PyExc_AssertionError); - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 88; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - } - #endif + __pyx_t_1 = PyBytes_FromString(ViterbiETree((__pyx_v_self->hg[0])).c_str()); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_1)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_1))->tp_name), 0))) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_tree = ((PyObject*)__pyx_t_1); + __pyx_t_1 = 0; - /* "_cdec.pyx":89 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":21 * def viterbi_tree(self): - * assert (self.hg != NULL) - * cdef str tree = hypergraph.ViterbiETree(self.hg[0]).c_str() # <<<<<<<<<<<<<< + * cdef str tree = hypergraph.ViterbiETree(self.hg[0]).c_str() + * return tree.decode('utf8') # <<<<<<<<<<<<<< + * + * def viterbi_source_tree(self): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_tree), __pyx_n_s__decode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_k_tuple_5), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_AddTraceback("_cdec.Hypergraph.viterbi_tree", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XDECREF(__pyx_v_tree); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_7viterbi_source_tree(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_7viterbi_source_tree(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("viterbi_source_tree (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_10Hypergraph_6viterbi_source_tree(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":23 * return tree.decode('utf8') * + * def viterbi_source_tree(self): # <<<<<<<<<<<<<< + * cdef str tree = hypergraph.ViterbiFTree(self.hg[0]).c_str() + * return tree.decode('utf8') */ - __pyx_t_1 = PyBytes_FromString(ViterbiETree((__pyx_v_self->hg[0])).c_str()); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 89; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + +static PyObject *__pyx_pf_5_cdec_10Hypergraph_6viterbi_source_tree(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self) { + PyObject *__pyx_v_tree = 0; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("viterbi_source_tree", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":24 + * + * def viterbi_source_tree(self): + * cdef str tree = hypergraph.ViterbiFTree(self.hg[0]).c_str() # <<<<<<<<<<<<<< + * return tree.decode('utf8') + * + */ + __pyx_t_1 = PyBytes_FromString(ViterbiFTree((__pyx_v_self->hg[0])).c_str()); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_1)); - if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_1)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_1))->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 89; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_1)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_1))->tp_name), 0))) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_v_tree = ((PyObject*)__pyx_t_1); __pyx_t_1 = 0; - /* "_cdec.pyx":90 - * assert (self.hg != NULL) - * cdef str tree = hypergraph.ViterbiETree(self.hg[0]).c_str() + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":25 + * def viterbi_source_tree(self): + * cdef str tree = hypergraph.ViterbiFTree(self.hg[0]).c_str() * return tree.decode('utf8') # <<<<<<<<<<<<<< * - * def kbest(self, size): + * def viterbi_features(self): */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_tree), __pyx_n_s__decode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = PyObject_GetAttr(((PyObject *)__pyx_v_tree), __pyx_n_s__decode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_1); - __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_k_tuple_4), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_k_tuple_6), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_2); __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; __pyx_r = __pyx_t_2; @@ -2244,7 +2892,7 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_4viterbi_tree(struct __pyx_obj_5_c __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_1); __Pyx_XDECREF(__pyx_t_2); - __Pyx_AddTraceback("_cdec.Hypergraph.viterbi_tree", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_AddTraceback("_cdec.Hypergraph.viterbi_source_tree", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; __Pyx_XDECREF(__pyx_v_tree); @@ -2252,36 +2900,111 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_4viterbi_tree(struct __pyx_obj_5_c __Pyx_RefNannyFinishContext(); return __pyx_r; } -static PyObject *__pyx_gb_5_cdec_10Hypergraph_8generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ /* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_10Hypergraph_7kbest(PyObject *__pyx_v_self, PyObject *__pyx_v_size); /*proto*/ -static PyObject *__pyx_pw_5_cdec_10Hypergraph_7kbest(PyObject *__pyx_v_self, PyObject *__pyx_v_size) { +static PyObject *__pyx_pw_5_cdec_10Hypergraph_9viterbi_features(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_9viterbi_features(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("kbest (wrapper)", 0); - __pyx_r = __pyx_pf_5_cdec_10Hypergraph_6kbest(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), ((PyObject *)__pyx_v_size)); + __Pyx_RefNannySetupContext("viterbi_features (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_10Hypergraph_8viterbi_features(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":92 +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":27 * return tree.decode('utf8') * + * def viterbi_features(self): # <<<<<<<<<<<<<< + * cdef SparseVector fmap = SparseVector() + * fmap.vector = new FastSparseVector[weight_t](hypergraph.ViterbiFeatures(self.hg[0])) + */ + +static PyObject *__pyx_pf_5_cdec_10Hypergraph_8viterbi_features(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self) { + struct __pyx_obj_5_cdec_SparseVector *__pyx_v_fmap = 0; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("viterbi_features", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":28 + * + * def viterbi_features(self): + * cdef SparseVector fmap = SparseVector() # <<<<<<<<<<<<<< + * fmap.vector = new FastSparseVector[weight_t](hypergraph.ViterbiFeatures(self.hg[0])) + * return fmap + */ + __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_SparseVector)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 28; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_v_fmap = ((struct __pyx_obj_5_cdec_SparseVector *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":29 + * def viterbi_features(self): + * cdef SparseVector fmap = SparseVector() + * fmap.vector = new FastSparseVector[weight_t](hypergraph.ViterbiFeatures(self.hg[0])) # <<<<<<<<<<<<<< + * return fmap + * + */ + __pyx_v_fmap->vector = new FastSparseVector<weight_t>(ViterbiFeatures((__pyx_v_self->hg[0]))); + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":30 + * cdef SparseVector fmap = SparseVector() + * fmap.vector = new FastSparseVector[weight_t](hypergraph.ViterbiFeatures(self.hg[0])) + * return fmap # <<<<<<<<<<<<<< + * + * def kbest(self, size): + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_fmap)); + __pyx_r = ((PyObject *)__pyx_v_fmap); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("_cdec.Hypergraph.viterbi_features", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XDECREF((PyObject *)__pyx_v_fmap); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} +static PyObject *__pyx_gb_5_cdec_10Hypergraph_12generator2(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_11kbest(PyObject *__pyx_v_self, PyObject *__pyx_v_size); /*proto*/ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_11kbest(PyObject *__pyx_v_self, PyObject *__pyx_v_size) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("kbest (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_10Hypergraph_10kbest(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), ((PyObject *)__pyx_v_size)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":32 + * return fmap + * * def kbest(self, size): # <<<<<<<<<<<<<< - * assert (self.hg != NULL) * cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal](self.hg[0], size) + * cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal].Derivation* derivation */ -static PyObject *__pyx_pf_5_cdec_10Hypergraph_6kbest(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_size) { - struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *__pyx_cur_scope; +static PyObject *__pyx_pf_5_cdec_10Hypergraph_10kbest(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_size) { + struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *__pyx_cur_scope; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; __Pyx_RefNannySetupContext("kbest", 0); - __pyx_cur_scope = (struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *)__pyx_ptype_5_cdec___pyx_scope_struct_1_kbest->tp_new(__pyx_ptype_5_cdec___pyx_scope_struct_1_kbest, __pyx_empty_tuple, NULL); + __pyx_cur_scope = (struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *)__pyx_ptype_5_cdec___pyx_scope_struct_2_kbest->tp_new(__pyx_ptype_5_cdec___pyx_scope_struct_2_kbest, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_cur_scope)) { __Pyx_RefNannyFinishContext(); return NULL; @@ -2294,7 +3017,7 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_6kbest(struct __pyx_obj_5_cdec_Hyp __Pyx_INCREF(__pyx_cur_scope->__pyx_v_size); __Pyx_GIVEREF(__pyx_cur_scope->__pyx_v_size); { - __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_10Hypergraph_8generator1, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_10Hypergraph_12generator2, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_DECREF(__pyx_cur_scope); __Pyx_RefNannyFinishContext(); return (PyObject *) gen; @@ -2312,9 +3035,9 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_6kbest(struct __pyx_obj_5_cdec_Hyp return __pyx_r; } -static PyObject *__pyx_gb_5_cdec_10Hypergraph_8generator1(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ +static PyObject *__pyx_gb_5_cdec_10Hypergraph_12generator2(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ { - struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *__pyx_cur_scope = ((struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *)__pyx_generator->closure); + struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *__pyx_cur_scope = ((struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *)__pyx_generator->closure); PyObject *__pyx_r = NULL; unsigned int __pyx_t_1; long __pyx_t_2; @@ -2331,58 +3054,44 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_8generator1(__pyx_GeneratorObject return NULL; } __pyx_L3_first_run:; - if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - /* "_cdec.pyx":93 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":33 * * def kbest(self, size): - * assert (self.hg != NULL) # <<<<<<<<<<<<<< - * cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal](self.hg[0], size) - * cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal].Derivation* derivation - */ - #ifndef CYTHON_WITHOUT_ASSERTIONS - if (unlikely(!(__pyx_cur_scope->__pyx_v_self->hg != NULL))) { - PyErr_SetNone(PyExc_AssertionError); - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 93; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - } - #endif - - /* "_cdec.pyx":94 - * def kbest(self, size): - * assert (self.hg != NULL) * cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal](self.hg[0], size) # <<<<<<<<<<<<<< * cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal].Derivation* derivation - * cdef str tree + * cdef str sentence */ - __pyx_t_1 = __Pyx_PyInt_AsUnsignedInt(__pyx_cur_scope->__pyx_v_size); if (unlikely((__pyx_t_1 == (unsigned int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 94; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = __Pyx_PyInt_AsUnsignedInt(__pyx_cur_scope->__pyx_v_size); if (unlikely((__pyx_t_1 == (unsigned int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_cur_scope->__pyx_v_derivations = new KBest::KBestDerivations<std::vector<WordID>,ESentenceTraversal>((__pyx_cur_scope->__pyx_v_self->hg[0]), __pyx_t_1); - /* "_cdec.pyx":98 - * cdef str tree + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":37 + * cdef str sentence * cdef unsigned k * for k in range(size): # <<<<<<<<<<<<<< * derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) * if not derivation: break */ - __pyx_t_2 = __Pyx_PyInt_AsLong(__pyx_cur_scope->__pyx_v_size); if (unlikely((__pyx_t_2 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 98; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = __Pyx_PyInt_AsLong(__pyx_cur_scope->__pyx_v_size); if (unlikely((__pyx_t_2 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1_error;} for (__pyx_t_1 = 0; __pyx_t_1 < __pyx_t_2; __pyx_t_1+=1) { __pyx_cur_scope->__pyx_v_k = __pyx_t_1; - /* "_cdec.pyx":99 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":38 * cdef unsigned k * for k in range(size): * derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) # <<<<<<<<<<<<<< * if not derivation: break - * tree = GetString(derivation._yield).c_str() + * sentence = GetString(derivation._yield).c_str() */ __pyx_cur_scope->__pyx_v_derivation = __pyx_cur_scope->__pyx_v_derivations->LazyKthBest((__pyx_cur_scope->__pyx_v_self->hg->nodes_.size() - 1), __pyx_cur_scope->__pyx_v_k); - /* "_cdec.pyx":100 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":39 * for k in range(size): * derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) * if not derivation: break # <<<<<<<<<<<<<< - * tree = GetString(derivation._yield).c_str() - * yield tree.decode('utf8') + * sentence = GetString(derivation._yield).c_str() + * yield sentence.decode('utf8') */ __pyx_t_3 = (!(__pyx_cur_scope->__pyx_v_derivation != 0)); if (__pyx_t_3) { @@ -2391,32 +3100,32 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_8generator1(__pyx_GeneratorObject } __pyx_L6:; - /* "_cdec.pyx":101 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":40 * derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) * if not derivation: break - * tree = GetString(derivation._yield).c_str() # <<<<<<<<<<<<<< - * yield tree.decode('utf8') + * sentence = GetString(derivation._yield).c_str() # <<<<<<<<<<<<<< + * yield sentence.decode('utf8') * del derivations */ - __pyx_t_4 = PyBytes_FromString(TD::GetString(__pyx_cur_scope->__pyx_v_derivation->yield).c_str()); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 101; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_4 = PyBytes_FromString(TD::GetString(__pyx_cur_scope->__pyx_v_derivation->yield).c_str()); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_4)); - if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_4)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_4))->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 101; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_XGOTREF(((PyObject *)__pyx_cur_scope->__pyx_v_tree)); - __Pyx_XDECREF(((PyObject *)__pyx_cur_scope->__pyx_v_tree)); + if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_4)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_4))->tp_name), 0))) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_XGOTREF(((PyObject *)__pyx_cur_scope->__pyx_v_sentence)); + __Pyx_XDECREF(((PyObject *)__pyx_cur_scope->__pyx_v_sentence)); __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); - __pyx_cur_scope->__pyx_v_tree = ((PyObject*)__pyx_t_4); + __pyx_cur_scope->__pyx_v_sentence = ((PyObject*)__pyx_t_4); __pyx_t_4 = 0; - /* "_cdec.pyx":102 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":41 * if not derivation: break - * tree = GetString(derivation._yield).c_str() - * yield tree.decode('utf8') # <<<<<<<<<<<<<< + * sentence = GetString(derivation._yield).c_str() + * yield sentence.decode('utf8') # <<<<<<<<<<<<<< * del derivations * */ - __pyx_t_4 = PyObject_GetAttr(((PyObject *)__pyx_cur_scope->__pyx_v_tree), __pyx_n_s__decode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 102; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_4 = PyObject_GetAttr(((PyObject *)__pyx_cur_scope->__pyx_v_sentence), __pyx_n_s__decode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 41; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_4); - __pyx_t_5 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_k_tuple_5), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 102; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_5 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_k_tuple_7), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 41; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_5); __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; __pyx_r = __pyx_t_5; @@ -2431,13 +3140,13 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_8generator1(__pyx_GeneratorObject __pyx_L7_resume_from_yield:; __pyx_t_1 = __pyx_cur_scope->__pyx_t_0; __pyx_t_2 = __pyx_cur_scope->__pyx_t_1; - if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 102; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 41; __pyx_clineno = __LINE__; goto __pyx_L1_error;} } __pyx_L5_break:; - /* "_cdec.pyx":103 - * tree = GetString(derivation._yield).c_str() - * yield tree.decode('utf8') + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":42 + * sentence = GetString(derivation._yield).c_str() + * yield sentence.decode('utf8') * del derivations # <<<<<<<<<<<<<< * * def kbest_tree(self, size): @@ -2455,36 +3164,36 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_8generator1(__pyx_GeneratorObject __Pyx_RefNannyFinishContext(); return NULL; } -static PyObject *__pyx_gb_5_cdec_10Hypergraph_11generator2(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ +static PyObject *__pyx_gb_5_cdec_10Hypergraph_15generator3(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ /* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_10Hypergraph_10kbest_tree(PyObject *__pyx_v_self, PyObject *__pyx_v_size); /*proto*/ -static PyObject *__pyx_pw_5_cdec_10Hypergraph_10kbest_tree(PyObject *__pyx_v_self, PyObject *__pyx_v_size) { +static PyObject *__pyx_pw_5_cdec_10Hypergraph_14kbest_tree(PyObject *__pyx_v_self, PyObject *__pyx_v_size); /*proto*/ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_14kbest_tree(PyObject *__pyx_v_self, PyObject *__pyx_v_size) { PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("kbest_tree (wrapper)", 0); - __pyx_r = __pyx_pf_5_cdec_10Hypergraph_9kbest_tree(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), ((PyObject *)__pyx_v_size)); + __pyx_r = __pyx_pf_5_cdec_10Hypergraph_13kbest_tree(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), ((PyObject *)__pyx_v_size)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":105 +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":44 * del derivations * * def kbest_tree(self, size): # <<<<<<<<<<<<<< - * assert (self.hg != NULL) * cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ETreeTraversal](self.hg[0], size) + * cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal].Derivation* derivation */ -static PyObject *__pyx_pf_5_cdec_10Hypergraph_9kbest_tree(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_size) { - struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *__pyx_cur_scope; +static PyObject *__pyx_pf_5_cdec_10Hypergraph_13kbest_tree(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_size) { + struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *__pyx_cur_scope; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; __Pyx_RefNannySetupContext("kbest_tree", 0); - __pyx_cur_scope = (struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *)__pyx_ptype_5_cdec___pyx_scope_struct_2_kbest_tree->tp_new(__pyx_ptype_5_cdec___pyx_scope_struct_2_kbest_tree, __pyx_empty_tuple, NULL); + __pyx_cur_scope = (struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *)__pyx_ptype_5_cdec___pyx_scope_struct_3_kbest_tree->tp_new(__pyx_ptype_5_cdec___pyx_scope_struct_3_kbest_tree, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_cur_scope)) { __Pyx_RefNannyFinishContext(); return NULL; @@ -2497,7 +3206,7 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_9kbest_tree(struct __pyx_obj_5_cde __Pyx_INCREF(__pyx_cur_scope->__pyx_v_size); __Pyx_GIVEREF(__pyx_cur_scope->__pyx_v_size); { - __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_10Hypergraph_11generator2, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_10Hypergraph_15generator3, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 44; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_DECREF(__pyx_cur_scope); __Pyx_RefNannyFinishContext(); return (PyObject *) gen; @@ -2515,9 +3224,9 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_9kbest_tree(struct __pyx_obj_5_cde return __pyx_r; } -static PyObject *__pyx_gb_5_cdec_10Hypergraph_11generator2(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ +static PyObject *__pyx_gb_5_cdec_10Hypergraph_15generator3(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ { - struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *__pyx_cur_scope = ((struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *)__pyx_generator->closure); + struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *__pyx_cur_scope = ((struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *)__pyx_generator->closure); PyObject *__pyx_r = NULL; unsigned int __pyx_t_1; long __pyx_t_2; @@ -2534,58 +3243,44 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_11generator2(__pyx_GeneratorObject return NULL; } __pyx_L3_first_run:; - if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 44; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - /* "_cdec.pyx":106 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":45 * * def kbest_tree(self, size): - * assert (self.hg != NULL) # <<<<<<<<<<<<<< - * cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ETreeTraversal](self.hg[0], size) - * cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal].Derivation* derivation - */ - #ifndef CYTHON_WITHOUT_ASSERTIONS - if (unlikely(!(__pyx_cur_scope->__pyx_v_self->hg != NULL))) { - PyErr_SetNone(PyExc_AssertionError); - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 106; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - } - #endif - - /* "_cdec.pyx":107 - * def kbest_tree(self, size): - * assert (self.hg != NULL) * cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ETreeTraversal](self.hg[0], size) # <<<<<<<<<<<<<< * cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal].Derivation* derivation - * cdef str sentence + * cdef str tree */ - __pyx_t_1 = __Pyx_PyInt_AsUnsignedInt(__pyx_cur_scope->__pyx_v_size); if (unlikely((__pyx_t_1 == (unsigned int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 107; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = __Pyx_PyInt_AsUnsignedInt(__pyx_cur_scope->__pyx_v_size); if (unlikely((__pyx_t_1 == (unsigned int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_cur_scope->__pyx_v_derivations = new KBest::KBestDerivations<std::vector<WordID>,ETreeTraversal>((__pyx_cur_scope->__pyx_v_self->hg[0]), __pyx_t_1); - /* "_cdec.pyx":111 - * cdef str sentence + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":49 + * cdef str tree * cdef unsigned k * for k in range(size): # <<<<<<<<<<<<<< * derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) * if not derivation: break */ - __pyx_t_2 = __Pyx_PyInt_AsLong(__pyx_cur_scope->__pyx_v_size); if (unlikely((__pyx_t_2 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 111; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_2 = __Pyx_PyInt_AsLong(__pyx_cur_scope->__pyx_v_size); if (unlikely((__pyx_t_2 == (long)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 49; __pyx_clineno = __LINE__; goto __pyx_L1_error;} for (__pyx_t_1 = 0; __pyx_t_1 < __pyx_t_2; __pyx_t_1+=1) { __pyx_cur_scope->__pyx_v_k = __pyx_t_1; - /* "_cdec.pyx":112 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":50 * cdef unsigned k * for k in range(size): * derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) # <<<<<<<<<<<<<< * if not derivation: break - * sentence = GetString(derivation._yield).c_str() + * tree = GetString(derivation._yield).c_str() */ __pyx_cur_scope->__pyx_v_derivation = __pyx_cur_scope->__pyx_v_derivations->LazyKthBest((__pyx_cur_scope->__pyx_v_self->hg->nodes_.size() - 1), __pyx_cur_scope->__pyx_v_k); - /* "_cdec.pyx":113 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":51 * for k in range(size): * derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) * if not derivation: break # <<<<<<<<<<<<<< - * sentence = GetString(derivation._yield).c_str() - * yield sentence.decode('utf8') + * tree = GetString(derivation._yield).c_str() + * yield tree.decode('utf8') */ __pyx_t_3 = (!(__pyx_cur_scope->__pyx_v_derivation != 0)); if (__pyx_t_3) { @@ -2594,32 +3289,32 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_11generator2(__pyx_GeneratorObject } __pyx_L6:; - /* "_cdec.pyx":114 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":52 * derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) * if not derivation: break - * sentence = GetString(derivation._yield).c_str() # <<<<<<<<<<<<<< - * yield sentence.decode('utf8') + * tree = GetString(derivation._yield).c_str() # <<<<<<<<<<<<<< + * yield tree.decode('utf8') * del derivations */ - __pyx_t_4 = PyBytes_FromString(TD::GetString(__pyx_cur_scope->__pyx_v_derivation->yield).c_str()); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 114; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_4 = PyBytes_FromString(TD::GetString(__pyx_cur_scope->__pyx_v_derivation->yield).c_str()); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_4)); - if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_4)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_4))->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 114; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_XGOTREF(((PyObject *)__pyx_cur_scope->__pyx_v_sentence)); - __Pyx_XDECREF(((PyObject *)__pyx_cur_scope->__pyx_v_sentence)); + if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_4)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_4))->tp_name), 0))) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_XGOTREF(((PyObject *)__pyx_cur_scope->__pyx_v_tree)); + __Pyx_XDECREF(((PyObject *)__pyx_cur_scope->__pyx_v_tree)); __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); - __pyx_cur_scope->__pyx_v_sentence = ((PyObject*)__pyx_t_4); + __pyx_cur_scope->__pyx_v_tree = ((PyObject*)__pyx_t_4); __pyx_t_4 = 0; - /* "_cdec.pyx":115 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":53 * if not derivation: break - * sentence = GetString(derivation._yield).c_str() - * yield sentence.decode('utf8') # <<<<<<<<<<<<<< + * tree = GetString(derivation._yield).c_str() + * yield tree.decode('utf8') # <<<<<<<<<<<<<< * del derivations * */ - __pyx_t_4 = PyObject_GetAttr(((PyObject *)__pyx_cur_scope->__pyx_v_sentence), __pyx_n_s__decode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_4 = PyObject_GetAttr(((PyObject *)__pyx_cur_scope->__pyx_v_tree), __pyx_n_s__decode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_4); - __pyx_t_5 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_k_tuple_6), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_5 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_k_tuple_8), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_5); __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; __pyx_r = __pyx_t_5; @@ -2634,16 +3329,16 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_11generator2(__pyx_GeneratorObject __pyx_L7_resume_from_yield:; __pyx_t_1 = __pyx_cur_scope->__pyx_t_0; __pyx_t_2 = __pyx_cur_scope->__pyx_t_1; - if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} } __pyx_L5_break:; - /* "_cdec.pyx":116 - * sentence = GetString(derivation._yield).c_str() - * yield sentence.decode('utf8') + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":54 + * tree = GetString(derivation._yield).c_str() + * yield tree.decode('utf8') * del derivations # <<<<<<<<<<<<<< * - * def intersect(self, Lattice lat): + * def sample(self, unsigned n): */ delete __pyx_cur_scope->__pyx_v_derivations; PyErr_SetNone(PyExc_StopIteration); @@ -2658,83 +3353,17 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_11generator2(__pyx_GeneratorObject __Pyx_RefNannyFinishContext(); return NULL; } +static PyObject *__pyx_gb_5_cdec_10Hypergraph_18generator4(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ /* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_10Hypergraph_13intersect(PyObject *__pyx_v_self, PyObject *__pyx_v_lat); /*proto*/ -static PyObject *__pyx_pw_5_cdec_10Hypergraph_13intersect(PyObject *__pyx_v_self, PyObject *__pyx_v_lat) { - PyObject *__pyx_r = 0; - __Pyx_RefNannyDeclarations - __Pyx_RefNannySetupContext("intersect (wrapper)", 0); - if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_lat), __pyx_ptype_5_cdec_Lattice, 1, "lat", 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 118; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_r = __pyx_pf_5_cdec_10Hypergraph_12intersect(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), ((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_lat)); - goto __pyx_L0; - __pyx_L1_error:; - __pyx_r = NULL; - __pyx_L0:; - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - -/* "_cdec.pyx":118 - * del derivations - * - * def intersect(self, Lattice lat): # <<<<<<<<<<<<<< - * assert (self.hg != NULL) - * hypergraph.Intersect(lat.lattice[0], self.hg) - */ - -static PyObject *__pyx_pf_5_cdec_10Hypergraph_12intersect(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, struct __pyx_obj_5_cdec_Lattice *__pyx_v_lat) { - PyObject *__pyx_r = NULL; - __Pyx_RefNannyDeclarations - int __pyx_lineno = 0; - const char *__pyx_filename = NULL; - int __pyx_clineno = 0; - __Pyx_RefNannySetupContext("intersect", 0); - - /* "_cdec.pyx":119 - * - * def intersect(self, Lattice lat): - * assert (self.hg != NULL) # <<<<<<<<<<<<<< - * hypergraph.Intersect(lat.lattice[0], self.hg) - * - */ - #ifndef CYTHON_WITHOUT_ASSERTIONS - if (unlikely(!(__pyx_v_self->hg != NULL))) { - PyErr_SetNone(PyExc_AssertionError); - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 119; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - } - #endif - - /* "_cdec.pyx":120 - * def intersect(self, Lattice lat): - * assert (self.hg != NULL) - * hypergraph.Intersect(lat.lattice[0], self.hg) # <<<<<<<<<<<<<< - * - * def sample(self, unsigned n): - */ - HG::Intersect((__pyx_v_lat->lattice[0]), __pyx_v_self->hg); - - __pyx_r = Py_None; __Pyx_INCREF(Py_None); - goto __pyx_L0; - __pyx_L1_error:; - __Pyx_AddTraceback("_cdec.Hypergraph.intersect", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = NULL; - __pyx_L0:; - __Pyx_XGIVEREF(__pyx_r); - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} -static PyObject *__pyx_gb_5_cdec_10Hypergraph_16generator3(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ - -/* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_10Hypergraph_15sample(PyObject *__pyx_v_self, PyObject *__pyx_arg_n); /*proto*/ -static PyObject *__pyx_pw_5_cdec_10Hypergraph_15sample(PyObject *__pyx_v_self, PyObject *__pyx_arg_n) { +static PyObject *__pyx_pw_5_cdec_10Hypergraph_17sample(PyObject *__pyx_v_self, PyObject *__pyx_arg_n); /*proto*/ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_17sample(PyObject *__pyx_v_self, PyObject *__pyx_arg_n) { unsigned int __pyx_v_n; PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("sample (wrapper)", 0); assert(__pyx_arg_n); { - __pyx_v_n = __Pyx_PyInt_AsUnsignedInt(__pyx_arg_n); if (unlikely((__pyx_v_n == (unsigned int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_v_n = __Pyx_PyInt_AsUnsignedInt(__pyx_arg_n); if (unlikely((__pyx_v_n == (unsigned int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L3_error;} } goto __pyx_L4_argument_unpacking_done; __pyx_L3_error:; @@ -2742,28 +3371,28 @@ static PyObject *__pyx_pw_5_cdec_10Hypergraph_15sample(PyObject *__pyx_v_self, P __Pyx_RefNannyFinishContext(); return NULL; __pyx_L4_argument_unpacking_done:; - __pyx_r = __pyx_pf_5_cdec_10Hypergraph_14sample(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), ((unsigned int)__pyx_v_n)); + __pyx_r = __pyx_pf_5_cdec_10Hypergraph_16sample(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), ((unsigned int)__pyx_v_n)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":122 - * hypergraph.Intersect(lat.lattice[0], self.hg) +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":56 + * del derivations * * def sample(self, unsigned n): # <<<<<<<<<<<<<< - * assert (self.hg != NULL) * cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() + * if self.rng == NULL: */ -static PyObject *__pyx_pf_5_cdec_10Hypergraph_14sample(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, unsigned int __pyx_v_n) { - struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *__pyx_cur_scope; +static PyObject *__pyx_pf_5_cdec_10Hypergraph_16sample(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, unsigned int __pyx_v_n) { + struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *__pyx_cur_scope; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; __Pyx_RefNannySetupContext("sample", 0); - __pyx_cur_scope = (struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *)__pyx_ptype_5_cdec___pyx_scope_struct_3_sample->tp_new(__pyx_ptype_5_cdec___pyx_scope_struct_3_sample, __pyx_empty_tuple, NULL); + __pyx_cur_scope = (struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *)__pyx_ptype_5_cdec___pyx_scope_struct_4_sample->tp_new(__pyx_ptype_5_cdec___pyx_scope_struct_4_sample, __pyx_empty_tuple, NULL); if (unlikely(!__pyx_cur_scope)) { __Pyx_RefNannyFinishContext(); return NULL; @@ -2774,7 +3403,7 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_14sample(struct __pyx_obj_5_cdec_H __Pyx_GIVEREF((PyObject *)__pyx_cur_scope->__pyx_v_self); __pyx_cur_scope->__pyx_v_n = __pyx_v_n; { - __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_10Hypergraph_16generator3, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_10Hypergraph_18generator4, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_DECREF(__pyx_cur_scope); __Pyx_RefNannyFinishContext(); return (PyObject *) gen; @@ -2792,9 +3421,9 @@ static PyObject *__pyx_pf_5_cdec_10Hypergraph_14sample(struct __pyx_obj_5_cdec_H return __pyx_r; } -static PyObject *__pyx_gb_5_cdec_10Hypergraph_16generator3(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ +static PyObject *__pyx_gb_5_cdec_10Hypergraph_18generator4(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ { - struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *__pyx_cur_scope = ((struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *)__pyx_generator->closure); + struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *__pyx_cur_scope = ((struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *)__pyx_generator->closure); PyObject *__pyx_r = NULL; int __pyx_t_1; size_t __pyx_t_2; @@ -2811,33 +3440,19 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_16generator3(__pyx_GeneratorObject return NULL; } __pyx_L3_first_run:; - if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - /* "_cdec.pyx":123 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":57 * * def sample(self, unsigned n): - * assert (self.hg != NULL) # <<<<<<<<<<<<<< - * cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() - * if self.rng == NULL: - */ - #ifndef CYTHON_WITHOUT_ASSERTIONS - if (unlikely(!(__pyx_cur_scope->__pyx_v_self->hg != NULL))) { - PyErr_SetNone(PyExc_AssertionError); - {__pyx_filename = __pyx_f[0]; __pyx_lineno = 123; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - } - #endif - - /* "_cdec.pyx":124 - * def sample(self, unsigned n): - * assert (self.hg != NULL) * cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() # <<<<<<<<<<<<<< * if self.rng == NULL: * self.rng = new MT19937() */ __pyx_cur_scope->__pyx_v_hypos = new std::vector<HypergraphSampler::Hypothesis>(); - /* "_cdec.pyx":125 - * assert (self.hg != NULL) + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":58 + * def sample(self, unsigned n): * cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() * if self.rng == NULL: # <<<<<<<<<<<<<< * self.rng = new MT19937() @@ -2846,7 +3461,7 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_16generator3(__pyx_GeneratorObject __pyx_t_1 = (__pyx_cur_scope->__pyx_v_self->rng == NULL); if (__pyx_t_1) { - /* "_cdec.pyx":126 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":59 * cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() * if self.rng == NULL: * self.rng = new MT19937() # <<<<<<<<<<<<<< @@ -2858,7 +3473,7 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_16generator3(__pyx_GeneratorObject } __pyx_L4:; - /* "_cdec.pyx":127 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":60 * if self.rng == NULL: * self.rng = new MT19937() * hypergraph.sample_hypotheses(self.hg[0], n, self.rng, hypos) # <<<<<<<<<<<<<< @@ -2867,7 +3482,7 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_16generator3(__pyx_GeneratorObject */ HypergraphSampler::sample_hypotheses((__pyx_cur_scope->__pyx_v_self->hg[0]), __pyx_cur_scope->__pyx_v_n, __pyx_cur_scope->__pyx_v_self->rng, __pyx_cur_scope->__pyx_v_hypos); - /* "_cdec.pyx":130 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":63 * cdef str sentence * cdef unsigned k * for k in range(hypos.size()): # <<<<<<<<<<<<<< @@ -2878,32 +3493,32 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_16generator3(__pyx_GeneratorObject for (__pyx_t_3 = 0; __pyx_t_3 < __pyx_t_2; __pyx_t_3+=1) { __pyx_cur_scope->__pyx_v_k = __pyx_t_3; - /* "_cdec.pyx":131 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":64 * cdef unsigned k * for k in range(hypos.size()): * sentence = GetString(hypos[0][k].words).c_str() # <<<<<<<<<<<<<< * yield sentence.decode('utf8') * del hypos */ - __pyx_t_4 = PyBytes_FromString(TD::GetString(((__pyx_cur_scope->__pyx_v_hypos[0])[__pyx_cur_scope->__pyx_v_k]).words).c_str()); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 131; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_4 = PyBytes_FromString(TD::GetString(((__pyx_cur_scope->__pyx_v_hypos[0])[__pyx_cur_scope->__pyx_v_k]).words).c_str()); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_4)); - if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_4)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_4))->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 131; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_4)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_4))->tp_name), 0))) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 64; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_XGOTREF(((PyObject *)__pyx_cur_scope->__pyx_v_sentence)); __Pyx_XDECREF(((PyObject *)__pyx_cur_scope->__pyx_v_sentence)); __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); __pyx_cur_scope->__pyx_v_sentence = ((PyObject*)__pyx_t_4); __pyx_t_4 = 0; - /* "_cdec.pyx":132 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":65 * for k in range(hypos.size()): * sentence = GetString(hypos[0][k].words).c_str() * yield sentence.decode('utf8') # <<<<<<<<<<<<<< * del hypos * */ - __pyx_t_4 = PyObject_GetAttr(((PyObject *)__pyx_cur_scope->__pyx_v_sentence), __pyx_n_s__decode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 132; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_4 = PyObject_GetAttr(((PyObject *)__pyx_cur_scope->__pyx_v_sentence), __pyx_n_s__decode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_4); - __pyx_t_5 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_k_tuple_7), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 132; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_5 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_k_tuple_9), NULL); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(__pyx_t_5); __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; __pyx_r = __pyx_t_5; @@ -2918,15 +3533,15 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_16generator3(__pyx_GeneratorObject __pyx_L7_resume_from_yield:; __pyx_t_2 = __pyx_cur_scope->__pyx_t_0; __pyx_t_3 = __pyx_cur_scope->__pyx_t_1; - if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 132; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;} } - /* "_cdec.pyx":133 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":66 * sentence = GetString(hypos[0][k].words).c_str() * yield sentence.decode('utf8') * del hypos # <<<<<<<<<<<<<< * - * # TODO: get feature expectations, get partition function ("inside" score) + * # TODO richer k-best/sample output (feature vectors, trees?) */ delete __pyx_cur_scope->__pyx_v_hypos; PyErr_SetNone(PyExc_StopIteration); @@ -2943,10 +3558,436 @@ static PyObject *__pyx_gb_5_cdec_10Hypergraph_16generator3(__pyx_GeneratorObject } /* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_20intersect(PyObject *__pyx_v_self, PyObject *__pyx_v_lat); /*proto*/ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_20intersect(PyObject *__pyx_v_self, PyObject *__pyx_v_lat) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("intersect (wrapper)", 0); + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_lat), __pyx_ptype_5_cdec_Lattice, 1, "lat", 0))) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 70; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_r = __pyx_pf_5_cdec_10Hypergraph_19intersect(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), ((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_lat)); + goto __pyx_L0; + __pyx_L1_error:; + __pyx_r = NULL; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":70 + * # TODO richer k-best/sample output (feature vectors, trees?) + * + * def intersect(self, Lattice lat): # <<<<<<<<<<<<<< + * return hypergraph.Intersect(lat.lattice[0], self.hg) + * + */ + +static PyObject *__pyx_pf_5_cdec_10Hypergraph_19intersect(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, struct __pyx_obj_5_cdec_Lattice *__pyx_v_lat) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("intersect", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":71 + * + * def intersect(self, Lattice lat): + * return hypergraph.Intersect(lat.lattice[0], self.hg) # <<<<<<<<<<<<<< + * + * def prune(self, beam_alpha=0, density=0, **kwargs): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_PyBool_FromLong(HG::Intersect((__pyx_v_lat->lattice[0]), __pyx_v_self->hg)); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_r = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_AddTraceback("_cdec.Hypergraph.intersect", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_22prune(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_22prune(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_beam_alpha = 0; + PyObject *__pyx_v_density = 0; + PyObject *__pyx_v_kwargs = 0; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__beam_alpha,&__pyx_n_s__density,0}; + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("prune (wrapper)", 0); + __pyx_v_kwargs = PyDict_New(); if (unlikely(!__pyx_v_kwargs)) return NULL; + __Pyx_GOTREF(__pyx_v_kwargs); + { + PyObject* values[2] = {0,0}; + values[0] = ((PyObject *)__pyx_int_0); + values[1] = ((PyObject *)__pyx_int_0); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args; + const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); + switch (pos_args) { + case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + kw_args = PyDict_Size(__pyx_kwds); + switch (pos_args) { + case 0: + if (kw_args > 0) { + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__beam_alpha); + if (value) { values[0] = value; kw_args--; } + } + case 1: + if (kw_args > 0) { + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__density); + if (value) { values[1] = value; kw_args--; } + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, __pyx_v_kwargs, values, pos_args, "prune") < 0)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } else { + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + } + __pyx_v_beam_alpha = values[0]; + __pyx_v_density = values[1]; + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("prune", 0, 0, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[2]; __pyx_lineno = 73; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_DECREF(__pyx_v_kwargs); __pyx_v_kwargs = 0; + __Pyx_AddTraceback("_cdec.Hypergraph.prune", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_r = __pyx_pf_5_cdec_10Hypergraph_21prune(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), __pyx_v_beam_alpha, __pyx_v_density, __pyx_v_kwargs); + __Pyx_XDECREF(__pyx_v_kwargs); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":73 + * return hypergraph.Intersect(lat.lattice[0], self.hg) + * + * def prune(self, beam_alpha=0, density=0, **kwargs): # <<<<<<<<<<<<<< + * cdef hypergraph.EdgeMask* preserve_mask = NULL + * if 'csplit_preserve_full_word' in kwargs: + */ + +static PyObject *__pyx_pf_5_cdec_10Hypergraph_21prune(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_beam_alpha, PyObject *__pyx_v_density, PyObject *__pyx_v_kwargs) { + std::vector<bool> *__pyx_v_preserve_mask; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + int __pyx_t_1; + double __pyx_t_2; + double __pyx_t_3; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("prune", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":74 + * + * def prune(self, beam_alpha=0, density=0, **kwargs): + * cdef hypergraph.EdgeMask* preserve_mask = NULL # <<<<<<<<<<<<<< + * if 'csplit_preserve_full_word' in kwargs: + * preserve_mask = new hypergraph.EdgeMask(self.hg.edges_.size()) + */ + __pyx_v_preserve_mask = NULL; + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":75 + * def prune(self, beam_alpha=0, density=0, **kwargs): + * cdef hypergraph.EdgeMask* preserve_mask = NULL + * if 'csplit_preserve_full_word' in kwargs: # <<<<<<<<<<<<<< + * preserve_mask = new hypergraph.EdgeMask(self.hg.edges_.size()) + * preserve_mask[0][hypergraph.GetFullWordEdgeIndex(self.hg[0])] = True + */ + __pyx_t_1 = ((PyDict_Contains(((PyObject *)__pyx_v_kwargs), ((PyObject *)__pyx_n_s_10)))); if (unlikely(__pyx_t_1 < 0)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 75; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_1) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":76 + * cdef hypergraph.EdgeMask* preserve_mask = NULL + * if 'csplit_preserve_full_word' in kwargs: + * preserve_mask = new hypergraph.EdgeMask(self.hg.edges_.size()) # <<<<<<<<<<<<<< + * preserve_mask[0][hypergraph.GetFullWordEdgeIndex(self.hg[0])] = True + * self.hg.PruneInsideOutside(beam_alpha, density, preserve_mask, False, 1, False) + */ + __pyx_v_preserve_mask = new std::vector<bool>(__pyx_v_self->hg->edges_.size()); + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":77 + * if 'csplit_preserve_full_word' in kwargs: + * preserve_mask = new hypergraph.EdgeMask(self.hg.edges_.size()) + * preserve_mask[0][hypergraph.GetFullWordEdgeIndex(self.hg[0])] = True # <<<<<<<<<<<<<< + * self.hg.PruneInsideOutside(beam_alpha, density, preserve_mask, False, 1, False) + * + */ + ((__pyx_v_preserve_mask[0])[CompoundSplit::GetFullWordEdgeIndex((__pyx_v_self->hg[0]))]) = 1; + goto __pyx_L3; + } + __pyx_L3:; + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":78 + * preserve_mask = new hypergraph.EdgeMask(self.hg.edges_.size()) + * preserve_mask[0][hypergraph.GetFullWordEdgeIndex(self.hg[0])] = True + * self.hg.PruneInsideOutside(beam_alpha, density, preserve_mask, False, 1, False) # <<<<<<<<<<<<<< + * + * def lattice(self): # TODO direct hg -> lattice conversion in cdec + */ + __pyx_t_2 = __pyx_PyFloat_AsDouble(__pyx_v_beam_alpha); if (unlikely((__pyx_t_2 == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_3 = __pyx_PyFloat_AsDouble(__pyx_v_density); if (unlikely((__pyx_t_3 == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 78; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_self->hg->PruneInsideOutside(__pyx_t_2, __pyx_t_3, __pyx_v_preserve_mask, 0, 1.0, 0); + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("_cdec.Hypergraph.prune", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_24lattice(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused); /*proto*/ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_24lattice(PyObject *__pyx_v_self, CYTHON_UNUSED PyObject *unused) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("lattice (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_10Hypergraph_23lattice(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":80 + * self.hg.PruneInsideOutside(beam_alpha, density, preserve_mask, False, 1, False) + * + * def lattice(self): # TODO direct hg -> lattice conversion in cdec # <<<<<<<<<<<<<< + * cdef str plf = hypergraph.AsPLF(self.hg[0], True).c_str() + * return Lattice(eval(plf)) + */ + +static PyObject *__pyx_pf_5_cdec_10Hypergraph_23lattice(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self) { + PyObject *__pyx_v_plf = 0; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("lattice", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":81 + * + * def lattice(self): # TODO direct hg -> lattice conversion in cdec + * cdef str plf = hypergraph.AsPLF(self.hg[0], True).c_str() # <<<<<<<<<<<<<< + * return Lattice(eval(plf)) + * + */ + __pyx_t_1 = PyBytes_FromString(HypergraphIO::AsPLF((__pyx_v_self->hg[0]), 1).c_str()); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_1)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_1))->tp_name), 0))) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 81; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_plf = ((PyObject*)__pyx_t_1); + __pyx_t_1 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":82 + * def lattice(self): # TODO direct hg -> lattice conversion in cdec + * cdef str plf = hypergraph.AsPLF(self.hg[0], True).c_str() + * return Lattice(eval(plf)) # <<<<<<<<<<<<<< + * + * def reweight(self, weights): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_1 = __Pyx_Globals(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = PyDict_New(); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_2)); + if (((PyObject *)__pyx_v_plf)) { + if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__plf), ((PyObject *)__pyx_v_plf)) < 0) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + if (((PyObject *)__pyx_v_self)) { + if (PyDict_SetItem(__pyx_t_2, ((PyObject *)__pyx_n_s__self), ((PyObject *)__pyx_v_self)) < 0) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_3 = PyTuple_New(3); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_INCREF(((PyObject *)__pyx_v_plf)); + PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_v_plf)); + __Pyx_GIVEREF(((PyObject *)__pyx_v_plf)); + PyTuple_SET_ITEM(__pyx_t_3, 1, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + PyTuple_SET_ITEM(__pyx_t_3, 2, ((PyObject *)__pyx_t_2)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_2)); + __pyx_t_1 = 0; + __pyx_t_2 = 0; + __pyx_t_2 = PyObject_Call(__pyx_builtin_eval, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_3, 0, __pyx_t_2); + __Pyx_GIVEREF(__pyx_t_2); + __pyx_t_2 = 0; + __pyx_t_2 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_Lattice)), ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __pyx_r = __pyx_t_2; + __pyx_t_2 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_XDECREF(__pyx_t_3); + __Pyx_AddTraceback("_cdec.Hypergraph.lattice", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XDECREF(__pyx_v_plf); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_26reweight(PyObject *__pyx_v_self, PyObject *__pyx_v_weights); /*proto*/ +static PyObject *__pyx_pw_5_cdec_10Hypergraph_26reweight(PyObject *__pyx_v_self, PyObject *__pyx_v_weights) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("reweight (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_10Hypergraph_25reweight(((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_v_self), ((PyObject *)__pyx_v_weights)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":84 + * return Lattice(eval(plf)) + * + * def reweight(self, weights): # <<<<<<<<<<<<<< + * if isinstance(weights, SparseVector): + * self.hg.Reweight((<SparseVector> weights).vector[0]) + */ + +static PyObject *__pyx_pf_5_cdec_10Hypergraph_25reweight(struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_self, PyObject *__pyx_v_weights) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("reweight", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":85 + * + * def reweight(self, weights): + * if isinstance(weights, SparseVector): # <<<<<<<<<<<<<< + * self.hg.Reweight((<SparseVector> weights).vector[0]) + * elif isinstance(weights, DenseVector): + */ + __pyx_t_1 = ((PyObject *)((PyObject*)__pyx_ptype_5_cdec_SparseVector)); + __Pyx_INCREF(__pyx_t_1); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_weights, __pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":86 + * def reweight(self, weights): + * if isinstance(weights, SparseVector): + * self.hg.Reweight((<SparseVector> weights).vector[0]) # <<<<<<<<<<<<<< + * elif isinstance(weights, DenseVector): + * self.hg.Reweight((<DenseVector> weights).vector[0]) + */ + __pyx_v_self->hg->Reweight((((struct __pyx_obj_5_cdec_SparseVector *)__pyx_v_weights)->vector[0])); + goto __pyx_L3; + } + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":87 + * if isinstance(weights, SparseVector): + * self.hg.Reweight((<SparseVector> weights).vector[0]) + * elif isinstance(weights, DenseVector): # <<<<<<<<<<<<<< + * self.hg.Reweight((<DenseVector> weights).vector[0]) + * else: + */ + __pyx_t_1 = ((PyObject *)((PyObject*)__pyx_ptype_5_cdec_DenseVector)); + __Pyx_INCREF(__pyx_t_1); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_weights, __pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":88 + * self.hg.Reweight((<SparseVector> weights).vector[0]) + * elif isinstance(weights, DenseVector): + * self.hg.Reweight((<DenseVector> weights).vector[0]) # <<<<<<<<<<<<<< + * else: + * raise ValueError('cannot reweight hypergraph with %s' % type(weights)) + */ + __pyx_v_self->hg->Reweight((((struct __pyx_obj_5_cdec_DenseVector *)__pyx_v_weights)->vector[0])); + goto __pyx_L3; + } + /*else*/ { + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":90 + * self.hg.Reweight((<DenseVector> weights).vector[0]) + * else: + * raise ValueError('cannot reweight hypergraph with %s' % type(weights)) # <<<<<<<<<<<<<< + * + * # TODO get feature expectations, get partition function ("inside" score) + */ + __pyx_t_1 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_11), ((PyObject *)Py_TYPE(__pyx_v_weights))); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_1)); + __pyx_t_3 = PyTuple_New(1); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + PyTuple_SET_ITEM(__pyx_t_3, 0, ((PyObject *)__pyx_t_1)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); + __pyx_t_1 = 0; + __pyx_t_1 = PyObject_Call(__pyx_builtin_ValueError, ((PyObject *)__pyx_t_3), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(((PyObject *)__pyx_t_3)); __pyx_t_3 = 0; + __Pyx_Raise(__pyx_t_1, 0, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[2]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_L3:; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_3); + __Pyx_AddTraceback("_cdec.Hypergraph.reweight", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ static int __pyx_pw_5_cdec_7Lattice_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ static int __pyx_pw_5_cdec_7Lattice_1__init__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { - PyObject *__pyx_v_plf_tuple = 0; - static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__plf_tuple,0}; + PyObject *__pyx_v_inp = 0; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__inp,0}; int __pyx_r; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("__init__ (wrapper)", 0); @@ -2963,129 +4004,756 @@ static int __pyx_pw_5_cdec_7Lattice_1__init__(PyObject *__pyx_v_self, PyObject * kw_args = PyDict_Size(__pyx_kwds); switch (pos_args) { case 0: - values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__plf_tuple); + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__inp); if (likely(values[0])) kw_args--; else goto __pyx_L5_argtuple_error; } if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 142; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__init__") < 0)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;} } } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { goto __pyx_L5_argtuple_error; } else { values[0] = PyTuple_GET_ITEM(__pyx_args, 0); } - __pyx_v_plf_tuple = ((PyObject*)values[0]); + __pyx_v_inp = values[0]; } goto __pyx_L4_argument_unpacking_done; __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 142; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __Pyx_RaiseArgtupleInvalid("__init__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[3]; __pyx_lineno = 6; __pyx_clineno = __LINE__; goto __pyx_L3_error;} __pyx_L3_error:; __Pyx_AddTraceback("_cdec.Lattice.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); return -1; __pyx_L4_argument_unpacking_done:; - if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_plf_tuple), (&PyTuple_Type), 1, "plf_tuple", 1))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 142; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_r = __pyx_pf_5_cdec_7Lattice___init__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self), __pyx_v_plf_tuple); - goto __pyx_L0; - __pyx_L1_error:; - __pyx_r = -1; - __pyx_L0:; + __pyx_r = __pyx_pf_5_cdec_7Lattice___init__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self), __pyx_v_inp); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":142 +/* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":6 * cdef lattice.Lattice* lattice * - * def __init__(self, tuple plf_tuple): # <<<<<<<<<<<<<< - * self.lattice = new lattice.Lattice() - * cdef bytes plf = str(plf_tuple) + * def __init__(self, inp): # <<<<<<<<<<<<<< + * if isinstance(inp, tuple): + * self.lattice = new lattice.Lattice(len(inp)) */ -static int __pyx_pf_5_cdec_7Lattice___init__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self, PyObject *__pyx_v_plf_tuple) { - PyObject *__pyx_v_plf = 0; +static int __pyx_pf_5_cdec_7Lattice___init__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self, PyObject *__pyx_v_inp) { + PyObject *__pyx_v_i = NULL; + PyObject *__pyx_v_arcs = NULL; int __pyx_r; __Pyx_RefNannyDeclarations PyObject *__pyx_t_1 = NULL; - PyObject *__pyx_t_2 = NULL; - char *__pyx_t_3; + int __pyx_t_2; + Py_ssize_t __pyx_t_3; + PyObject *__pyx_t_4 = NULL; + PyObject *(*__pyx_t_5)(PyObject *); + PyObject *__pyx_t_6 = NULL; + int __pyx_t_7; + char *__pyx_t_8; int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; __Pyx_RefNannySetupContext("__init__", 0); + __Pyx_INCREF(__pyx_v_inp); - /* "_cdec.pyx":143 + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":7 * - * def __init__(self, tuple plf_tuple): - * self.lattice = new lattice.Lattice() # <<<<<<<<<<<<<< - * cdef bytes plf = str(plf_tuple) - * hypergraph.PLFtoLattice(string(<char *>plf), self.lattice) + * def __init__(self, inp): + * if isinstance(inp, tuple): # <<<<<<<<<<<<<< + * self.lattice = new lattice.Lattice(len(inp)) + * for i, arcs in enumerate(inp): + */ + __pyx_t_1 = ((PyObject *)((PyObject*)(&PyTuple_Type))); + __Pyx_INCREF(__pyx_t_1); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_inp, __pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":8 + * def __init__(self, inp): + * if isinstance(inp, tuple): + * self.lattice = new lattice.Lattice(len(inp)) # <<<<<<<<<<<<<< + * for i, arcs in enumerate(inp): + * self[i] = arcs + */ + __pyx_t_3 = PyObject_Length(__pyx_v_inp); if (unlikely(__pyx_t_3 == -1)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 8; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_self->lattice = new Lattice(__pyx_t_3); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":9 + * if isinstance(inp, tuple): + * self.lattice = new lattice.Lattice(len(inp)) + * for i, arcs in enumerate(inp): # <<<<<<<<<<<<<< + * self[i] = arcs + * else: + */ + __Pyx_INCREF(__pyx_int_0); + __pyx_t_1 = __pyx_int_0; + if (PyList_CheckExact(__pyx_v_inp) || PyTuple_CheckExact(__pyx_v_inp)) { + __pyx_t_4 = __pyx_v_inp; __Pyx_INCREF(__pyx_t_4); __pyx_t_3 = 0; + __pyx_t_5 = NULL; + } else { + __pyx_t_3 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_v_inp); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_5 = Py_TYPE(__pyx_t_4)->tp_iternext; + } + for (;;) { + if (!__pyx_t_5 && PyList_CheckExact(__pyx_t_4)) { + if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_4)) break; + __pyx_t_6 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_3); __Pyx_INCREF(__pyx_t_6); __pyx_t_3++; + } else if (!__pyx_t_5 && PyTuple_CheckExact(__pyx_t_4)) { + if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_4)) break; + __pyx_t_6 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_3); __Pyx_INCREF(__pyx_t_6); __pyx_t_3++; + } else { + __pyx_t_6 = __pyx_t_5(__pyx_t_4); + if (unlikely(!__pyx_t_6)) { + if (PyErr_Occurred()) { + if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) PyErr_Clear(); + else {__pyx_filename = __pyx_f[3]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + break; + } + __Pyx_GOTREF(__pyx_t_6); + } + __Pyx_XDECREF(__pyx_v_arcs); + __pyx_v_arcs = __pyx_t_6; + __pyx_t_6 = 0; + __Pyx_INCREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_v_i); + __pyx_v_i = __pyx_t_1; + __pyx_t_6 = PyNumber_Add(__pyx_t_1, __pyx_int_1); if (unlikely(!__pyx_t_6)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_6); + __Pyx_DECREF(__pyx_t_1); + __pyx_t_1 = __pyx_t_6; + __pyx_t_6 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":10 + * self.lattice = new lattice.Lattice(len(inp)) + * for i, arcs in enumerate(inp): + * self[i] = arcs # <<<<<<<<<<<<<< + * else: + * if isinstance(inp, unicode): + */ + if (PyObject_SetItem(((PyObject *)__pyx_v_self), __pyx_v_i, __pyx_v_arcs) < 0) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + goto __pyx_L3; + } + /*else*/ { + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":12 + * self[i] = arcs + * else: + * if isinstance(inp, unicode): # <<<<<<<<<<<<<< + * inp = inp.encode('utf8') + * if not isinstance(inp, str): */ - __pyx_v_self->lattice = new Lattice(); + __pyx_t_1 = ((PyObject *)((PyObject*)(&PyUnicode_Type))); + __Pyx_INCREF(__pyx_t_1); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_inp, __pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":13 + * else: + * if isinstance(inp, unicode): + * inp = inp.encode('utf8') # <<<<<<<<<<<<<< + * if not isinstance(inp, str): + * raise TypeError('Cannot create lattice from %s' % type(inp)) + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_inp, __pyx_n_s__encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_4 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_k_tuple_12), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_v_inp); + __pyx_v_inp = __pyx_t_4; + __pyx_t_4 = 0; + goto __pyx_L6; + } + __pyx_L6:; - /* "_cdec.pyx":144 - * def __init__(self, tuple plf_tuple): - * self.lattice = new lattice.Lattice() - * cdef bytes plf = str(plf_tuple) # <<<<<<<<<<<<<< - * hypergraph.PLFtoLattice(string(<char *>plf), self.lattice) + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":14 + * 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() + */ + __pyx_t_4 = ((PyObject *)((PyObject*)(&PyString_Type))); + __Pyx_INCREF(__pyx_t_4); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_inp, __pyx_t_4); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __pyx_t_7 = (!__pyx_t_2); + if (__pyx_t_7) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":15 + * 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) + */ + __pyx_t_4 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_13), ((PyObject *)Py_TYPE(__pyx_v_inp))); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_4)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_4)); + __pyx_t_4 = 0; + __pyx_t_4 = PyObject_Call(__pyx_builtin_TypeError, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_Raise(__pyx_t_4, 0, 0, 0); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + {__pyx_filename = __pyx_f[3]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L7; + } + __pyx_L7:; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":16 + * 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) * */ - __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 144; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_1); - __Pyx_INCREF(((PyObject *)__pyx_v_plf_tuple)); - PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_plf_tuple)); - __Pyx_GIVEREF(((PyObject *)__pyx_v_plf_tuple)); - __pyx_t_2 = PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 144; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; - if (!(likely(PyBytes_CheckExact(__pyx_t_2))||((__pyx_t_2) == Py_None)||(PyErr_Format(PyExc_TypeError, "Expected bytes, got %.200s", Py_TYPE(__pyx_t_2)->tp_name), 0))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 144; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_v_plf = ((PyObject*)__pyx_t_2); - __pyx_t_2 = 0; + __pyx_v_self->lattice = new Lattice(); - /* "_cdec.pyx":145 - * self.lattice = new lattice.Lattice() - * cdef bytes plf = str(plf_tuple) - * hypergraph.PLFtoLattice(string(<char *>plf), self.lattice) # <<<<<<<<<<<<<< + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":17 + * raise TypeError('Cannot create lattice from %s' % type(inp)) + * self.lattice = new lattice.Lattice() + * lattice.ConvertTextToLattice(string(<char *>inp), self.lattice) # <<<<<<<<<<<<<< * - * def __str__(self): + * def __getitem__(self, int index): */ - __pyx_t_3 = PyBytes_AsString(((PyObject *)__pyx_v_plf)); if (unlikely((!__pyx_t_3) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 145; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - HypergraphIO::PLFtoLattice(std::string(((char *)__pyx_t_3)), __pyx_v_self->lattice); + __pyx_t_8 = PyBytes_AsString(__pyx_v_inp); if (unlikely((!__pyx_t_8) && PyErr_Occurred())) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + LatticeTools::ConvertTextToLattice(std::string(((char *)__pyx_t_8)), __pyx_v_self->lattice); + } + __pyx_L3:; __pyx_r = 0; goto __pyx_L0; __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_6); __Pyx_AddTraceback("_cdec.Lattice.__init__", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = -1; __pyx_L0:; - __Pyx_XDECREF(__pyx_v_plf); + __Pyx_XDECREF(__pyx_v_i); + __Pyx_XDECREF(__pyx_v_arcs); + __Pyx_XDECREF(__pyx_v_inp); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_7Lattice_3__str__(PyObject *__pyx_v_self); /*proto*/ -static PyObject *__pyx_pw_5_cdec_7Lattice_3__str__(PyObject *__pyx_v_self) { +static PyObject *__pyx_pw_5_cdec_7Lattice_3__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_index); /*proto*/ +static PyObject *__pyx_pw_5_cdec_7Lattice_3__getitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_index) { + int __pyx_v_index; + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__getitem__ (wrapper)", 0); + assert(__pyx_arg_index); { + __pyx_v_index = __Pyx_PyInt_AsInt(__pyx_arg_index); if (unlikely((__pyx_v_index == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L3_error:; + __Pyx_AddTraceback("_cdec.Lattice.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_r = __pyx_pf_5_cdec_7Lattice_2__getitem__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self), ((int)__pyx_v_index)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":19 + * lattice.ConvertTextToLattice(string(<char *>inp), self.lattice) + * + * def __getitem__(self, int index): # <<<<<<<<<<<<<< + * if not 0 <= index < len(self): + * raise IndexError('lattice index out of range') + */ + +static PyObject *__pyx_pf_5_cdec_7Lattice_2__getitem__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self, int __pyx_v_index) { + PyObject *__pyx_v_arcs = NULL; + std::vector<LatticeArc> __pyx_v_arc_vector; + LatticeArc *__pyx_v_arc; + PyObject *__pyx_v_label = 0; + unsigned int __pyx_v_i; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + int __pyx_t_1; + Py_ssize_t __pyx_t_2; + int __pyx_t_3; + PyObject *__pyx_t_4 = NULL; + size_t __pyx_t_5; + unsigned int __pyx_t_6; + PyObject *__pyx_t_7 = NULL; + PyObject *__pyx_t_8 = NULL; + PyObject *__pyx_t_9 = NULL; + int __pyx_t_10; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("__getitem__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":20 + * + * def __getitem__(self, int index): + * if not 0 <= index < len(self): # <<<<<<<<<<<<<< + * raise IndexError('lattice index out of range') + * arcs = [] + */ + __pyx_t_1 = (0 <= __pyx_v_index); + if (__pyx_t_1) { + __pyx_t_2 = PyObject_Length(((PyObject *)__pyx_v_self)); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = (__pyx_v_index < __pyx_t_2); + } + __pyx_t_3 = (!__pyx_t_1); + if (__pyx_t_3) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":21 + * def __getitem__(self, int index): + * if not 0 <= index < len(self): + * raise IndexError('lattice index out of range') # <<<<<<<<<<<<<< + * arcs = [] + * cdef vector[lattice.LatticeArc] arc_vector = self.lattice[0][index] + */ + __pyx_t_4 = PyObject_Call(__pyx_builtin_IndexError, ((PyObject *)__pyx_k_tuple_15), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_Raise(__pyx_t_4, 0, 0, 0); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + {__pyx_filename = __pyx_f[3]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L3; + } + __pyx_L3:; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":22 + * if not 0 <= index < len(self): + * raise IndexError('lattice index out of range') + * arcs = [] # <<<<<<<<<<<<<< + * cdef vector[lattice.LatticeArc] arc_vector = self.lattice[0][index] + * cdef lattice.LatticeArc* arc + */ + __pyx_t_4 = PyList_New(0); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 22; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_v_arcs = __pyx_t_4; + __pyx_t_4 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":23 + * raise IndexError('lattice index out of range') + * arcs = [] + * cdef vector[lattice.LatticeArc] arc_vector = self.lattice[0][index] # <<<<<<<<<<<<<< + * cdef lattice.LatticeArc* arc + * cdef str label + */ + __pyx_v_arc_vector = ((__pyx_v_self->lattice[0])[__pyx_v_index]); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":27 + * cdef str label + * cdef unsigned i + * for i in range(arc_vector.size()): # <<<<<<<<<<<<<< + * arc = &arc_vector[i] + * label = TDConvert(arc.label) + */ + __pyx_t_5 = __pyx_v_arc_vector.size(); + for (__pyx_t_6 = 0; __pyx_t_6 < __pyx_t_5; __pyx_t_6+=1) { + __pyx_v_i = __pyx_t_6; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":28 + * cdef unsigned i + * for i in range(arc_vector.size()): + * arc = &arc_vector[i] # <<<<<<<<<<<<<< + * label = TDConvert(arc.label) + * arcs.append((label.decode('utf8'), arc.cost, arc.dist2next)) + */ + __pyx_v_arc = (&(__pyx_v_arc_vector[__pyx_v_i])); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":29 + * for i in range(arc_vector.size()): + * arc = &arc_vector[i] + * label = TDConvert(arc.label) # <<<<<<<<<<<<<< + * arcs.append((label.decode('utf8'), arc.cost, arc.dist2next)) + * return tuple(arcs) + */ + __pyx_t_4 = PyBytes_FromString(TD::Convert(__pyx_v_arc->label)); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_4)); + if (!(likely(PyString_CheckExact(((PyObject *)__pyx_t_4)))||(PyErr_Format(PyExc_TypeError, "Expected str, got %.200s", Py_TYPE(((PyObject *)__pyx_t_4))->tp_name), 0))) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 29; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_XDECREF(((PyObject *)__pyx_v_label)); + __pyx_v_label = ((PyObject*)__pyx_t_4); + __pyx_t_4 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":30 + * arc = &arc_vector[i] + * label = TDConvert(arc.label) + * arcs.append((label.decode('utf8'), arc.cost, arc.dist2next)) # <<<<<<<<<<<<<< + * return tuple(arcs) + * + */ + __pyx_t_4 = PyObject_GetAttr(((PyObject *)__pyx_v_label), __pyx_n_s__decode); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_7 = PyObject_Call(__pyx_t_4, ((PyObject *)__pyx_k_tuple_16), NULL); if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __pyx_t_4 = PyFloat_FromDouble(__pyx_v_arc->cost); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_8 = PyInt_FromLong(__pyx_v_arc->dist2next); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + __pyx_t_9 = PyTuple_New(3); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + PyTuple_SET_ITEM(__pyx_t_9, 0, __pyx_t_7); + __Pyx_GIVEREF(__pyx_t_7); + PyTuple_SET_ITEM(__pyx_t_9, 1, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + PyTuple_SET_ITEM(__pyx_t_9, 2, __pyx_t_8); + __Pyx_GIVEREF(__pyx_t_8); + __pyx_t_7 = 0; + __pyx_t_4 = 0; + __pyx_t_8 = 0; + __pyx_t_10 = PyList_Append(__pyx_v_arcs, ((PyObject *)__pyx_t_9)); if (unlikely(__pyx_t_10 == -1)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(((PyObject *)__pyx_t_9)); __pyx_t_9 = 0; + } + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":31 + * label = TDConvert(arc.label) + * arcs.append((label.decode('utf8'), arc.cost, arc.dist2next)) + * return tuple(arcs) # <<<<<<<<<<<<<< + * + * def __setitem__(self, int index, tuple arcs): + */ + __Pyx_XDECREF(__pyx_r); + __pyx_t_9 = ((PyObject *)PyList_AsTuple(__pyx_v_arcs)); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_9)); + __pyx_r = ((PyObject *)__pyx_t_9); + __pyx_t_9 = 0; + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_XDECREF(__pyx_t_9); + __Pyx_AddTraceback("_cdec.Lattice.__getitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XDECREF(__pyx_v_arcs); + __Pyx_XDECREF(__pyx_v_label); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static int __pyx_pw_5_cdec_7Lattice_5__setitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_index, PyObject *__pyx_v_arcs); /*proto*/ +static int __pyx_pw_5_cdec_7Lattice_5__setitem__(PyObject *__pyx_v_self, PyObject *__pyx_arg_index, PyObject *__pyx_v_arcs) { + int __pyx_v_index; + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__setitem__ (wrapper)", 0); + assert(__pyx_arg_index); { + __pyx_v_index = __Pyx_PyInt_AsInt(__pyx_arg_index); if (unlikely((__pyx_v_index == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L3_error:; + __Pyx_AddTraceback("_cdec.Lattice.__setitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + if (unlikely(!__Pyx_ArgTypeTest(((PyObject *)__pyx_v_arcs), (&PyTuple_Type), 1, "arcs", 1))) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_r = __pyx_pf_5_cdec_7Lattice_4__setitem__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self), ((int)__pyx_v_index), ((PyObject*)__pyx_v_arcs)); + goto __pyx_L0; + __pyx_L1_error:; + __pyx_r = -1; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":33 + * return tuple(arcs) + * + * def __setitem__(self, int index, tuple arcs): # <<<<<<<<<<<<<< + * if not 0 <= index < len(self): + * raise IndexError('lattice index out of range') + */ + +static int __pyx_pf_5_cdec_7Lattice_4__setitem__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self, int __pyx_v_index, PyObject *__pyx_v_arcs) { + LatticeArc *__pyx_v_arc; + PyObject *__pyx_v_label = NULL; + PyObject *__pyx_v_cost = NULL; + PyObject *__pyx_v_dist2next = NULL; + int __pyx_r; + __Pyx_RefNannyDeclarations + int __pyx_t_1; + Py_ssize_t __pyx_t_2; + int __pyx_t_3; + PyObject *__pyx_t_4 = NULL; + PyObject *__pyx_t_5 = NULL; + PyObject *__pyx_t_6 = NULL; + PyObject *__pyx_t_7 = NULL; + PyObject *__pyx_t_8 = NULL; + PyObject *__pyx_t_9 = NULL; + PyObject *(*__pyx_t_10)(PyObject *); + char *__pyx_t_11; + double __pyx_t_12; + int __pyx_t_13; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("__setitem__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":34 + * + * def __setitem__(self, int index, tuple arcs): + * if not 0 <= index < len(self): # <<<<<<<<<<<<<< + * raise IndexError('lattice index out of range') + * cdef lattice.LatticeArc* arc + */ + __pyx_t_1 = (0 <= __pyx_v_index); + if (__pyx_t_1) { + __pyx_t_2 = PyObject_Length(((PyObject *)__pyx_v_self)); if (unlikely(__pyx_t_2 == -1)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 34; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = (__pyx_v_index < __pyx_t_2); + } + __pyx_t_3 = (!__pyx_t_1); + if (__pyx_t_3) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":35 + * def __setitem__(self, int index, tuple arcs): + * if not 0 <= index < len(self): + * raise IndexError('lattice index out of range') # <<<<<<<<<<<<<< + * cdef lattice.LatticeArc* arc + * for (label, cost, dist2next) in arcs: + */ + __pyx_t_4 = PyObject_Call(__pyx_builtin_IndexError, ((PyObject *)__pyx_k_tuple_17), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 35; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_Raise(__pyx_t_4, 0, 0, 0); + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + {__pyx_filename = __pyx_f[3]; __pyx_lineno = 35; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L3; + } + __pyx_L3:; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":37 + * 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') + */ + if (unlikely(((PyObject *)__pyx_v_arcs) == Py_None)) { + PyErr_SetString(PyExc_TypeError, "'NoneType' object is not iterable"); {__pyx_filename = __pyx_f[3]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_4 = ((PyObject *)__pyx_v_arcs); __Pyx_INCREF(__pyx_t_4); __pyx_t_2 = 0; + for (;;) { + if (__pyx_t_2 >= PyTuple_GET_SIZE(__pyx_t_4)) break; + __pyx_t_5 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_2); __Pyx_INCREF(__pyx_t_5); __pyx_t_2++; + if ((likely(PyTuple_CheckExact(__pyx_t_5))) || (PyList_CheckExact(__pyx_t_5))) { + PyObject* sequence = __pyx_t_5; + if (likely(PyTuple_CheckExact(sequence))) { + if (unlikely(PyTuple_GET_SIZE(sequence) != 3)) { + if (PyTuple_GET_SIZE(sequence) > 3) __Pyx_RaiseTooManyValuesError(3); + else __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(sequence)); + {__pyx_filename = __pyx_f[3]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_6 = PyTuple_GET_ITEM(sequence, 0); + __pyx_t_7 = PyTuple_GET_ITEM(sequence, 1); + __pyx_t_8 = PyTuple_GET_ITEM(sequence, 2); + } else { + if (unlikely(PyList_GET_SIZE(sequence) != 3)) { + if (PyList_GET_SIZE(sequence) > 3) __Pyx_RaiseTooManyValuesError(3); + else __Pyx_RaiseNeedMoreValuesError(PyList_GET_SIZE(sequence)); + {__pyx_filename = __pyx_f[3]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_t_6 = PyList_GET_ITEM(sequence, 0); + __pyx_t_7 = PyList_GET_ITEM(sequence, 1); + __pyx_t_8 = PyList_GET_ITEM(sequence, 2); + } + __Pyx_INCREF(__pyx_t_6); + __Pyx_INCREF(__pyx_t_7); + __Pyx_INCREF(__pyx_t_8); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + } else { + Py_ssize_t index = -1; + __pyx_t_9 = PyObject_GetIter(__pyx_t_5); if (unlikely(!__pyx_t_9)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_9); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __pyx_t_10 = Py_TYPE(__pyx_t_9)->tp_iternext; + index = 0; __pyx_t_6 = __pyx_t_10(__pyx_t_9); if (unlikely(!__pyx_t_6)) goto __pyx_L6_unpacking_failed; + __Pyx_GOTREF(__pyx_t_6); + index = 1; __pyx_t_7 = __pyx_t_10(__pyx_t_9); if (unlikely(!__pyx_t_7)) goto __pyx_L6_unpacking_failed; + __Pyx_GOTREF(__pyx_t_7); + index = 2; __pyx_t_8 = __pyx_t_10(__pyx_t_9); if (unlikely(!__pyx_t_8)) goto __pyx_L6_unpacking_failed; + __Pyx_GOTREF(__pyx_t_8); + if (__Pyx_IternextUnpackEndCheck(__pyx_t_10(__pyx_t_9), 3) < 0) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; + goto __pyx_L7_unpacking_done; + __pyx_L6_unpacking_failed:; + __Pyx_DECREF(__pyx_t_9); __pyx_t_9 = 0; + if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration)) PyErr_Clear(); + if (!PyErr_Occurred()) __Pyx_RaiseNeedMoreValuesError(index); + {__pyx_filename = __pyx_f[3]; __pyx_lineno = 37; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_L7_unpacking_done:; + } + __Pyx_XDECREF(__pyx_v_label); + __pyx_v_label = __pyx_t_6; + __pyx_t_6 = 0; + __Pyx_XDECREF(__pyx_v_cost); + __pyx_v_cost = __pyx_t_7; + __pyx_t_7 = 0; + __Pyx_XDECREF(__pyx_v_dist2next); + __pyx_v_dist2next = __pyx_t_8; + __pyx_t_8 = 0; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":38 + * 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) + */ + __pyx_t_5 = ((PyObject *)((PyObject*)(&PyUnicode_Type))); + __Pyx_INCREF(__pyx_t_5); + __pyx_t_3 = __Pyx_TypeCheck(__pyx_v_label, __pyx_t_5); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + if (__pyx_t_3) { + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":39 + * for (label, cost, dist2next) in arcs: + * if isinstance(label, unicode): + * label = label.encode('utf8') # <<<<<<<<<<<<<< + * arc = new lattice.LatticeArc(TDConvert(<char *>label), cost, dist2next) + * self.lattice[0][index].push_back(arc[0]) + */ + __pyx_t_5 = PyObject_GetAttr(__pyx_v_label, __pyx_n_s__encode); if (unlikely(!__pyx_t_5)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 39; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_5); + __pyx_t_8 = PyObject_Call(__pyx_t_5, ((PyObject *)__pyx_k_tuple_18), NULL); if (unlikely(!__pyx_t_8)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 39; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_8); + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_DECREF(__pyx_v_label); + __pyx_v_label = __pyx_t_8; + __pyx_t_8 = 0; + goto __pyx_L8; + } + __pyx_L8:; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":40 + * if isinstance(label, unicode): + * label = label.encode('utf8') + * arc = new lattice.LatticeArc(TDConvert(<char *>label), cost, dist2next) # <<<<<<<<<<<<<< + * self.lattice[0][index].push_back(arc[0]) + * del arc + */ + __pyx_t_11 = PyBytes_AsString(__pyx_v_label); if (unlikely((!__pyx_t_11) && PyErr_Occurred())) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_12 = __pyx_PyFloat_AsDouble(__pyx_v_cost); if (unlikely((__pyx_t_12 == (double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_13 = __Pyx_PyInt_AsInt(__pyx_v_dist2next); if (unlikely((__pyx_t_13 == (int)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_arc = new LatticeArc(TD::Convert(((char *)__pyx_t_11)), __pyx_t_12, __pyx_t_13); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":41 + * label = label.encode('utf8') + * arc = new lattice.LatticeArc(TDConvert(<char *>label), cost, dist2next) + * self.lattice[0][index].push_back(arc[0]) # <<<<<<<<<<<<<< + * del arc + * + */ + ((__pyx_v_self->lattice[0])[__pyx_v_index]).push_back((__pyx_v_arc[0])); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":42 + * arc = new lattice.LatticeArc(TDConvert(<char *>label), cost, dist2next) + * self.lattice[0][index].push_back(arc[0]) + * del arc # <<<<<<<<<<<<<< + * + * def __len__(self): + */ + delete __pyx_v_arc; + } + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); + __Pyx_XDECREF(__pyx_t_8); + __Pyx_XDECREF(__pyx_t_9); + __Pyx_AddTraceback("_cdec.Lattice.__setitem__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = -1; + __pyx_L0:; + __Pyx_XDECREF(__pyx_v_label); + __Pyx_XDECREF(__pyx_v_cost); + __Pyx_XDECREF(__pyx_v_dist2next); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static Py_ssize_t __pyx_pw_5_cdec_7Lattice_7__len__(PyObject *__pyx_v_self); /*proto*/ +static Py_ssize_t __pyx_pw_5_cdec_7Lattice_7__len__(PyObject *__pyx_v_self) { + Py_ssize_t __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__len__ (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_7Lattice_6__len__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":44 + * del arc + * + * def __len__(self): # <<<<<<<<<<<<<< + * return self.lattice.size() + * + */ + +static Py_ssize_t __pyx_pf_5_cdec_7Lattice_6__len__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self) { + Py_ssize_t __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__len__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":45 + * + * def __len__(self): + * return self.lattice.size() # <<<<<<<<<<<<<< + * + * def __str__(self): + */ + __pyx_r = __pyx_v_self->lattice->size(); + goto __pyx_L0; + + __pyx_r = 0; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_7Lattice_9__str__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pw_5_cdec_7Lattice_9__str__(PyObject *__pyx_v_self) { PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("__str__ (wrapper)", 0); - __pyx_r = __pyx_pf_5_cdec_7Lattice_2__str__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self)); + __pyx_r = __pyx_pf_5_cdec_7Lattice_8__str__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":147 - * hypergraph.PLFtoLattice(string(<char *>plf), self.lattice) +/* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":47 + * return self.lattice.size() * * def __str__(self): # <<<<<<<<<<<<<< - * return hypergraph.AsPLF(self.lattice[0]).c_str() + * return hypergraph.AsPLF(self.lattice[0], True).c_str() * */ -static PyObject *__pyx_pf_5_cdec_7Lattice_2__str__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self) { +static PyObject *__pyx_pf_5_cdec_7Lattice_8__str__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self) { PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations PyObject *__pyx_t_1 = NULL; @@ -3094,15 +4762,15 @@ static PyObject *__pyx_pf_5_cdec_7Lattice_2__str__(struct __pyx_obj_5_cdec_Latti int __pyx_clineno = 0; __Pyx_RefNannySetupContext("__str__", 0); - /* "_cdec.pyx":148 + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":48 * * def __str__(self): - * return hypergraph.AsPLF(self.lattice[0]).c_str() # <<<<<<<<<<<<<< + * return hypergraph.AsPLF(self.lattice[0], True).c_str() # <<<<<<<<<<<<<< * * def __iter__(self): */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = PyBytes_FromString(HypergraphIO::AsPLF((__pyx_v_self->lattice[0]), NULL).c_str()); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 148; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_t_1 = PyBytes_FromString(HypergraphIO::AsPLF((__pyx_v_self->lattice[0]), 1).c_str()); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_1)); __pyx_r = ((PyObject *)__pyx_t_1); __pyx_t_1 = 0; @@ -3119,143 +4787,1068 @@ static PyObject *__pyx_pf_5_cdec_7Lattice_2__str__(struct __pyx_obj_5_cdec_Latti __Pyx_RefNannyFinishContext(); return __pyx_r; } +static PyObject *__pyx_gb_5_cdec_7Lattice_12generator5(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value); /* proto */ /* Python wrapper */ -static PyObject *__pyx_pw_5_cdec_7Lattice_5__iter__(PyObject *__pyx_v_self); /*proto*/ -static PyObject *__pyx_pw_5_cdec_7Lattice_5__iter__(PyObject *__pyx_v_self) { +static PyObject *__pyx_pw_5_cdec_7Lattice_11__iter__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pw_5_cdec_7Lattice_11__iter__(PyObject *__pyx_v_self) { PyObject *__pyx_r = 0; __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("__iter__ (wrapper)", 0); - __pyx_r = __pyx_pf_5_cdec_7Lattice_4__iter__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self)); + __pyx_r = __pyx_pf_5_cdec_7Lattice_10__iter__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self)); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "_cdec.pyx":150 - * return hypergraph.AsPLF(self.lattice[0]).c_str() +/* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":50 + * return hypergraph.AsPLF(self.lattice[0], True).c_str() * * def __iter__(self): # <<<<<<<<<<<<<< - * return iter(eval(str(self))) - * + * cdef unsigned i + * for i in range(len(self)): */ -static PyObject *__pyx_pf_5_cdec_7Lattice_4__iter__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self) { +static PyObject *__pyx_pf_5_cdec_7Lattice_10__iter__(struct __pyx_obj_5_cdec_Lattice *__pyx_v_self) { + struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *__pyx_cur_scope; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations - PyObject *__pyx_t_1 = NULL; - PyObject *__pyx_t_2 = NULL; - PyObject *__pyx_t_3 = NULL; - PyObject *__pyx_t_4 = NULL; int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; __Pyx_RefNannySetupContext("__iter__", 0); + __pyx_cur_scope = (struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *)__pyx_ptype_5_cdec___pyx_scope_struct_5___iter__->tp_new(__pyx_ptype_5_cdec___pyx_scope_struct_5___iter__, __pyx_empty_tuple, NULL); + if (unlikely(!__pyx_cur_scope)) { + __Pyx_RefNannyFinishContext(); + return NULL; + } + __Pyx_GOTREF(__pyx_cur_scope); + __pyx_cur_scope->__pyx_v_self = __pyx_v_self; + __Pyx_INCREF((PyObject *)__pyx_cur_scope->__pyx_v_self); + __Pyx_GIVEREF((PyObject *)__pyx_cur_scope->__pyx_v_self); + { + __pyx_GeneratorObject *gen = __Pyx_Generator_New((__pyx_generator_body_t) __pyx_gb_5_cdec_7Lattice_12generator5, (PyObject *) __pyx_cur_scope); if (unlikely(!gen)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_DECREF(__pyx_cur_scope); + __Pyx_RefNannyFinishContext(); + return (PyObject *) gen; + } - /* "_cdec.pyx":151 - * + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("_cdec.Lattice.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_DECREF(((PyObject *)__pyx_cur_scope)); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +static PyObject *__pyx_gb_5_cdec_7Lattice_12generator5(__pyx_GeneratorObject *__pyx_generator, PyObject *__pyx_sent_value) /* generator body */ +{ + struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *__pyx_cur_scope = ((struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *)__pyx_generator->closure); + PyObject *__pyx_r = NULL; + Py_ssize_t __pyx_t_1; + unsigned int __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("None", 0); + switch (__pyx_generator->resume_label) { + case 0: goto __pyx_L3_first_run; + case 1: goto __pyx_L6_resume_from_yield; + default: /* CPython raises the right error here */ + __Pyx_RefNannyFinishContext(); + return NULL; + } + __pyx_L3_first_run:; + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":52 * def __iter__(self): - * return iter(eval(str(self))) # <<<<<<<<<<<<<< + * cdef unsigned i + * for i in range(len(self)): # <<<<<<<<<<<<<< + * yield self[i] + * + */ + __pyx_t_1 = PyObject_Length(((PyObject *)__pyx_cur_scope->__pyx_v_self)); if (unlikely(__pyx_t_1 == -1)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 52; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + for (__pyx_t_2 = 0; __pyx_t_2 < __pyx_t_1; __pyx_t_2+=1) { + __pyx_cur_scope->__pyx_v_i = __pyx_t_2; + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":53 + * cdef unsigned i + * for i in range(len(self)): + * yield self[i] # <<<<<<<<<<<<<< * * def __dealloc__(self): */ + __pyx_t_3 = __Pyx_GetItemInt(((PyObject *)__pyx_cur_scope->__pyx_v_self), __pyx_cur_scope->__pyx_v_i, sizeof(unsigned int)+1, PyLong_FromUnsignedLong); if (!__pyx_t_3) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_r = __pyx_t_3; + __pyx_t_3 = 0; + __pyx_cur_scope->__pyx_t_0 = __pyx_t_1; + __pyx_cur_scope->__pyx_t_1 = __pyx_t_2; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + /* return from generator, yielding value */ + __pyx_generator->resume_label = 1; + return __pyx_r; + __pyx_L6_resume_from_yield:; + __pyx_t_1 = __pyx_cur_scope->__pyx_t_0; + __pyx_t_2 = __pyx_cur_scope->__pyx_t_1; + if (unlikely(!__pyx_sent_value)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + PyErr_SetNone(PyExc_StopIteration); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_3); + __Pyx_AddTraceback("__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_L0:; __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_1); - __Pyx_INCREF(((PyObject *)__pyx_v_self)); - PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_v_self)); - __Pyx_GIVEREF(((PyObject *)__pyx_v_self)); - __pyx_t_2 = PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_2); - __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; - __pyx_t_1 = __Pyx_Globals(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_3 = PyDict_New(); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(((PyObject *)__pyx_t_3)); - if (((PyObject *)__pyx_v_self)) { - if (PyDict_SetItem(__pyx_t_3, ((PyObject *)__pyx_n_s__self), ((PyObject *)__pyx_v_self)) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_generator->resume_label = -1; + __Pyx_RefNannyFinishContext(); + return NULL; +} + +/* Python wrapper */ +static void __pyx_pw_5_cdec_7Lattice_14__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pw_5_cdec_7Lattice_14__dealloc__(PyObject *__pyx_v_self) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0); + __pyx_pf_5_cdec_7Lattice_13__dealloc__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self)); + __Pyx_RefNannyFinishContext(); +} + +/* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":55 + * yield self[i] + * + * def __dealloc__(self): # <<<<<<<<<<<<<< + * del self.lattice + */ + +static void __pyx_pf_5_cdec_7Lattice_13__dealloc__(CYTHON_UNUSED struct __pyx_obj_5_cdec_Lattice *__pyx_v_self) { + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__dealloc__", 0); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":56 + * + * def __dealloc__(self): + * del self.lattice # <<<<<<<<<<<<<< + */ + delete __pyx_v_self->lattice; + + __Pyx_RefNannyFinishContext(); +} + +/* Python wrapper */ +static int __pyx_pw_5_cdec_7Decoder_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static int __pyx_pw_5_cdec_7Decoder_1__cinit__(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + char *__pyx_v_config; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__config,0}; + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__cinit__ (wrapper)", 0); + { + PyObject* values[1] = {0}; + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args; + const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); + switch (pos_args) { + case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + kw_args = PyDict_Size(__pyx_kwds); + switch (pos_args) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__config); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "__cinit__") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } else if (PyTuple_GET_SIZE(__pyx_args) != 1) { + goto __pyx_L5_argtuple_error; + } else { + values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + } + __pyx_v_config = PyBytes_AsString(values[0]); if (unlikely((!__pyx_v_config) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L3_error;} } - __pyx_t_4 = PyTuple_New(3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_4); - PyTuple_SET_ITEM(__pyx_t_4, 0, __pyx_t_2); - __Pyx_GIVEREF(__pyx_t_2); - PyTuple_SET_ITEM(__pyx_t_4, 1, ((PyObject *)__pyx_t_1)); - __Pyx_GIVEREF(((PyObject *)__pyx_t_1)); - PyTuple_SET_ITEM(__pyx_t_4, 2, ((PyObject *)__pyx_t_3)); - __Pyx_GIVEREF(((PyObject *)__pyx_t_3)); - __pyx_t_2 = 0; + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("__cinit__", 1, 1, 1, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 19; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("_cdec.Decoder.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return -1; + __pyx_L4_argument_unpacking_done:; + __pyx_r = __pyx_pf_5_cdec_7Decoder___cinit__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self), __pyx_v_config); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "_cdec.pyx":19 + * cdef public DenseVector weights + * + * def __cinit__(self, char* config): # <<<<<<<<<<<<<< + * decoder.register_feature_functions() + * cdef istringstream* config_stream = new istringstream(config) + */ + +static int __pyx_pf_5_cdec_7Decoder___cinit__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, char *__pyx_v_config) { + std::istringstream *__pyx_v_config_stream; + int __pyx_r; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("__cinit__", 0); + + /* "_cdec.pyx":20 + * + * def __cinit__(self, char* config): + * decoder.register_feature_functions() # <<<<<<<<<<<<<< + * cdef istringstream* config_stream = new istringstream(config) + * self.dec = new decoder.Decoder(config_stream) + */ + register_feature_functions(); + + /* "_cdec.pyx":21 + * def __cinit__(self, char* config): + * decoder.register_feature_functions() + * cdef istringstream* config_stream = new istringstream(config) # <<<<<<<<<<<<<< + * self.dec = new decoder.Decoder(config_stream) + * del config_stream + */ + __pyx_v_config_stream = new std::istringstream(__pyx_v_config); + + /* "_cdec.pyx":22 + * decoder.register_feature_functions() + * cdef istringstream* config_stream = new istringstream(config) + * self.dec = new decoder.Decoder(config_stream) # <<<<<<<<<<<<<< + * del config_stream + * self.weights = DenseVector() + */ + __pyx_v_self->dec = new Decoder(__pyx_v_config_stream); + + /* "_cdec.pyx":23 + * cdef istringstream* config_stream = new istringstream(config) + * self.dec = new decoder.Decoder(config_stream) + * del config_stream # <<<<<<<<<<<<<< + * self.weights = DenseVector() + * self.weights.vector = &self.dec.CurrentWeightVector() + */ + delete __pyx_v_config_stream; + + /* "_cdec.pyx":24 + * self.dec = new decoder.Decoder(config_stream) + * del config_stream + * self.weights = DenseVector() # <<<<<<<<<<<<<< + * self.weights.vector = &self.dec.CurrentWeightVector() + * + */ + __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_DenseVector)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 24; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + __Pyx_GOTREF(__pyx_v_self->weights); + __Pyx_DECREF(((PyObject *)__pyx_v_self->weights)); + __pyx_v_self->weights = ((struct __pyx_obj_5_cdec_DenseVector *)__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_3 = 0; - __pyx_t_3 = PyObject_Call(__pyx_builtin_eval, ((PyObject *)__pyx_t_4), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_3); - __Pyx_DECREF(((PyObject *)__pyx_t_4)); __pyx_t_4 = 0; - __pyx_t_4 = PyObject_GetIter(__pyx_t_3); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_t_4); - __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; - __pyx_r = __pyx_t_4; - __pyx_t_4 = 0; - goto __pyx_L0; - __pyx_r = Py_None; __Pyx_INCREF(Py_None); + /* "_cdec.pyx":25 + * del config_stream + * self.weights = DenseVector() + * self.weights.vector = &self.dec.CurrentWeightVector() # <<<<<<<<<<<<<< + * + * def __dealloc__(self): + */ + __pyx_v_self->weights->vector = (&__pyx_v_self->dec->CurrentWeightVector()); + + __pyx_r = 0; goto __pyx_L0; __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_2); - __Pyx_XDECREF(__pyx_t_3); - __Pyx_XDECREF(__pyx_t_4); - __Pyx_AddTraceback("_cdec.Lattice.__iter__", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = NULL; + __Pyx_AddTraceback("_cdec.Decoder.__cinit__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = -1; __pyx_L0:; - __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } /* Python wrapper */ -static void __pyx_pw_5_cdec_7Lattice_7__dealloc__(PyObject *__pyx_v_self); /*proto*/ -static void __pyx_pw_5_cdec_7Lattice_7__dealloc__(PyObject *__pyx_v_self) { +static void __pyx_pw_5_cdec_7Decoder_3__dealloc__(PyObject *__pyx_v_self); /*proto*/ +static void __pyx_pw_5_cdec_7Decoder_3__dealloc__(PyObject *__pyx_v_self) { __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("__dealloc__ (wrapper)", 0); - __pyx_pf_5_cdec_7Lattice_6__dealloc__(((struct __pyx_obj_5_cdec_Lattice *)__pyx_v_self)); + __pyx_pf_5_cdec_7Decoder_2__dealloc__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self)); __Pyx_RefNannyFinishContext(); } -/* "_cdec.pyx":153 - * return iter(eval(str(self))) +/* "_cdec.pyx":27 + * self.weights.vector = &self.dec.CurrentWeightVector() * * def __dealloc__(self): # <<<<<<<<<<<<<< - * del self.lattice + * del self.dec * */ -static void __pyx_pf_5_cdec_7Lattice_6__dealloc__(CYTHON_UNUSED struct __pyx_obj_5_cdec_Lattice *__pyx_v_self) { +static void __pyx_pf_5_cdec_7Decoder_2__dealloc__(CYTHON_UNUSED struct __pyx_obj_5_cdec_Decoder *__pyx_v_self) { __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("__dealloc__", 0); - /* "_cdec.pyx":154 + /* "_cdec.pyx":28 * * def __dealloc__(self): - * del self.lattice # <<<<<<<<<<<<<< + * del self.dec # <<<<<<<<<<<<<< * - * # TODO: wrap SparseVector + * def read_weights(self, cfg): */ - delete __pyx_v_self->lattice; + delete __pyx_v_self->dec; + + __Pyx_RefNannyFinishContext(); +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_7Decoder_5read_weights(PyObject *__pyx_v_self, PyObject *__pyx_v_cfg); /*proto*/ +static PyObject *__pyx_pw_5_cdec_7Decoder_5read_weights(PyObject *__pyx_v_self, PyObject *__pyx_v_cfg) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("read_weights (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_7Decoder_4read_weights(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self), ((PyObject *)__pyx_v_cfg)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "_cdec.pyx":30 + * del self.dec + * + * def read_weights(self, cfg): # <<<<<<<<<<<<<< + * with open(cfg) as fp: + * for line in fp: + */ + +static PyObject *__pyx_pf_5_cdec_7Decoder_4read_weights(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, PyObject *__pyx_v_cfg) { + PyObject *__pyx_v_fp = NULL; + PyObject *__pyx_v_line = NULL; + PyObject *__pyx_v_fname = NULL; + PyObject *__pyx_v_value = NULL; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + PyObject *__pyx_t_3 = NULL; + PyObject *__pyx_t_4 = NULL; + PyObject *__pyx_t_5 = NULL; + PyObject *__pyx_t_6 = NULL; + PyObject *__pyx_t_7 = NULL; + Py_ssize_t __pyx_t_8; + PyObject *(*__pyx_t_9)(PyObject *); + PyObject *__pyx_t_10 = NULL; + PyObject *__pyx_t_11 = NULL; + PyObject *(*__pyx_t_12)(PyObject *); + double __pyx_t_13; + int __pyx_t_14; + PyObject *__pyx_t_15 = NULL; + int __pyx_t_16; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("read_weights", 0); + + /* "_cdec.pyx":31 + * + * def read_weights(self, cfg): + * with open(cfg) as fp: # <<<<<<<<<<<<<< + * for line in fp: + * fname, value = line.split() + */ + /*with:*/ { + __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_INCREF(__pyx_v_cfg); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_cfg); + __Pyx_GIVEREF(__pyx_v_cfg); + __pyx_t_2 = PyObject_Call(__pyx_builtin_open, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __pyx_t_3 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s____exit__); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_1 = PyObject_GetAttr(__pyx_t_2, __pyx_n_s____enter__); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_4 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + /*try:*/ { + { + __Pyx_ExceptionSave(&__pyx_t_5, &__pyx_t_6, &__pyx_t_7); + __Pyx_XGOTREF(__pyx_t_5); + __Pyx_XGOTREF(__pyx_t_6); + __Pyx_XGOTREF(__pyx_t_7); + /*try:*/ { + __Pyx_INCREF(__pyx_t_4); + __pyx_v_fp = __pyx_t_4; + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + + /* "_cdec.pyx":32 + * def read_weights(self, cfg): + * with open(cfg) as fp: + * for line in fp: # <<<<<<<<<<<<<< + * fname, value = line.split() + * self.weights[fname.strip()] = float(value) + */ + if (PyList_CheckExact(__pyx_v_fp) || PyTuple_CheckExact(__pyx_v_fp)) { + __pyx_t_4 = __pyx_v_fp; __Pyx_INCREF(__pyx_t_4); __pyx_t_8 = 0; + __pyx_t_9 = NULL; + } else { + __pyx_t_8 = -1; __pyx_t_4 = PyObject_GetIter(__pyx_v_fp); if (unlikely(!__pyx_t_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __Pyx_GOTREF(__pyx_t_4); + __pyx_t_9 = Py_TYPE(__pyx_t_4)->tp_iternext; + } + for (;;) { + if (!__pyx_t_9 && PyList_CheckExact(__pyx_t_4)) { + if (__pyx_t_8 >= PyList_GET_SIZE(__pyx_t_4)) break; + __pyx_t_2 = PyList_GET_ITEM(__pyx_t_4, __pyx_t_8); __Pyx_INCREF(__pyx_t_2); __pyx_t_8++; + } else if (!__pyx_t_9 && PyTuple_CheckExact(__pyx_t_4)) { + if (__pyx_t_8 >= PyTuple_GET_SIZE(__pyx_t_4)) break; + __pyx_t_2 = PyTuple_GET_ITEM(__pyx_t_4, __pyx_t_8); __Pyx_INCREF(__pyx_t_2); __pyx_t_8++; + } else { + __pyx_t_2 = __pyx_t_9(__pyx_t_4); + if (unlikely(!__pyx_t_2)) { + if (PyErr_Occurred()) { + if (likely(PyErr_ExceptionMatches(PyExc_StopIteration))) PyErr_Clear(); + else {__pyx_filename = __pyx_f[0]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + } + break; + } + __Pyx_GOTREF(__pyx_t_2); + } + __Pyx_XDECREF(__pyx_v_line); + __pyx_v_line = __pyx_t_2; + __pyx_t_2 = 0; + + /* "_cdec.pyx":33 + * with open(cfg) as fp: + * for line in fp: + * fname, value = line.split() # <<<<<<<<<<<<<< + * self.weights[fname.strip()] = float(value) + * + */ + __pyx_t_2 = PyObject_GetAttr(__pyx_v_line, __pyx_n_s__split); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_1 = PyObject_Call(__pyx_t_2, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + if ((likely(PyTuple_CheckExact(__pyx_t_1))) || (PyList_CheckExact(__pyx_t_1))) { + PyObject* sequence = __pyx_t_1; + if (likely(PyTuple_CheckExact(sequence))) { + if (unlikely(PyTuple_GET_SIZE(sequence) != 2)) { + if (PyTuple_GET_SIZE(sequence) > 2) __Pyx_RaiseTooManyValuesError(2); + else __Pyx_RaiseNeedMoreValuesError(PyTuple_GET_SIZE(sequence)); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + } + __pyx_t_2 = PyTuple_GET_ITEM(sequence, 0); + __pyx_t_10 = PyTuple_GET_ITEM(sequence, 1); + } else { + if (unlikely(PyList_GET_SIZE(sequence) != 2)) { + if (PyList_GET_SIZE(sequence) > 2) __Pyx_RaiseTooManyValuesError(2); + else __Pyx_RaiseNeedMoreValuesError(PyList_GET_SIZE(sequence)); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + } + __pyx_t_2 = PyList_GET_ITEM(sequence, 0); + __pyx_t_10 = PyList_GET_ITEM(sequence, 1); + } + __Pyx_INCREF(__pyx_t_2); + __Pyx_INCREF(__pyx_t_10); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + } else { + Py_ssize_t index = -1; + __pyx_t_11 = PyObject_GetIter(__pyx_t_1); if (unlikely(!__pyx_t_11)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __Pyx_GOTREF(__pyx_t_11); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_12 = Py_TYPE(__pyx_t_11)->tp_iternext; + index = 0; __pyx_t_2 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_2)) goto __pyx_L18_unpacking_failed; + __Pyx_GOTREF(__pyx_t_2); + index = 1; __pyx_t_10 = __pyx_t_12(__pyx_t_11); if (unlikely(!__pyx_t_10)) goto __pyx_L18_unpacking_failed; + __Pyx_GOTREF(__pyx_t_10); + if (__Pyx_IternextUnpackEndCheck(__pyx_t_12(__pyx_t_11), 2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; + goto __pyx_L19_unpacking_done; + __pyx_L18_unpacking_failed:; + __Pyx_DECREF(__pyx_t_11); __pyx_t_11 = 0; + if (PyErr_Occurred() && PyErr_ExceptionMatches(PyExc_StopIteration)) PyErr_Clear(); + if (!PyErr_Occurred()) __Pyx_RaiseNeedMoreValuesError(index); + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __pyx_L19_unpacking_done:; + } + __Pyx_XDECREF(__pyx_v_fname); + __pyx_v_fname = __pyx_t_2; + __pyx_t_2 = 0; + __Pyx_XDECREF(__pyx_v_value); + __pyx_v_value = __pyx_t_10; + __pyx_t_10 = 0; + + /* "_cdec.pyx":34 + * for line in fp: + * fname, value = line.split() + * self.weights[fname.strip()] = float(value) # <<<<<<<<<<<<<< + * + * def translate(self, sentence, grammar=None): + */ + __pyx_t_13 = __Pyx_PyObject_AsDouble(__pyx_v_value); if (unlikely(__pyx_t_13 == ((double)-1) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __pyx_t_1 = PyFloat_FromDouble(__pyx_t_13); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_10 = PyObject_GetAttr(__pyx_v_fname, __pyx_n_s__strip); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __Pyx_GOTREF(__pyx_t_10); + __pyx_t_2 = PyObject_Call(__pyx_t_10, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __Pyx_GOTREF(__pyx_t_2); + __Pyx_DECREF(__pyx_t_10); __pyx_t_10 = 0; + if (PyObject_SetItem(((PyObject *)__pyx_v_self->weights), __pyx_t_2, __pyx_t_1) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 34; __pyx_clineno = __LINE__; goto __pyx_L7_error;} + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + } + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + } + __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0; + __Pyx_XDECREF(__pyx_t_6); __pyx_t_6 = 0; + __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0; + goto __pyx_L14_try_end; + __pyx_L7_error:; + __Pyx_XDECREF(__pyx_t_11); __pyx_t_11 = 0; + __Pyx_XDECREF(__pyx_t_10); __pyx_t_10 = 0; + __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_XDECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0; + + /* "_cdec.pyx":31 + * + * def read_weights(self, cfg): + * with open(cfg) as fp: # <<<<<<<<<<<<<< + * for line in fp: + * fname, value = line.split() + */ + /*except:*/ { + __Pyx_AddTraceback("_cdec.Decoder.read_weights", __pyx_clineno, __pyx_lineno, __pyx_filename); + if (__Pyx_GetException(&__pyx_t_4, &__pyx_t_1, &__pyx_t_2) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} + __Pyx_GOTREF(__pyx_t_4); + __Pyx_GOTREF(__pyx_t_1); + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_10 = PyTuple_New(3); if (unlikely(!__pyx_t_10)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} + __Pyx_GOTREF(__pyx_t_10); + __Pyx_INCREF(__pyx_t_4); + PyTuple_SET_ITEM(__pyx_t_10, 0, __pyx_t_4); + __Pyx_GIVEREF(__pyx_t_4); + __Pyx_INCREF(__pyx_t_1); + PyTuple_SET_ITEM(__pyx_t_10, 1, __pyx_t_1); + __Pyx_GIVEREF(__pyx_t_1); + __Pyx_INCREF(__pyx_t_2); + PyTuple_SET_ITEM(__pyx_t_10, 2, __pyx_t_2); + __Pyx_GIVEREF(__pyx_t_2); + __pyx_t_15 = PyObject_Call(__pyx_t_3, __pyx_t_10, NULL); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + if (unlikely(!__pyx_t_15)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} + __Pyx_GOTREF(__pyx_t_15); + __pyx_t_14 = __Pyx_PyObject_IsTrue(__pyx_t_15); + __Pyx_DECREF(__pyx_t_15); __pyx_t_15 = 0; + if (unlikely(__pyx_t_14 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} + __pyx_t_16 = (!__pyx_t_14); + if (__pyx_t_16) { + __Pyx_GIVEREF(__pyx_t_4); + __Pyx_GIVEREF(__pyx_t_1); + __Pyx_GIVEREF(__pyx_t_2); + __Pyx_ErrRestore(__pyx_t_4, __pyx_t_1, __pyx_t_2); + __pyx_t_4 = 0; __pyx_t_1 = 0; __pyx_t_2 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L9_except_error;} + goto __pyx_L22; + } + __pyx_L22:; + __Pyx_DECREF(((PyObject *)__pyx_t_10)); __pyx_t_10 = 0; + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + goto __pyx_L8_exception_handled; + } + __pyx_L9_except_error:; + __Pyx_XGIVEREF(__pyx_t_5); + __Pyx_XGIVEREF(__pyx_t_6); + __Pyx_XGIVEREF(__pyx_t_7); + __Pyx_ExceptionReset(__pyx_t_5, __pyx_t_6, __pyx_t_7); + goto __pyx_L1_error; + __pyx_L8_exception_handled:; + __Pyx_XGIVEREF(__pyx_t_5); + __Pyx_XGIVEREF(__pyx_t_6); + __Pyx_XGIVEREF(__pyx_t_7); + __Pyx_ExceptionReset(__pyx_t_5, __pyx_t_6, __pyx_t_7); + __pyx_L14_try_end:; + } + } + /*finally:*/ { + if (__pyx_t_3) { + __pyx_t_7 = PyObject_Call(__pyx_t_3, __pyx_k_tuple_19, NULL); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + if (unlikely(!__pyx_t_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_7); + __pyx_t_16 = __Pyx_PyObject_IsTrue(__pyx_t_7); + __Pyx_DECREF(__pyx_t_7); __pyx_t_7 = 0; + if (unlikely(__pyx_t_16 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + } + goto __pyx_L23; + __pyx_L3_error:; + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + goto __pyx_L1_error; + __pyx_L23:; + } + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_XDECREF(__pyx_t_4); + __Pyx_XDECREF(__pyx_t_10); + __Pyx_XDECREF(__pyx_t_11); + __Pyx_AddTraceback("_cdec.Decoder.read_weights", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XDECREF(__pyx_v_fp); + __Pyx_XDECREF(__pyx_v_line); + __Pyx_XDECREF(__pyx_v_fname); + __Pyx_XDECREF(__pyx_v_value); + __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); + return __pyx_r; } -static PyObject *__pyx_tp_new_5_cdec_Weights(PyTypeObject *t, PyObject *a, PyObject *k) { +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_7Decoder_7translate(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds); /*proto*/ +static PyObject *__pyx_pw_5_cdec_7Decoder_7translate(PyObject *__pyx_v_self, PyObject *__pyx_args, PyObject *__pyx_kwds) { + PyObject *__pyx_v_sentence = 0; + PyObject *__pyx_v_grammar = 0; + static PyObject **__pyx_pyargnames[] = {&__pyx_n_s__sentence,&__pyx_n_s__grammar,0}; + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("translate (wrapper)", 0); + { + PyObject* values[2] = {0,0}; + + /* "_cdec.pyx":36 + * self.weights[fname.strip()] = float(value) + * + * def translate(self, sentence, grammar=None): # <<<<<<<<<<<<<< + * if isinstance(sentence, unicode): + * inp = sentence.strip().encode('utf8') + */ + values[1] = ((PyObject *)Py_None); + if (unlikely(__pyx_kwds)) { + Py_ssize_t kw_args; + const Py_ssize_t pos_args = PyTuple_GET_SIZE(__pyx_args); + switch (pos_args) { + case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + case 0: break; + default: goto __pyx_L5_argtuple_error; + } + kw_args = PyDict_Size(__pyx_kwds); + switch (pos_args) { + case 0: + values[0] = PyDict_GetItem(__pyx_kwds, __pyx_n_s__sentence); + if (likely(values[0])) kw_args--; + else goto __pyx_L5_argtuple_error; + case 1: + if (kw_args > 0) { + PyObject* value = PyDict_GetItem(__pyx_kwds, __pyx_n_s__grammar); + if (value) { values[1] = value; kw_args--; } + } + } + if (unlikely(kw_args > 0)) { + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "translate") < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + } + } else { + switch (PyTuple_GET_SIZE(__pyx_args)) { + case 2: values[1] = PyTuple_GET_ITEM(__pyx_args, 1); + case 1: values[0] = PyTuple_GET_ITEM(__pyx_args, 0); + break; + default: goto __pyx_L5_argtuple_error; + } + } + __pyx_v_sentence = values[0]; + __pyx_v_grammar = values[1]; + } + goto __pyx_L4_argument_unpacking_done; + __pyx_L5_argtuple_error:; + __Pyx_RaiseArgtupleInvalid("translate", 0, 1, 2, PyTuple_GET_SIZE(__pyx_args)); {__pyx_filename = __pyx_f[0]; __pyx_lineno = 36; __pyx_clineno = __LINE__; goto __pyx_L3_error;} + __pyx_L3_error:; + __Pyx_AddTraceback("_cdec.Decoder.translate", __pyx_clineno, __pyx_lineno, __pyx_filename); + __Pyx_RefNannyFinishContext(); + return NULL; + __pyx_L4_argument_unpacking_done:; + __pyx_r = __pyx_pf_5_cdec_7Decoder_6translate(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self), __pyx_v_sentence, __pyx_v_grammar); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +static PyObject *__pyx_pf_5_cdec_7Decoder_6translate(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, PyObject *__pyx_v_sentence, PyObject *__pyx_v_grammar) { + PyObject *__pyx_v_inp = NULL; + BasicObserver __pyx_v_observer; + struct __pyx_obj_5_cdec_Hypergraph *__pyx_v_hg = 0; + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + PyObject *__pyx_t_1 = NULL; + int __pyx_t_2; + PyObject *__pyx_t_3 = NULL; + char *__pyx_t_4; + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("translate", 0); + + /* "_cdec.pyx":37 + * + * def translate(self, sentence, grammar=None): + * if isinstance(sentence, unicode): # <<<<<<<<<<<<<< + * inp = sentence.strip().encode('utf8') + * elif isinstance(sentence, str): + */ + __pyx_t_1 = ((PyObject *)((PyObject*)(&PyUnicode_Type))); + __Pyx_INCREF(__pyx_t_1); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_sentence, __pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "_cdec.pyx":38 + * def translate(self, sentence, grammar=None): + * if isinstance(sentence, unicode): + * inp = sentence.strip().encode('utf8') # <<<<<<<<<<<<<< + * elif isinstance(sentence, str): + * inp = sentence.strip() + */ + __pyx_t_1 = PyObject_GetAttr(__pyx_v_sentence, __pyx_n_s__strip); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_3 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_t_1 = PyObject_GetAttr(__pyx_t_3, __pyx_n_s__encode); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_t_3 = PyObject_Call(__pyx_t_1, ((PyObject *)__pyx_k_tuple_20), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_v_inp = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L3; + } + + /* "_cdec.pyx":39 + * if isinstance(sentence, unicode): + * inp = sentence.strip().encode('utf8') + * elif isinstance(sentence, str): # <<<<<<<<<<<<<< + * inp = sentence.strip() + * elif isinstance(sentence, Lattice): + */ + __pyx_t_3 = ((PyObject *)((PyObject*)(&PyString_Type))); + __Pyx_INCREF(__pyx_t_3); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_sentence, __pyx_t_3); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + if (__pyx_t_2) { + + /* "_cdec.pyx":40 + * inp = sentence.strip().encode('utf8') + * elif isinstance(sentence, str): + * inp = sentence.strip() # <<<<<<<<<<<<<< + * elif isinstance(sentence, Lattice): + * inp = str(sentence) # PLF format + */ + __pyx_t_3 = PyObject_GetAttr(__pyx_v_sentence, __pyx_n_s__strip); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_1 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 40; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __pyx_v_inp = __pyx_t_1; + __pyx_t_1 = 0; + goto __pyx_L3; + } + + /* "_cdec.pyx":41 + * elif isinstance(sentence, str): + * inp = sentence.strip() + * elif isinstance(sentence, Lattice): # <<<<<<<<<<<<<< + * inp = str(sentence) # PLF format + * else: + */ + __pyx_t_1 = ((PyObject *)((PyObject*)__pyx_ptype_5_cdec_Lattice)); + __Pyx_INCREF(__pyx_t_1); + __pyx_t_2 = __Pyx_TypeCheck(__pyx_v_sentence, __pyx_t_1); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + if (__pyx_t_2) { + + /* "_cdec.pyx":42 + * inp = sentence.strip() + * elif isinstance(sentence, Lattice): + * inp = str(sentence) # PLF format # <<<<<<<<<<<<<< + * else: + * raise TypeError('Cannot translate input type %s' % type(sentence)) + */ + __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_INCREF(__pyx_v_sentence); + PyTuple_SET_ITEM(__pyx_t_1, 0, __pyx_v_sentence); + __Pyx_GIVEREF(__pyx_v_sentence); + __pyx_t_3 = PyObject_Call(((PyObject *)((PyObject*)(&PyString_Type))), ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 42; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __pyx_v_inp = __pyx_t_3; + __pyx_t_3 = 0; + goto __pyx_L3; + } + /*else*/ { + + /* "_cdec.pyx":44 + * inp = str(sentence) # PLF format + * else: + * raise TypeError('Cannot translate input type %s' % type(sentence)) # <<<<<<<<<<<<<< + * if grammar: + * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) + */ + __pyx_t_3 = PyNumber_Remainder(((PyObject *)__pyx_kp_s_21), ((PyObject *)Py_TYPE(__pyx_v_sentence))); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(((PyObject *)__pyx_t_3)); + __pyx_t_1 = PyTuple_New(1); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + PyTuple_SET_ITEM(__pyx_t_1, 0, ((PyObject *)__pyx_t_3)); + __Pyx_GIVEREF(((PyObject *)__pyx_t_3)); + __pyx_t_3 = 0; + __pyx_t_3 = PyObject_Call(__pyx_builtin_TypeError, ((PyObject *)__pyx_t_1), NULL); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __Pyx_DECREF(((PyObject *)__pyx_t_1)); __pyx_t_1 = 0; + __Pyx_Raise(__pyx_t_3, 0, 0, 0); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 44; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + } + __pyx_L3:; + + /* "_cdec.pyx":45 + * else: + * raise TypeError('Cannot translate input type %s' % type(sentence)) + * if grammar: # <<<<<<<<<<<<<< + * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) + * cdef decoder.BasicObserver observer = decoder.BasicObserver() + */ + __pyx_t_2 = __Pyx_PyObject_IsTrue(__pyx_v_grammar); if (unlikely(__pyx_t_2 < 0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 45; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__pyx_t_2) { + + /* "_cdec.pyx":46 + * raise TypeError('Cannot translate input type %s' % type(sentence)) + * if grammar: + * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) # <<<<<<<<<<<<<< + * cdef decoder.BasicObserver observer = decoder.BasicObserver() + * self.dec.Decode(string(<char *>inp), &observer) + */ + __pyx_t_4 = PyBytes_AsString(__pyx_v_grammar); if (unlikely((!__pyx_t_4) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 46; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_self->dec->SetSentenceGrammarFromString(std::string(((char *)__pyx_t_4))); + goto __pyx_L4; + } + __pyx_L4:; + + /* "_cdec.pyx":47 + * if grammar: + * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) + * cdef decoder.BasicObserver observer = decoder.BasicObserver() # <<<<<<<<<<<<<< + * self.dec.Decode(string(<char *>inp), &observer) + * if observer.hypergraph == NULL: + */ + __pyx_v_observer = BasicObserver(); + + /* "_cdec.pyx":48 + * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) + * cdef decoder.BasicObserver observer = decoder.BasicObserver() + * self.dec.Decode(string(<char *>inp), &observer) # <<<<<<<<<<<<<< + * if observer.hypergraph == NULL: + * raise ParseFailed() + */ + __pyx_t_4 = PyBytes_AsString(__pyx_v_inp); if (unlikely((!__pyx_t_4) && PyErr_Occurred())) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 48; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_v_self->dec->Decode(std::string(((char *)__pyx_t_4)), (&__pyx_v_observer)); + + /* "_cdec.pyx":49 + * cdef decoder.BasicObserver observer = decoder.BasicObserver() + * self.dec.Decode(string(<char *>inp), &observer) + * if observer.hypergraph == NULL: # <<<<<<<<<<<<<< + * raise ParseFailed() + * cdef Hypergraph hg = Hypergraph() + */ + __pyx_t_2 = (__pyx_v_observer.hypergraph == NULL); + if (__pyx_t_2) { + + /* "_cdec.pyx":50 + * self.dec.Decode(string(<char *>inp), &observer) + * if observer.hypergraph == NULL: + * raise ParseFailed() # <<<<<<<<<<<<<< + * cdef Hypergraph hg = Hypergraph() + * hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0]) + */ + __pyx_t_3 = __Pyx_GetName(__pyx_m, __pyx_n_s__ParseFailed); if (unlikely(!__pyx_t_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_3); + __pyx_t_1 = PyObject_Call(__pyx_t_3, ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0; + __Pyx_Raise(__pyx_t_1, 0, 0, 0); + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + {__pyx_filename = __pyx_f[0]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + goto __pyx_L5; + } + __pyx_L5:; + + /* "_cdec.pyx":51 + * if observer.hypergraph == NULL: + * raise ParseFailed() + * cdef Hypergraph hg = Hypergraph() # <<<<<<<<<<<<<< + * hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0]) + * return hg + */ + __pyx_t_1 = PyObject_Call(((PyObject *)((PyObject*)__pyx_ptype_5_cdec_Hypergraph)), ((PyObject *)__pyx_empty_tuple), NULL); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 51; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_t_1); + __pyx_v_hg = ((struct __pyx_obj_5_cdec_Hypergraph *)__pyx_t_1); + __pyx_t_1 = 0; + + /* "_cdec.pyx":52 + * raise ParseFailed() + * cdef Hypergraph hg = Hypergraph() + * hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0]) # <<<<<<<<<<<<<< + * return hg + */ + __pyx_v_hg->hg = new Hypergraph((__pyx_v_observer.hypergraph[0])); + + /* "_cdec.pyx":53 + * cdef Hypergraph hg = Hypergraph() + * hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0]) + * return hg # <<<<<<<<<<<<<< + */ + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_hg)); + __pyx_r = ((PyObject *)__pyx_v_hg); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_3); + __Pyx_AddTraceback("_cdec.Decoder.translate", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = NULL; + __pyx_L0:; + __Pyx_XDECREF(__pyx_v_inp); + __Pyx_XDECREF((PyObject *)__pyx_v_hg); + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static PyObject *__pyx_pw_5_cdec_7Decoder_7weights_1__get__(PyObject *__pyx_v_self); /*proto*/ +static PyObject *__pyx_pw_5_cdec_7Decoder_7weights_1__get__(PyObject *__pyx_v_self) { + PyObject *__pyx_r = 0; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__get__ (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_7Decoder_7weights___get__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* "_cdec.pyx":17 + * cdef class Decoder: + * cdef decoder.Decoder* dec + * cdef public DenseVector weights # <<<<<<<<<<<<<< + * + * def __cinit__(self, char* config): + */ + +static PyObject *__pyx_pf_5_cdec_7Decoder_7weights___get__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self) { + PyObject *__pyx_r = NULL; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__get__", 0); + __Pyx_XDECREF(__pyx_r); + __Pyx_INCREF(((PyObject *)__pyx_v_self->weights)); + __pyx_r = ((PyObject *)__pyx_v_self->weights); + goto __pyx_L0; + + __pyx_r = Py_None; __Pyx_INCREF(Py_None); + __pyx_L0:; + __Pyx_XGIVEREF(__pyx_r); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static int __pyx_pw_5_cdec_7Decoder_7weights_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_value); /*proto*/ +static int __pyx_pw_5_cdec_7Decoder_7weights_3__set__(PyObject *__pyx_v_self, PyObject *__pyx_v_value) { + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__set__ (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_7Decoder_7weights_2__set__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self), ((PyObject *)__pyx_v_value)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +static int __pyx_pf_5_cdec_7Decoder_7weights_2__set__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self, PyObject *__pyx_v_value) { + int __pyx_r; + __Pyx_RefNannyDeclarations + int __pyx_lineno = 0; + const char *__pyx_filename = NULL; + int __pyx_clineno = 0; + __Pyx_RefNannySetupContext("__set__", 0); + if (!(likely(((__pyx_v_value) == Py_None) || likely(__Pyx_TypeTest(__pyx_v_value, __pyx_ptype_5_cdec_DenseVector))))) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_INCREF(__pyx_v_value); + __Pyx_GIVEREF(__pyx_v_value); + __Pyx_GOTREF(__pyx_v_self->weights); + __Pyx_DECREF(((PyObject *)__pyx_v_self->weights)); + __pyx_v_self->weights = ((struct __pyx_obj_5_cdec_DenseVector *)__pyx_v_value); + + __pyx_r = 0; + goto __pyx_L0; + __pyx_L1_error:; + __Pyx_AddTraceback("_cdec.Decoder.weights.__set__", __pyx_clineno, __pyx_lineno, __pyx_filename); + __pyx_r = -1; + __pyx_L0:; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +/* Python wrapper */ +static int __pyx_pw_5_cdec_7Decoder_7weights_5__del__(PyObject *__pyx_v_self); /*proto*/ +static int __pyx_pw_5_cdec_7Decoder_7weights_5__del__(PyObject *__pyx_v_self) { + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__del__ (wrapper)", 0); + __pyx_r = __pyx_pf_5_cdec_7Decoder_7weights_4__del__(((struct __pyx_obj_5_cdec_Decoder *)__pyx_v_self)); + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +static int __pyx_pf_5_cdec_7Decoder_7weights_4__del__(struct __pyx_obj_5_cdec_Decoder *__pyx_v_self) { + int __pyx_r; + __Pyx_RefNannyDeclarations + __Pyx_RefNannySetupContext("__del__", 0); + __Pyx_INCREF(Py_None); + __Pyx_GIVEREF(Py_None); + __Pyx_GOTREF(__pyx_v_self->weights); + __Pyx_DECREF(((PyObject *)__pyx_v_self->weights)); + __pyx_v_self->weights = ((struct __pyx_obj_5_cdec_DenseVector *)Py_None); + + __pyx_r = 0; + __Pyx_RefNannyFinishContext(); + return __pyx_r; +} + +static PyObject *__pyx_tp_new_5_cdec_DenseVector(PyTypeObject *t, PyObject *a, PyObject *k) { PyObject *o = (*t->tp_alloc)(t, 0); if (!o) return 0; - if (__pyx_pw_5_cdec_7Weights_1__cinit__(o, a, k) < 0) { - Py_DECREF(o); o = 0; - } return o; } -static void __pyx_tp_dealloc_5_cdec_Weights(PyObject *o) { +static void __pyx_tp_dealloc_5_cdec_DenseVector(PyObject *o) { (*Py_TYPE(o)->tp_free)(o); } -static PyObject *__pyx_sq_item_5_cdec_Weights(PyObject *o, Py_ssize_t i) { +static PyObject *__pyx_sq_item_5_cdec_DenseVector(PyObject *o, Py_ssize_t i) { PyObject *r; PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0; r = Py_TYPE(o)->tp_as_mapping->mp_subscript(o, x); @@ -3263,9 +5856,9 @@ static PyObject *__pyx_sq_item_5_cdec_Weights(PyObject *o, Py_ssize_t i) { return r; } -static int __pyx_mp_ass_subscript_5_cdec_Weights(PyObject *o, PyObject *i, PyObject *v) { +static int __pyx_mp_ass_subscript_5_cdec_DenseVector(PyObject *o, PyObject *i, PyObject *v) { if (v) { - return __pyx_pw_5_cdec_7Weights_5__setitem__(o, i, v); + return __pyx_pw_5_cdec_11DenseVector_3__setitem__(o, i, v); } else { PyErr_Format(PyExc_NotImplementedError, @@ -3274,11 +5867,13 @@ static int __pyx_mp_ass_subscript_5_cdec_Weights(PyObject *o, PyObject *i, PyObj } } -static PyMethodDef __pyx_methods_5_cdec_Weights[] = { +static PyMethodDef __pyx_methods_5_cdec_DenseVector[] = { + {__Pyx_NAMESTR("dot"), (PyCFunction)__pyx_pw_5_cdec_11DenseVector_8dot, METH_O, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("tosparse"), (PyCFunction)__pyx_pw_5_cdec_11DenseVector_10tosparse, METH_NOARGS, __Pyx_DOCSTR(0)}, {0, 0, 0, 0} }; -static PyNumberMethods __pyx_tp_as_number_Weights = { +static PyNumberMethods __pyx_tp_as_number_DenseVector = { 0, /*nb_add*/ 0, /*nb_subtract*/ 0, /*nb_multiply*/ @@ -3336,11 +5931,11 @@ static PyNumberMethods __pyx_tp_as_number_Weights = { #endif }; -static PySequenceMethods __pyx_tp_as_sequence_Weights = { +static PySequenceMethods __pyx_tp_as_sequence_DenseVector = { 0, /*sq_length*/ 0, /*sq_concat*/ 0, /*sq_repeat*/ - __pyx_sq_item_5_cdec_Weights, /*sq_item*/ + __pyx_sq_item_5_cdec_DenseVector, /*sq_item*/ 0, /*sq_slice*/ 0, /*sq_ass_item*/ 0, /*sq_ass_slice*/ @@ -3349,13 +5944,13 @@ static PySequenceMethods __pyx_tp_as_sequence_Weights = { 0, /*sq_inplace_repeat*/ }; -static PyMappingMethods __pyx_tp_as_mapping_Weights = { +static PyMappingMethods __pyx_tp_as_mapping_DenseVector = { 0, /*mp_length*/ - __pyx_pw_5_cdec_7Weights_3__getitem__, /*mp_subscript*/ - __pyx_mp_ass_subscript_5_cdec_Weights, /*mp_ass_subscript*/ + __pyx_pw_5_cdec_11DenseVector_1__getitem__, /*mp_subscript*/ + __pyx_mp_ass_subscript_5_cdec_DenseVector, /*mp_ass_subscript*/ }; -static PyBufferProcs __pyx_tp_as_buffer_Weights = { +static PyBufferProcs __pyx_tp_as_buffer_DenseVector = { #if PY_MAJOR_VERSION < 3 0, /*bf_getreadbuffer*/ #endif @@ -3376,12 +5971,12 @@ static PyBufferProcs __pyx_tp_as_buffer_Weights = { #endif }; -static PyTypeObject __pyx_type_5_cdec_Weights = { +static PyTypeObject __pyx_type_5_cdec_DenseVector = { PyVarObject_HEAD_INIT(0, 0) - __Pyx_NAMESTR("_cdec.Weights"), /*tp_name*/ - sizeof(struct __pyx_obj_5_cdec_Weights), /*tp_basicsize*/ + __Pyx_NAMESTR("_cdec.DenseVector"), /*tp_name*/ + sizeof(struct __pyx_obj_5_cdec_DenseVector), /*tp_basicsize*/ 0, /*tp_itemsize*/ - __pyx_tp_dealloc_5_cdec_Weights, /*tp_dealloc*/ + __pyx_tp_dealloc_5_cdec_DenseVector, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -3391,24 +5986,24 @@ static PyTypeObject __pyx_type_5_cdec_Weights = { 0, /*reserved*/ #endif 0, /*tp_repr*/ - &__pyx_tp_as_number_Weights, /*tp_as_number*/ - &__pyx_tp_as_sequence_Weights, /*tp_as_sequence*/ - &__pyx_tp_as_mapping_Weights, /*tp_as_mapping*/ + &__pyx_tp_as_number_DenseVector, /*tp_as_number*/ + &__pyx_tp_as_sequence_DenseVector, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_DenseVector, /*tp_as_mapping*/ 0, /*tp_hash*/ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ - &__pyx_tp_as_buffer_Weights, /*tp_as_buffer*/ + &__pyx_tp_as_buffer_DenseVector, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/ 0, /*tp_doc*/ 0, /*tp_traverse*/ 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ - __pyx_pw_5_cdec_7Weights_7__iter__, /*tp_iter*/ + __pyx_pw_5_cdec_11DenseVector_5__iter__, /*tp_iter*/ 0, /*tp_iternext*/ - __pyx_methods_5_cdec_Weights, /*tp_methods*/ + __pyx_methods_5_cdec_DenseVector, /*tp_methods*/ 0, /*tp_members*/ 0, /*tp_getset*/ 0, /*tp_base*/ @@ -3418,7 +6013,7 @@ static PyTypeObject __pyx_type_5_cdec_Weights = { 0, /*tp_dictoffset*/ 0, /*tp_init*/ 0, /*tp_alloc*/ - __pyx_tp_new_5_cdec_Weights, /*tp_new*/ + __pyx_tp_new_5_cdec_DenseVector, /*tp_new*/ 0, /*tp_free*/ 0, /*tp_is_gc*/ 0, /*tp_bases*/ @@ -3432,78 +6027,43 @@ static PyTypeObject __pyx_type_5_cdec_Weights = { #endif }; -static PyObject *__pyx_tp_new_5_cdec_Decoder(PyTypeObject *t, PyObject *a, PyObject *k) { - struct __pyx_obj_5_cdec_Decoder *p; +static PyObject *__pyx_tp_new_5_cdec_SparseVector(PyTypeObject *t, PyObject *a, PyObject *k) { PyObject *o = (*t->tp_alloc)(t, 0); if (!o) return 0; - p = ((struct __pyx_obj_5_cdec_Decoder *)o); - p->weights = ((struct __pyx_obj_5_cdec_Weights *)Py_None); Py_INCREF(Py_None); - if (__pyx_pw_5_cdec_7Decoder_1__cinit__(o, a, k) < 0) { - Py_DECREF(o); o = 0; - } return o; } -static void __pyx_tp_dealloc_5_cdec_Decoder(PyObject *o) { - struct __pyx_obj_5_cdec_Decoder *p = (struct __pyx_obj_5_cdec_Decoder *)o; - { - PyObject *etype, *eval, *etb; - PyErr_Fetch(&etype, &eval, &etb); - ++Py_REFCNT(o); - __pyx_pw_5_cdec_7Decoder_3__dealloc__(o); - if (PyErr_Occurred()) PyErr_WriteUnraisable(o); - --Py_REFCNT(o); - PyErr_Restore(etype, eval, etb); - } - Py_XDECREF(((PyObject *)p->weights)); +static void __pyx_tp_dealloc_5_cdec_SparseVector(PyObject *o) { (*Py_TYPE(o)->tp_free)(o); } - -static int __pyx_tp_traverse_5_cdec_Decoder(PyObject *o, visitproc v, void *a) { - int e; - struct __pyx_obj_5_cdec_Decoder *p = (struct __pyx_obj_5_cdec_Decoder *)o; - if (p->weights) { - e = (*v)(((PyObject*)p->weights), a); if (e) return e; - } - return 0; -} - -static int __pyx_tp_clear_5_cdec_Decoder(PyObject *o) { - struct __pyx_obj_5_cdec_Decoder *p = (struct __pyx_obj_5_cdec_Decoder *)o; - PyObject* tmp; - tmp = ((PyObject*)p->weights); - p->weights = ((struct __pyx_obj_5_cdec_Weights *)Py_None); Py_INCREF(Py_None); - Py_XDECREF(tmp); - return 0; -} - -static PyObject *__pyx_getprop_5_cdec_7Decoder_weights(PyObject *o, void *x) { - return __pyx_pw_5_cdec_7Decoder_7weights_1__get__(o); +static PyObject *__pyx_sq_item_5_cdec_SparseVector(PyObject *o, Py_ssize_t i) { + PyObject *r; + PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0; + r = Py_TYPE(o)->tp_as_mapping->mp_subscript(o, x); + Py_DECREF(x); + return r; } -static int __pyx_setprop_5_cdec_7Decoder_weights(PyObject *o, PyObject *v, void *x) { +static int __pyx_mp_ass_subscript_5_cdec_SparseVector(PyObject *o, PyObject *i, PyObject *v) { if (v) { - return __pyx_pw_5_cdec_7Decoder_7weights_3__set__(o, v); + return __pyx_pw_5_cdec_12SparseVector_3__setitem__(o, i, v); } else { - return __pyx_pw_5_cdec_7Decoder_7weights_5__del__(o); + PyErr_Format(PyExc_NotImplementedError, + "Subscript deletion not supported by %s", Py_TYPE(o)->tp_name); + return -1; } } -static PyMethodDef __pyx_methods_5_cdec_Decoder[] = { - {__Pyx_NAMESTR("read_weights"), (PyCFunction)__pyx_pw_5_cdec_7Decoder_5read_weights, METH_O, __Pyx_DOCSTR(0)}, - {__Pyx_NAMESTR("translate"), (PyCFunction)__pyx_pw_5_cdec_7Decoder_7translate, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)}, +static PyMethodDef __pyx_methods_5_cdec_SparseVector[] = { + {__Pyx_NAMESTR("dot"), (PyCFunction)__pyx_pw_5_cdec_12SparseVector_8dot, METH_O, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("todense"), (PyCFunction)__pyx_pw_5_cdec_12SparseVector_10todense, METH_NOARGS, __Pyx_DOCSTR(0)}, {0, 0, 0, 0} }; -static struct PyGetSetDef __pyx_getsets_5_cdec_Decoder[] = { - {(char *)"weights", __pyx_getprop_5_cdec_7Decoder_weights, __pyx_setprop_5_cdec_7Decoder_weights, 0, 0}, - {0, 0, 0, 0, 0} -}; - -static PyNumberMethods __pyx_tp_as_number_Decoder = { - 0, /*nb_add*/ - 0, /*nb_subtract*/ +static PyNumberMethods __pyx_tp_as_number_SparseVector = { + __pyx_pw_5_cdec_12SparseVector_26__add__, /*nb_add*/ + __pyx_pw_5_cdec_12SparseVector_28__sub__, /*nb_subtract*/ 0, /*nb_multiply*/ #if PY_MAJOR_VERSION < 3 0, /*nb_divide*/ @@ -3537,11 +6097,11 @@ static PyNumberMethods __pyx_tp_as_number_Decoder = { #if PY_MAJOR_VERSION < 3 0, /*nb_hex*/ #endif - 0, /*nb_inplace_add*/ - 0, /*nb_inplace_subtract*/ - 0, /*nb_inplace_multiply*/ + __pyx_pw_5_cdec_12SparseVector_18__iadd__, /*nb_inplace_add*/ + __pyx_pw_5_cdec_12SparseVector_20__isub__, /*nb_inplace_subtract*/ + __pyx_pw_5_cdec_12SparseVector_22__imul__, /*nb_inplace_multiply*/ #if PY_MAJOR_VERSION < 3 - 0, /*nb_inplace_divide*/ + __pyx_pw_5_cdec_12SparseVector_24__idiv__, /*nb_inplace_divide*/ #endif 0, /*nb_inplace_remainder*/ 0, /*nb_inplace_power*/ @@ -3559,26 +6119,26 @@ static PyNumberMethods __pyx_tp_as_number_Decoder = { #endif }; -static PySequenceMethods __pyx_tp_as_sequence_Decoder = { - 0, /*sq_length*/ +static PySequenceMethods __pyx_tp_as_sequence_SparseVector = { + __pyx_pw_5_cdec_12SparseVector_14__len__, /*sq_length*/ 0, /*sq_concat*/ 0, /*sq_repeat*/ - 0, /*sq_item*/ + __pyx_sq_item_5_cdec_SparseVector, /*sq_item*/ 0, /*sq_slice*/ 0, /*sq_ass_item*/ 0, /*sq_ass_slice*/ - 0, /*sq_contains*/ + __pyx_pw_5_cdec_12SparseVector_16__contains__, /*sq_contains*/ 0, /*sq_inplace_concat*/ 0, /*sq_inplace_repeat*/ }; -static PyMappingMethods __pyx_tp_as_mapping_Decoder = { - 0, /*mp_length*/ - 0, /*mp_subscript*/ - 0, /*mp_ass_subscript*/ +static PyMappingMethods __pyx_tp_as_mapping_SparseVector = { + __pyx_pw_5_cdec_12SparseVector_14__len__, /*mp_length*/ + __pyx_pw_5_cdec_12SparseVector_1__getitem__, /*mp_subscript*/ + __pyx_mp_ass_subscript_5_cdec_SparseVector, /*mp_ass_subscript*/ }; -static PyBufferProcs __pyx_tp_as_buffer_Decoder = { +static PyBufferProcs __pyx_tp_as_buffer_SparseVector = { #if PY_MAJOR_VERSION < 3 0, /*bf_getreadbuffer*/ #endif @@ -3599,12 +6159,12 @@ static PyBufferProcs __pyx_tp_as_buffer_Decoder = { #endif }; -static PyTypeObject __pyx_type_5_cdec_Decoder = { +static PyTypeObject __pyx_type_5_cdec_SparseVector = { PyVarObject_HEAD_INIT(0, 0) - __Pyx_NAMESTR("_cdec.Decoder"), /*tp_name*/ - sizeof(struct __pyx_obj_5_cdec_Decoder), /*tp_basicsize*/ + __Pyx_NAMESTR("_cdec.SparseVector"), /*tp_name*/ + sizeof(struct __pyx_obj_5_cdec_SparseVector), /*tp_basicsize*/ 0, /*tp_itemsize*/ - __pyx_tp_dealloc_5_cdec_Decoder, /*tp_dealloc*/ + __pyx_tp_dealloc_5_cdec_SparseVector, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -3614,26 +6174,26 @@ static PyTypeObject __pyx_type_5_cdec_Decoder = { 0, /*reserved*/ #endif 0, /*tp_repr*/ - &__pyx_tp_as_number_Decoder, /*tp_as_number*/ - &__pyx_tp_as_sequence_Decoder, /*tp_as_sequence*/ - &__pyx_tp_as_mapping_Decoder, /*tp_as_mapping*/ + &__pyx_tp_as_number_SparseVector, /*tp_as_number*/ + &__pyx_tp_as_sequence_SparseVector, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_SparseVector, /*tp_as_mapping*/ 0, /*tp_hash*/ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ - &__pyx_tp_as_buffer_Decoder, /*tp_as_buffer*/ - Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + &__pyx_tp_as_buffer_SparseVector, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE, /*tp_flags*/ 0, /*tp_doc*/ - __pyx_tp_traverse_5_cdec_Decoder, /*tp_traverse*/ - __pyx_tp_clear_5_cdec_Decoder, /*tp_clear*/ - 0, /*tp_richcompare*/ + 0, /*tp_traverse*/ + 0, /*tp_clear*/ + __pyx_pw_5_cdec_12SparseVector_12__richcmp__, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ - 0, /*tp_iter*/ + __pyx_pw_5_cdec_12SparseVector_5__iter__, /*tp_iter*/ 0, /*tp_iternext*/ - __pyx_methods_5_cdec_Decoder, /*tp_methods*/ + __pyx_methods_5_cdec_SparseVector, /*tp_methods*/ 0, /*tp_members*/ - __pyx_getsets_5_cdec_Decoder, /*tp_getset*/ + 0, /*tp_getset*/ 0, /*tp_base*/ 0, /*tp_dict*/ 0, /*tp_descr_get*/ @@ -3641,7 +6201,7 @@ static PyTypeObject __pyx_type_5_cdec_Decoder = { 0, /*tp_dictoffset*/ 0, /*tp_init*/ 0, /*tp_alloc*/ - __pyx_tp_new_5_cdec_Decoder, /*tp_new*/ + __pyx_tp_new_5_cdec_SparseVector, /*tp_new*/ 0, /*tp_free*/ 0, /*tp_is_gc*/ 0, /*tp_bases*/ @@ -3677,10 +6237,15 @@ static void __pyx_tp_dealloc_5_cdec_Hypergraph(PyObject *o) { static PyMethodDef __pyx_methods_5_cdec_Hypergraph[] = { {__Pyx_NAMESTR("viterbi"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_3viterbi, METH_NOARGS, __Pyx_DOCSTR(0)}, {__Pyx_NAMESTR("viterbi_tree"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_5viterbi_tree, METH_NOARGS, __Pyx_DOCSTR(0)}, - {__Pyx_NAMESTR("kbest"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_7kbest, METH_O, __Pyx_DOCSTR(0)}, - {__Pyx_NAMESTR("kbest_tree"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_10kbest_tree, METH_O, __Pyx_DOCSTR(0)}, - {__Pyx_NAMESTR("intersect"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_13intersect, METH_O, __Pyx_DOCSTR(0)}, - {__Pyx_NAMESTR("sample"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_15sample, METH_O, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("viterbi_source_tree"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_7viterbi_source_tree, METH_NOARGS, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("viterbi_features"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_9viterbi_features, METH_NOARGS, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("kbest"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_11kbest, METH_O, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("kbest_tree"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_14kbest_tree, METH_O, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("sample"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_17sample, METH_O, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("intersect"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_20intersect, METH_O, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("prune"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_22prune, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("lattice"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_24lattice, METH_NOARGS, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("reweight"), (PyCFunction)__pyx_pw_5_cdec_10Hypergraph_26reweight, METH_O, __Pyx_DOCSTR(0)}, {0, 0, 0, 0} }; @@ -3849,13 +6414,31 @@ static void __pyx_tp_dealloc_5_cdec_Lattice(PyObject *o) { PyObject *etype, *eval, *etb; PyErr_Fetch(&etype, &eval, &etb); ++Py_REFCNT(o); - __pyx_pw_5_cdec_7Lattice_7__dealloc__(o); + __pyx_pw_5_cdec_7Lattice_14__dealloc__(o); if (PyErr_Occurred()) PyErr_WriteUnraisable(o); --Py_REFCNT(o); PyErr_Restore(etype, eval, etb); } (*Py_TYPE(o)->tp_free)(o); } +static PyObject *__pyx_sq_item_5_cdec_Lattice(PyObject *o, Py_ssize_t i) { + PyObject *r; + PyObject *x = PyInt_FromSsize_t(i); if(!x) return 0; + r = Py_TYPE(o)->tp_as_mapping->mp_subscript(o, x); + Py_DECREF(x); + return r; +} + +static int __pyx_mp_ass_subscript_5_cdec_Lattice(PyObject *o, PyObject *i, PyObject *v) { + if (v) { + return __pyx_pw_5_cdec_7Lattice_5__setitem__(o, i, v); + } + else { + PyErr_Format(PyExc_NotImplementedError, + "Subscript deletion not supported by %s", Py_TYPE(o)->tp_name); + return -1; + } +} static PyMethodDef __pyx_methods_5_cdec_Lattice[] = { {0, 0, 0, 0} @@ -3920,10 +6503,10 @@ static PyNumberMethods __pyx_tp_as_number_Lattice = { }; static PySequenceMethods __pyx_tp_as_sequence_Lattice = { - 0, /*sq_length*/ + __pyx_pw_5_cdec_7Lattice_7__len__, /*sq_length*/ 0, /*sq_concat*/ 0, /*sq_repeat*/ - 0, /*sq_item*/ + __pyx_sq_item_5_cdec_Lattice, /*sq_item*/ 0, /*sq_slice*/ 0, /*sq_ass_item*/ 0, /*sq_ass_slice*/ @@ -3933,9 +6516,9 @@ static PySequenceMethods __pyx_tp_as_sequence_Lattice = { }; static PyMappingMethods __pyx_tp_as_mapping_Lattice = { - 0, /*mp_length*/ - 0, /*mp_subscript*/ - 0, /*mp_ass_subscript*/ + __pyx_pw_5_cdec_7Lattice_7__len__, /*mp_length*/ + __pyx_pw_5_cdec_7Lattice_3__getitem__, /*mp_subscript*/ + __pyx_mp_ass_subscript_5_cdec_Lattice, /*mp_ass_subscript*/ }; static PyBufferProcs __pyx_tp_as_buffer_Lattice = { @@ -3979,7 +6562,7 @@ static PyTypeObject __pyx_type_5_cdec_Lattice = { &__pyx_tp_as_mapping_Lattice, /*tp_as_mapping*/ 0, /*tp_hash*/ 0, /*tp_call*/ - __pyx_pw_5_cdec_7Lattice_3__str__, /*tp_str*/ + __pyx_pw_5_cdec_7Lattice_9__str__, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ &__pyx_tp_as_buffer_Lattice, /*tp_as_buffer*/ @@ -3989,7 +6572,7 @@ static PyTypeObject __pyx_type_5_cdec_Lattice = { 0, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ - __pyx_pw_5_cdec_7Lattice_5__iter__, /*tp_iter*/ + __pyx_pw_5_cdec_7Lattice_11__iter__, /*tp_iter*/ 0, /*tp_iternext*/ __pyx_methods_5_cdec_Lattice, /*tp_methods*/ 0, /*tp_members*/ @@ -4015,6 +6598,229 @@ static PyTypeObject __pyx_type_5_cdec_Lattice = { #endif }; +static PyObject *__pyx_tp_new_5_cdec_Decoder(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_5_cdec_Decoder *p; + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + p = ((struct __pyx_obj_5_cdec_Decoder *)o); + p->weights = ((struct __pyx_obj_5_cdec_DenseVector *)Py_None); Py_INCREF(Py_None); + if (__pyx_pw_5_cdec_7Decoder_1__cinit__(o, a, k) < 0) { + Py_DECREF(o); o = 0; + } + return o; +} + +static void __pyx_tp_dealloc_5_cdec_Decoder(PyObject *o) { + struct __pyx_obj_5_cdec_Decoder *p = (struct __pyx_obj_5_cdec_Decoder *)o; + { + PyObject *etype, *eval, *etb; + PyErr_Fetch(&etype, &eval, &etb); + ++Py_REFCNT(o); + __pyx_pw_5_cdec_7Decoder_3__dealloc__(o); + if (PyErr_Occurred()) PyErr_WriteUnraisable(o); + --Py_REFCNT(o); + PyErr_Restore(etype, eval, etb); + } + Py_XDECREF(((PyObject *)p->weights)); + (*Py_TYPE(o)->tp_free)(o); +} + +static int __pyx_tp_traverse_5_cdec_Decoder(PyObject *o, visitproc v, void *a) { + int e; + struct __pyx_obj_5_cdec_Decoder *p = (struct __pyx_obj_5_cdec_Decoder *)o; + if (p->weights) { + e = (*v)(((PyObject*)p->weights), a); if (e) return e; + } + return 0; +} + +static int __pyx_tp_clear_5_cdec_Decoder(PyObject *o) { + struct __pyx_obj_5_cdec_Decoder *p = (struct __pyx_obj_5_cdec_Decoder *)o; + PyObject* tmp; + tmp = ((PyObject*)p->weights); + p->weights = ((struct __pyx_obj_5_cdec_DenseVector *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + return 0; +} + +static PyObject *__pyx_getprop_5_cdec_7Decoder_weights(PyObject *o, void *x) { + return __pyx_pw_5_cdec_7Decoder_7weights_1__get__(o); +} + +static int __pyx_setprop_5_cdec_7Decoder_weights(PyObject *o, PyObject *v, void *x) { + if (v) { + return __pyx_pw_5_cdec_7Decoder_7weights_3__set__(o, v); + } + else { + return __pyx_pw_5_cdec_7Decoder_7weights_5__del__(o); + } +} + +static PyMethodDef __pyx_methods_5_cdec_Decoder[] = { + {__Pyx_NAMESTR("read_weights"), (PyCFunction)__pyx_pw_5_cdec_7Decoder_5read_weights, METH_O, __Pyx_DOCSTR(0)}, + {__Pyx_NAMESTR("translate"), (PyCFunction)__pyx_pw_5_cdec_7Decoder_7translate, METH_VARARGS|METH_KEYWORDS, __Pyx_DOCSTR(0)}, + {0, 0, 0, 0} +}; + +static struct PyGetSetDef __pyx_getsets_5_cdec_Decoder[] = { + {(char *)"weights", __pyx_getprop_5_cdec_7Decoder_weights, __pyx_setprop_5_cdec_7Decoder_weights, 0, 0}, + {0, 0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number_Decoder = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence_Decoder = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping_Decoder = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer_Decoder = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +static PyTypeObject __pyx_type_5_cdec_Decoder = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("_cdec.Decoder"), /*tp_name*/ + sizeof(struct __pyx_obj_5_cdec_Decoder), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_5_cdec_Decoder, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + #if PY_MAJOR_VERSION < 3 + 0, /*tp_compare*/ + #else + 0, /*reserved*/ + #endif + 0, /*tp_repr*/ + &__pyx_tp_as_number_Decoder, /*tp_as_number*/ + &__pyx_tp_as_sequence_Decoder, /*tp_as_sequence*/ + &__pyx_tp_as_mapping_Decoder, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer_Decoder, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_BASETYPE|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + 0, /*tp_doc*/ + __pyx_tp_traverse_5_cdec_Decoder, /*tp_traverse*/ + __pyx_tp_clear_5_cdec_Decoder, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_5_cdec_Decoder, /*tp_methods*/ + 0, /*tp_members*/ + __pyx_getsets_5_cdec_Decoder, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + 0, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_5_cdec_Decoder, /*tp_new*/ + 0, /*tp_free*/ + 0, /*tp_is_gc*/ + 0, /*tp_bases*/ + 0, /*tp_mro*/ + 0, /*tp_cache*/ + 0, /*tp_subclasses*/ + 0, /*tp_weaklist*/ + 0, /*tp_del*/ + #if PY_VERSION_HEX >= 0x02060000 + 0, /*tp_version_tag*/ + #endif +}; + static PyObject *__pyx_tp_new_5_cdec___pyx_scope_struct____iter__(PyTypeObject *t, PyObject *a, PyObject *k) { struct __pyx_obj_5_cdec___pyx_scope_struct____iter__ *p; PyObject *o = (*t->tp_alloc)(t, 0); @@ -4043,7 +6849,7 @@ static int __pyx_tp_clear_5_cdec___pyx_scope_struct____iter__(PyObject *o) { struct __pyx_obj_5_cdec___pyx_scope_struct____iter__ *p = (struct __pyx_obj_5_cdec___pyx_scope_struct____iter__ *)o; PyObject* tmp; tmp = ((PyObject*)p->__pyx_v_self); - p->__pyx_v_self = ((struct __pyx_obj_5_cdec_Weights *)Py_None); Py_INCREF(Py_None); + p->__pyx_v_self = ((struct __pyx_obj_5_cdec_DenseVector *)Py_None); Py_INCREF(Py_None); Py_XDECREF(tmp); return 0; } @@ -4206,60 +7012,259 @@ static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct____iter__ = { #endif }; -static PyObject *__pyx_tp_new_5_cdec___pyx_scope_struct_1_kbest(PyTypeObject *t, PyObject *a, PyObject *k) { - struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *p; +static PyObject *__pyx_tp_new_5_cdec___pyx_scope_struct_1___iter__(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *p; PyObject *o = (*t->tp_alloc)(t, 0); if (!o) return 0; - p = ((struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *)o); + p = ((struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *)o); + p->__pyx_v_fname = 0; p->__pyx_v_self = 0; + return o; +} + +static void __pyx_tp_dealloc_5_cdec___pyx_scope_struct_1___iter__(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *)o; + Py_XDECREF(((PyObject *)p->__pyx_v_fname)); + Py_XDECREF(((PyObject *)p->__pyx_v_self)); + (*Py_TYPE(o)->tp_free)(o); +} + +static int __pyx_tp_traverse_5_cdec___pyx_scope_struct_1___iter__(PyObject *o, visitproc v, void *a) { + int e; + struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *)o; + if (p->__pyx_v_fname) { + e = (*v)(p->__pyx_v_fname, a); if (e) return e; + } + if (p->__pyx_v_self) { + e = (*v)(((PyObject*)p->__pyx_v_self), a); if (e) return e; + } + return 0; +} + +static int __pyx_tp_clear_5_cdec___pyx_scope_struct_1___iter__(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__ *)o; + PyObject* tmp; + tmp = ((PyObject*)p->__pyx_v_fname); + p->__pyx_v_fname = ((PyObject*)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + tmp = ((PyObject*)p->__pyx_v_self); + p->__pyx_v_self = ((struct __pyx_obj_5_cdec_SparseVector *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + return 0; +} + +static PyMethodDef __pyx_methods_5_cdec___pyx_scope_struct_1___iter__[] = { + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_1___iter__ = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_1___iter__ = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping___pyx_scope_struct_1___iter__ = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_1___iter__ = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_1___iter__ = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("_cdec.__pyx_scope_struct_1___iter__"), /*tp_name*/ + sizeof(struct __pyx_obj_5_cdec___pyx_scope_struct_1___iter__), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_5_cdec___pyx_scope_struct_1___iter__, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + #if PY_MAJOR_VERSION < 3 + 0, /*tp_compare*/ + #else + 0, /*reserved*/ + #endif + 0, /*tp_repr*/ + &__pyx_tp_as_number___pyx_scope_struct_1___iter__, /*tp_as_number*/ + &__pyx_tp_as_sequence___pyx_scope_struct_1___iter__, /*tp_as_sequence*/ + &__pyx_tp_as_mapping___pyx_scope_struct_1___iter__, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer___pyx_scope_struct_1___iter__, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + 0, /*tp_doc*/ + __pyx_tp_traverse_5_cdec___pyx_scope_struct_1___iter__, /*tp_traverse*/ + __pyx_tp_clear_5_cdec___pyx_scope_struct_1___iter__, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_5_cdec___pyx_scope_struct_1___iter__, /*tp_methods*/ + 0, /*tp_members*/ + 0, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + 0, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_5_cdec___pyx_scope_struct_1___iter__, /*tp_new*/ + 0, /*tp_free*/ + 0, /*tp_is_gc*/ + 0, /*tp_bases*/ + 0, /*tp_mro*/ + 0, /*tp_cache*/ + 0, /*tp_subclasses*/ + 0, /*tp_weaklist*/ + 0, /*tp_del*/ + #if PY_VERSION_HEX >= 0x02060000 + 0, /*tp_version_tag*/ + #endif +}; + +static PyObject *__pyx_tp_new_5_cdec___pyx_scope_struct_2_kbest(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *p; + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + p = ((struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *)o); + p->__pyx_v_self = 0; + p->__pyx_v_sentence = 0; p->__pyx_v_size = 0; - p->__pyx_v_tree = 0; return o; } -static void __pyx_tp_dealloc_5_cdec___pyx_scope_struct_1_kbest(PyObject *o) { - struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *)o; +static void __pyx_tp_dealloc_5_cdec___pyx_scope_struct_2_kbest(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *)o; Py_XDECREF(((PyObject *)p->__pyx_v_self)); + Py_XDECREF(((PyObject *)p->__pyx_v_sentence)); Py_XDECREF(p->__pyx_v_size); - Py_XDECREF(((PyObject *)p->__pyx_v_tree)); (*Py_TYPE(o)->tp_free)(o); } -static int __pyx_tp_traverse_5_cdec___pyx_scope_struct_1_kbest(PyObject *o, visitproc v, void *a) { +static int __pyx_tp_traverse_5_cdec___pyx_scope_struct_2_kbest(PyObject *o, visitproc v, void *a) { int e; - struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *)o; + struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *)o; if (p->__pyx_v_self) { e = (*v)(((PyObject*)p->__pyx_v_self), a); if (e) return e; } + if (p->__pyx_v_sentence) { + e = (*v)(p->__pyx_v_sentence, a); if (e) return e; + } if (p->__pyx_v_size) { e = (*v)(p->__pyx_v_size, a); if (e) return e; } - if (p->__pyx_v_tree) { - e = (*v)(p->__pyx_v_tree, a); if (e) return e; - } return 0; } -static int __pyx_tp_clear_5_cdec___pyx_scope_struct_1_kbest(PyObject *o) { - struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest *)o; +static int __pyx_tp_clear_5_cdec___pyx_scope_struct_2_kbest(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest *)o; PyObject* tmp; tmp = ((PyObject*)p->__pyx_v_self); p->__pyx_v_self = ((struct __pyx_obj_5_cdec_Hypergraph *)Py_None); Py_INCREF(Py_None); Py_XDECREF(tmp); + tmp = ((PyObject*)p->__pyx_v_sentence); + p->__pyx_v_sentence = ((PyObject*)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); tmp = ((PyObject*)p->__pyx_v_size); p->__pyx_v_size = Py_None; Py_INCREF(Py_None); Py_XDECREF(tmp); - tmp = ((PyObject*)p->__pyx_v_tree); - p->__pyx_v_tree = ((PyObject*)Py_None); Py_INCREF(Py_None); - Py_XDECREF(tmp); return 0; } -static PyMethodDef __pyx_methods_5_cdec___pyx_scope_struct_1_kbest[] = { +static PyMethodDef __pyx_methods_5_cdec___pyx_scope_struct_2_kbest[] = { {0, 0, 0, 0} }; -static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_1_kbest = { +static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_2_kbest = { 0, /*nb_add*/ 0, /*nb_subtract*/ 0, /*nb_multiply*/ @@ -4317,7 +7322,7 @@ static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_1_kbest = { #endif }; -static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_1_kbest = { +static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_2_kbest = { 0, /*sq_length*/ 0, /*sq_concat*/ 0, /*sq_repeat*/ @@ -4330,13 +7335,13 @@ static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_1_kbest = { 0, /*sq_inplace_repeat*/ }; -static PyMappingMethods __pyx_tp_as_mapping___pyx_scope_struct_1_kbest = { +static PyMappingMethods __pyx_tp_as_mapping___pyx_scope_struct_2_kbest = { 0, /*mp_length*/ 0, /*mp_subscript*/ 0, /*mp_ass_subscript*/ }; -static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_1_kbest = { +static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_2_kbest = { #if PY_MAJOR_VERSION < 3 0, /*bf_getreadbuffer*/ #endif @@ -4357,12 +7362,12 @@ static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_1_kbest = { #endif }; -static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_1_kbest = { +static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_2_kbest = { PyVarObject_HEAD_INIT(0, 0) - __Pyx_NAMESTR("_cdec.__pyx_scope_struct_1_kbest"), /*tp_name*/ - sizeof(struct __pyx_obj_5_cdec___pyx_scope_struct_1_kbest), /*tp_basicsize*/ + __Pyx_NAMESTR("_cdec.__pyx_scope_struct_2_kbest"), /*tp_name*/ + sizeof(struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest), /*tp_basicsize*/ 0, /*tp_itemsize*/ - __pyx_tp_dealloc_5_cdec___pyx_scope_struct_1_kbest, /*tp_dealloc*/ + __pyx_tp_dealloc_5_cdec___pyx_scope_struct_2_kbest, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -4372,24 +7377,24 @@ static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_1_kbest = { 0, /*reserved*/ #endif 0, /*tp_repr*/ - &__pyx_tp_as_number___pyx_scope_struct_1_kbest, /*tp_as_number*/ - &__pyx_tp_as_sequence___pyx_scope_struct_1_kbest, /*tp_as_sequence*/ - &__pyx_tp_as_mapping___pyx_scope_struct_1_kbest, /*tp_as_mapping*/ + &__pyx_tp_as_number___pyx_scope_struct_2_kbest, /*tp_as_number*/ + &__pyx_tp_as_sequence___pyx_scope_struct_2_kbest, /*tp_as_sequence*/ + &__pyx_tp_as_mapping___pyx_scope_struct_2_kbest, /*tp_as_mapping*/ 0, /*tp_hash*/ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ - &__pyx_tp_as_buffer___pyx_scope_struct_1_kbest, /*tp_as_buffer*/ + &__pyx_tp_as_buffer___pyx_scope_struct_2_kbest, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ 0, /*tp_doc*/ - __pyx_tp_traverse_5_cdec___pyx_scope_struct_1_kbest, /*tp_traverse*/ - __pyx_tp_clear_5_cdec___pyx_scope_struct_1_kbest, /*tp_clear*/ + __pyx_tp_traverse_5_cdec___pyx_scope_struct_2_kbest, /*tp_traverse*/ + __pyx_tp_clear_5_cdec___pyx_scope_struct_2_kbest, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ 0, /*tp_iter*/ 0, /*tp_iternext*/ - __pyx_methods_5_cdec___pyx_scope_struct_1_kbest, /*tp_methods*/ + __pyx_methods_5_cdec___pyx_scope_struct_2_kbest, /*tp_methods*/ 0, /*tp_members*/ 0, /*tp_getset*/ 0, /*tp_base*/ @@ -4399,7 +7404,7 @@ static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_1_kbest = { 0, /*tp_dictoffset*/ 0, /*tp_init*/ 0, /*tp_alloc*/ - __pyx_tp_new_5_cdec___pyx_scope_struct_1_kbest, /*tp_new*/ + __pyx_tp_new_5_cdec___pyx_scope_struct_2_kbest, /*tp_new*/ 0, /*tp_free*/ 0, /*tp_is_gc*/ 0, /*tp_bases*/ @@ -4413,60 +7418,60 @@ static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_1_kbest = { #endif }; -static PyObject *__pyx_tp_new_5_cdec___pyx_scope_struct_2_kbest_tree(PyTypeObject *t, PyObject *a, PyObject *k) { - struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *p; +static PyObject *__pyx_tp_new_5_cdec___pyx_scope_struct_3_kbest_tree(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *p; PyObject *o = (*t->tp_alloc)(t, 0); if (!o) return 0; - p = ((struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *)o); + p = ((struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *)o); p->__pyx_v_self = 0; - p->__pyx_v_sentence = 0; p->__pyx_v_size = 0; + p->__pyx_v_tree = 0; return o; } -static void __pyx_tp_dealloc_5_cdec___pyx_scope_struct_2_kbest_tree(PyObject *o) { - struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *)o; +static void __pyx_tp_dealloc_5_cdec___pyx_scope_struct_3_kbest_tree(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *)o; Py_XDECREF(((PyObject *)p->__pyx_v_self)); - Py_XDECREF(((PyObject *)p->__pyx_v_sentence)); Py_XDECREF(p->__pyx_v_size); + Py_XDECREF(((PyObject *)p->__pyx_v_tree)); (*Py_TYPE(o)->tp_free)(o); } -static int __pyx_tp_traverse_5_cdec___pyx_scope_struct_2_kbest_tree(PyObject *o, visitproc v, void *a) { +static int __pyx_tp_traverse_5_cdec___pyx_scope_struct_3_kbest_tree(PyObject *o, visitproc v, void *a) { int e; - struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *)o; + struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *)o; if (p->__pyx_v_self) { e = (*v)(((PyObject*)p->__pyx_v_self), a); if (e) return e; } - if (p->__pyx_v_sentence) { - e = (*v)(p->__pyx_v_sentence, a); if (e) return e; - } if (p->__pyx_v_size) { e = (*v)(p->__pyx_v_size, a); if (e) return e; } + if (p->__pyx_v_tree) { + e = (*v)(p->__pyx_v_tree, a); if (e) return e; + } return 0; } -static int __pyx_tp_clear_5_cdec___pyx_scope_struct_2_kbest_tree(PyObject *o) { - struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree *)o; +static int __pyx_tp_clear_5_cdec___pyx_scope_struct_3_kbest_tree(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree *)o; PyObject* tmp; tmp = ((PyObject*)p->__pyx_v_self); p->__pyx_v_self = ((struct __pyx_obj_5_cdec_Hypergraph *)Py_None); Py_INCREF(Py_None); Py_XDECREF(tmp); - tmp = ((PyObject*)p->__pyx_v_sentence); - p->__pyx_v_sentence = ((PyObject*)Py_None); Py_INCREF(Py_None); - Py_XDECREF(tmp); tmp = ((PyObject*)p->__pyx_v_size); p->__pyx_v_size = Py_None; Py_INCREF(Py_None); Py_XDECREF(tmp); + tmp = ((PyObject*)p->__pyx_v_tree); + p->__pyx_v_tree = ((PyObject*)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); return 0; } -static PyMethodDef __pyx_methods_5_cdec___pyx_scope_struct_2_kbest_tree[] = { +static PyMethodDef __pyx_methods_5_cdec___pyx_scope_struct_3_kbest_tree[] = { {0, 0, 0, 0} }; -static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_2_kbest_tree = { +static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_3_kbest_tree = { 0, /*nb_add*/ 0, /*nb_subtract*/ 0, /*nb_multiply*/ @@ -4524,7 +7529,7 @@ static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_2_kbest_tree = { #endif }; -static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_2_kbest_tree = { +static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_3_kbest_tree = { 0, /*sq_length*/ 0, /*sq_concat*/ 0, /*sq_repeat*/ @@ -4537,13 +7542,13 @@ static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_2_kbest_tree = 0, /*sq_inplace_repeat*/ }; -static PyMappingMethods __pyx_tp_as_mapping___pyx_scope_struct_2_kbest_tree = { +static PyMappingMethods __pyx_tp_as_mapping___pyx_scope_struct_3_kbest_tree = { 0, /*mp_length*/ 0, /*mp_subscript*/ 0, /*mp_ass_subscript*/ }; -static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_2_kbest_tree = { +static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_3_kbest_tree = { #if PY_MAJOR_VERSION < 3 0, /*bf_getreadbuffer*/ #endif @@ -4564,12 +7569,12 @@ static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_2_kbest_tree = { #endif }; -static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_2_kbest_tree = { +static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_3_kbest_tree = { PyVarObject_HEAD_INIT(0, 0) - __Pyx_NAMESTR("_cdec.__pyx_scope_struct_2_kbest_tree"), /*tp_name*/ - sizeof(struct __pyx_obj_5_cdec___pyx_scope_struct_2_kbest_tree), /*tp_basicsize*/ + __Pyx_NAMESTR("_cdec.__pyx_scope_struct_3_kbest_tree"), /*tp_name*/ + sizeof(struct __pyx_obj_5_cdec___pyx_scope_struct_3_kbest_tree), /*tp_basicsize*/ 0, /*tp_itemsize*/ - __pyx_tp_dealloc_5_cdec___pyx_scope_struct_2_kbest_tree, /*tp_dealloc*/ + __pyx_tp_dealloc_5_cdec___pyx_scope_struct_3_kbest_tree, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -4579,24 +7584,24 @@ static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_2_kbest_tree = { 0, /*reserved*/ #endif 0, /*tp_repr*/ - &__pyx_tp_as_number___pyx_scope_struct_2_kbest_tree, /*tp_as_number*/ - &__pyx_tp_as_sequence___pyx_scope_struct_2_kbest_tree, /*tp_as_sequence*/ - &__pyx_tp_as_mapping___pyx_scope_struct_2_kbest_tree, /*tp_as_mapping*/ + &__pyx_tp_as_number___pyx_scope_struct_3_kbest_tree, /*tp_as_number*/ + &__pyx_tp_as_sequence___pyx_scope_struct_3_kbest_tree, /*tp_as_sequence*/ + &__pyx_tp_as_mapping___pyx_scope_struct_3_kbest_tree, /*tp_as_mapping*/ 0, /*tp_hash*/ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ - &__pyx_tp_as_buffer___pyx_scope_struct_2_kbest_tree, /*tp_as_buffer*/ + &__pyx_tp_as_buffer___pyx_scope_struct_3_kbest_tree, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ 0, /*tp_doc*/ - __pyx_tp_traverse_5_cdec___pyx_scope_struct_2_kbest_tree, /*tp_traverse*/ - __pyx_tp_clear_5_cdec___pyx_scope_struct_2_kbest_tree, /*tp_clear*/ + __pyx_tp_traverse_5_cdec___pyx_scope_struct_3_kbest_tree, /*tp_traverse*/ + __pyx_tp_clear_5_cdec___pyx_scope_struct_3_kbest_tree, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ 0, /*tp_iter*/ 0, /*tp_iternext*/ - __pyx_methods_5_cdec___pyx_scope_struct_2_kbest_tree, /*tp_methods*/ + __pyx_methods_5_cdec___pyx_scope_struct_3_kbest_tree, /*tp_methods*/ 0, /*tp_members*/ 0, /*tp_getset*/ 0, /*tp_base*/ @@ -4606,7 +7611,7 @@ static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_2_kbest_tree = { 0, /*tp_dictoffset*/ 0, /*tp_init*/ 0, /*tp_alloc*/ - __pyx_tp_new_5_cdec___pyx_scope_struct_2_kbest_tree, /*tp_new*/ + __pyx_tp_new_5_cdec___pyx_scope_struct_3_kbest_tree, /*tp_new*/ 0, /*tp_free*/ 0, /*tp_is_gc*/ 0, /*tp_bases*/ @@ -4620,26 +7625,26 @@ static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_2_kbest_tree = { #endif }; -static PyObject *__pyx_tp_new_5_cdec___pyx_scope_struct_3_sample(PyTypeObject *t, PyObject *a, PyObject *k) { - struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *p; +static PyObject *__pyx_tp_new_5_cdec___pyx_scope_struct_4_sample(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *p; PyObject *o = (*t->tp_alloc)(t, 0); if (!o) return 0; - p = ((struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *)o); + p = ((struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *)o); p->__pyx_v_self = 0; p->__pyx_v_sentence = 0; return o; } -static void __pyx_tp_dealloc_5_cdec___pyx_scope_struct_3_sample(PyObject *o) { - struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *)o; +static void __pyx_tp_dealloc_5_cdec___pyx_scope_struct_4_sample(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *)o; Py_XDECREF(((PyObject *)p->__pyx_v_self)); Py_XDECREF(((PyObject *)p->__pyx_v_sentence)); (*Py_TYPE(o)->tp_free)(o); } -static int __pyx_tp_traverse_5_cdec___pyx_scope_struct_3_sample(PyObject *o, visitproc v, void *a) { +static int __pyx_tp_traverse_5_cdec___pyx_scope_struct_4_sample(PyObject *o, visitproc v, void *a) { int e; - struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *)o; + struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *)o; if (p->__pyx_v_self) { e = (*v)(((PyObject*)p->__pyx_v_self), a); if (e) return e; } @@ -4649,8 +7654,8 @@ static int __pyx_tp_traverse_5_cdec___pyx_scope_struct_3_sample(PyObject *o, vis return 0; } -static int __pyx_tp_clear_5_cdec___pyx_scope_struct_3_sample(PyObject *o) { - struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample *)o; +static int __pyx_tp_clear_5_cdec___pyx_scope_struct_4_sample(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample *)o; PyObject* tmp; tmp = ((PyObject*)p->__pyx_v_self); p->__pyx_v_self = ((struct __pyx_obj_5_cdec_Hypergraph *)Py_None); Py_INCREF(Py_None); @@ -4661,11 +7666,11 @@ static int __pyx_tp_clear_5_cdec___pyx_scope_struct_3_sample(PyObject *o) { return 0; } -static PyMethodDef __pyx_methods_5_cdec___pyx_scope_struct_3_sample[] = { +static PyMethodDef __pyx_methods_5_cdec___pyx_scope_struct_4_sample[] = { {0, 0, 0, 0} }; -static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_3_sample = { +static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_4_sample = { 0, /*nb_add*/ 0, /*nb_subtract*/ 0, /*nb_multiply*/ @@ -4723,7 +7728,7 @@ static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_3_sample = { #endif }; -static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_3_sample = { +static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_4_sample = { 0, /*sq_length*/ 0, /*sq_concat*/ 0, /*sq_repeat*/ @@ -4736,13 +7741,13 @@ static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_3_sample = { 0, /*sq_inplace_repeat*/ }; -static PyMappingMethods __pyx_tp_as_mapping___pyx_scope_struct_3_sample = { +static PyMappingMethods __pyx_tp_as_mapping___pyx_scope_struct_4_sample = { 0, /*mp_length*/ 0, /*mp_subscript*/ 0, /*mp_ass_subscript*/ }; -static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_3_sample = { +static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_4_sample = { #if PY_MAJOR_VERSION < 3 0, /*bf_getreadbuffer*/ #endif @@ -4763,12 +7768,12 @@ static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_3_sample = { #endif }; -static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_3_sample = { +static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_4_sample = { PyVarObject_HEAD_INIT(0, 0) - __Pyx_NAMESTR("_cdec.__pyx_scope_struct_3_sample"), /*tp_name*/ - sizeof(struct __pyx_obj_5_cdec___pyx_scope_struct_3_sample), /*tp_basicsize*/ + __Pyx_NAMESTR("_cdec.__pyx_scope_struct_4_sample"), /*tp_name*/ + sizeof(struct __pyx_obj_5_cdec___pyx_scope_struct_4_sample), /*tp_basicsize*/ 0, /*tp_itemsize*/ - __pyx_tp_dealloc_5_cdec___pyx_scope_struct_3_sample, /*tp_dealloc*/ + __pyx_tp_dealloc_5_cdec___pyx_scope_struct_4_sample, /*tp_dealloc*/ 0, /*tp_print*/ 0, /*tp_getattr*/ 0, /*tp_setattr*/ @@ -4778,24 +7783,24 @@ static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_3_sample = { 0, /*reserved*/ #endif 0, /*tp_repr*/ - &__pyx_tp_as_number___pyx_scope_struct_3_sample, /*tp_as_number*/ - &__pyx_tp_as_sequence___pyx_scope_struct_3_sample, /*tp_as_sequence*/ - &__pyx_tp_as_mapping___pyx_scope_struct_3_sample, /*tp_as_mapping*/ + &__pyx_tp_as_number___pyx_scope_struct_4_sample, /*tp_as_number*/ + &__pyx_tp_as_sequence___pyx_scope_struct_4_sample, /*tp_as_sequence*/ + &__pyx_tp_as_mapping___pyx_scope_struct_4_sample, /*tp_as_mapping*/ 0, /*tp_hash*/ 0, /*tp_call*/ 0, /*tp_str*/ 0, /*tp_getattro*/ 0, /*tp_setattro*/ - &__pyx_tp_as_buffer___pyx_scope_struct_3_sample, /*tp_as_buffer*/ + &__pyx_tp_as_buffer___pyx_scope_struct_4_sample, /*tp_as_buffer*/ Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ 0, /*tp_doc*/ - __pyx_tp_traverse_5_cdec___pyx_scope_struct_3_sample, /*tp_traverse*/ - __pyx_tp_clear_5_cdec___pyx_scope_struct_3_sample, /*tp_clear*/ + __pyx_tp_traverse_5_cdec___pyx_scope_struct_4_sample, /*tp_traverse*/ + __pyx_tp_clear_5_cdec___pyx_scope_struct_4_sample, /*tp_clear*/ 0, /*tp_richcompare*/ 0, /*tp_weaklistoffset*/ 0, /*tp_iter*/ 0, /*tp_iternext*/ - __pyx_methods_5_cdec___pyx_scope_struct_3_sample, /*tp_methods*/ + __pyx_methods_5_cdec___pyx_scope_struct_4_sample, /*tp_methods*/ 0, /*tp_members*/ 0, /*tp_getset*/ 0, /*tp_base*/ @@ -4805,7 +7810,198 @@ static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_3_sample = { 0, /*tp_dictoffset*/ 0, /*tp_init*/ 0, /*tp_alloc*/ - __pyx_tp_new_5_cdec___pyx_scope_struct_3_sample, /*tp_new*/ + __pyx_tp_new_5_cdec___pyx_scope_struct_4_sample, /*tp_new*/ + 0, /*tp_free*/ + 0, /*tp_is_gc*/ + 0, /*tp_bases*/ + 0, /*tp_mro*/ + 0, /*tp_cache*/ + 0, /*tp_subclasses*/ + 0, /*tp_weaklist*/ + 0, /*tp_del*/ + #if PY_VERSION_HEX >= 0x02060000 + 0, /*tp_version_tag*/ + #endif +}; + +static PyObject *__pyx_tp_new_5_cdec___pyx_scope_struct_5___iter__(PyTypeObject *t, PyObject *a, PyObject *k) { + struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *p; + PyObject *o = (*t->tp_alloc)(t, 0); + if (!o) return 0; + p = ((struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *)o); + p->__pyx_v_self = 0; + return o; +} + +static void __pyx_tp_dealloc_5_cdec___pyx_scope_struct_5___iter__(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *)o; + Py_XDECREF(((PyObject *)p->__pyx_v_self)); + (*Py_TYPE(o)->tp_free)(o); +} + +static int __pyx_tp_traverse_5_cdec___pyx_scope_struct_5___iter__(PyObject *o, visitproc v, void *a) { + int e; + struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *)o; + if (p->__pyx_v_self) { + e = (*v)(((PyObject*)p->__pyx_v_self), a); if (e) return e; + } + return 0; +} + +static int __pyx_tp_clear_5_cdec___pyx_scope_struct_5___iter__(PyObject *o) { + struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *p = (struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__ *)o; + PyObject* tmp; + tmp = ((PyObject*)p->__pyx_v_self); + p->__pyx_v_self = ((struct __pyx_obj_5_cdec_Lattice *)Py_None); Py_INCREF(Py_None); + Py_XDECREF(tmp); + return 0; +} + +static PyMethodDef __pyx_methods_5_cdec___pyx_scope_struct_5___iter__[] = { + {0, 0, 0, 0} +}; + +static PyNumberMethods __pyx_tp_as_number___pyx_scope_struct_5___iter__ = { + 0, /*nb_add*/ + 0, /*nb_subtract*/ + 0, /*nb_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_divide*/ + #endif + 0, /*nb_remainder*/ + 0, /*nb_divmod*/ + 0, /*nb_power*/ + 0, /*nb_negative*/ + 0, /*nb_positive*/ + 0, /*nb_absolute*/ + 0, /*nb_nonzero*/ + 0, /*nb_invert*/ + 0, /*nb_lshift*/ + 0, /*nb_rshift*/ + 0, /*nb_and*/ + 0, /*nb_xor*/ + 0, /*nb_or*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_coerce*/ + #endif + 0, /*nb_int*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_long*/ + #else + 0, /*reserved*/ + #endif + 0, /*nb_float*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_oct*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*nb_hex*/ + #endif + 0, /*nb_inplace_add*/ + 0, /*nb_inplace_subtract*/ + 0, /*nb_inplace_multiply*/ + #if PY_MAJOR_VERSION < 3 + 0, /*nb_inplace_divide*/ + #endif + 0, /*nb_inplace_remainder*/ + 0, /*nb_inplace_power*/ + 0, /*nb_inplace_lshift*/ + 0, /*nb_inplace_rshift*/ + 0, /*nb_inplace_and*/ + 0, /*nb_inplace_xor*/ + 0, /*nb_inplace_or*/ + 0, /*nb_floor_divide*/ + 0, /*nb_true_divide*/ + 0, /*nb_inplace_floor_divide*/ + 0, /*nb_inplace_true_divide*/ + #if PY_VERSION_HEX >= 0x02050000 + 0, /*nb_index*/ + #endif +}; + +static PySequenceMethods __pyx_tp_as_sequence___pyx_scope_struct_5___iter__ = { + 0, /*sq_length*/ + 0, /*sq_concat*/ + 0, /*sq_repeat*/ + 0, /*sq_item*/ + 0, /*sq_slice*/ + 0, /*sq_ass_item*/ + 0, /*sq_ass_slice*/ + 0, /*sq_contains*/ + 0, /*sq_inplace_concat*/ + 0, /*sq_inplace_repeat*/ +}; + +static PyMappingMethods __pyx_tp_as_mapping___pyx_scope_struct_5___iter__ = { + 0, /*mp_length*/ + 0, /*mp_subscript*/ + 0, /*mp_ass_subscript*/ +}; + +static PyBufferProcs __pyx_tp_as_buffer___pyx_scope_struct_5___iter__ = { + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getreadbuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getwritebuffer*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getsegcount*/ + #endif + #if PY_MAJOR_VERSION < 3 + 0, /*bf_getcharbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_getbuffer*/ + #endif + #if PY_VERSION_HEX >= 0x02060000 + 0, /*bf_releasebuffer*/ + #endif +}; + +static PyTypeObject __pyx_type_5_cdec___pyx_scope_struct_5___iter__ = { + PyVarObject_HEAD_INIT(0, 0) + __Pyx_NAMESTR("_cdec.__pyx_scope_struct_5___iter__"), /*tp_name*/ + sizeof(struct __pyx_obj_5_cdec___pyx_scope_struct_5___iter__), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + __pyx_tp_dealloc_5_cdec___pyx_scope_struct_5___iter__, /*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + #if PY_MAJOR_VERSION < 3 + 0, /*tp_compare*/ + #else + 0, /*reserved*/ + #endif + 0, /*tp_repr*/ + &__pyx_tp_as_number___pyx_scope_struct_5___iter__, /*tp_as_number*/ + &__pyx_tp_as_sequence___pyx_scope_struct_5___iter__, /*tp_as_sequence*/ + &__pyx_tp_as_mapping___pyx_scope_struct_5___iter__, /*tp_as_mapping*/ + 0, /*tp_hash*/ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + &__pyx_tp_as_buffer___pyx_scope_struct_5___iter__, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT|Py_TPFLAGS_CHECKTYPES|Py_TPFLAGS_HAVE_NEWBUFFER|Py_TPFLAGS_HAVE_GC, /*tp_flags*/ + 0, /*tp_doc*/ + __pyx_tp_traverse_5_cdec___pyx_scope_struct_5___iter__, /*tp_traverse*/ + __pyx_tp_clear_5_cdec___pyx_scope_struct_5___iter__, /*tp_clear*/ + 0, /*tp_richcompare*/ + 0, /*tp_weaklistoffset*/ + 0, /*tp_iter*/ + 0, /*tp_iternext*/ + __pyx_methods_5_cdec___pyx_scope_struct_5___iter__, /*tp_methods*/ + 0, /*tp_members*/ + 0, /*tp_getset*/ + 0, /*tp_base*/ + 0, /*tp_dict*/ + 0, /*tp_descr_get*/ + 0, /*tp_descr_set*/ + 0, /*tp_dictoffset*/ + 0, /*tp_init*/ + 0, /*tp_alloc*/ + __pyx_tp_new_5_cdec___pyx_scope_struct_5___iter__, /*tp_new*/ 0, /*tp_free*/ 0, /*tp_is_gc*/ 0, /*tp_bases*/ @@ -4838,22 +8034,37 @@ static struct PyModuleDef __pyx_moduledef = { #endif static __Pyx_StringTabEntry __pyx_string_tab[] = { + {&__pyx_kp_s_1, __pyx_k_1, sizeof(__pyx_k_1), 0, 0, 1, 0}, + {&__pyx_n_s_10, __pyx_k_10, sizeof(__pyx_k_10), 0, 0, 1, 1}, + {&__pyx_kp_s_11, __pyx_k_11, sizeof(__pyx_k_11), 0, 0, 1, 0}, + {&__pyx_kp_s_13, __pyx_k_13, sizeof(__pyx_k_13), 0, 0, 1, 0}, + {&__pyx_kp_s_14, __pyx_k_14, sizeof(__pyx_k_14), 0, 0, 1, 0}, + {&__pyx_kp_s_2, __pyx_k_2, sizeof(__pyx_k_2), 0, 0, 1, 0}, + {&__pyx_kp_s_21, __pyx_k_21, sizeof(__pyx_k_21), 0, 0, 1, 0}, {&__pyx_n_s__Exception, __pyx_k__Exception, sizeof(__pyx_k__Exception), 0, 0, 1, 1}, + {&__pyx_n_s__IndexError, __pyx_k__IndexError, sizeof(__pyx_k__IndexError), 0, 0, 1, 1}, {&__pyx_n_s__KeyError, __pyx_k__KeyError, sizeof(__pyx_k__KeyError), 0, 0, 1, 1}, + {&__pyx_n_s__NotImplemented, __pyx_k__NotImplemented, sizeof(__pyx_k__NotImplemented), 0, 0, 1, 1}, {&__pyx_n_s__ParseFailed, __pyx_k__ParseFailed, sizeof(__pyx_k__ParseFailed), 0, 0, 1, 1}, + {&__pyx_n_s__TypeError, __pyx_k__TypeError, sizeof(__pyx_k__TypeError), 0, 0, 1, 1}, + {&__pyx_n_s__ValueError, __pyx_k__ValueError, sizeof(__pyx_k__ValueError), 0, 0, 1, 1}, {&__pyx_n_s____enter__, __pyx_k____enter__, sizeof(__pyx_k____enter__), 0, 0, 1, 1}, {&__pyx_n_s____exit__, __pyx_k____exit__, sizeof(__pyx_k____exit__), 0, 0, 1, 1}, {&__pyx_n_s____main__, __pyx_k____main__, sizeof(__pyx_k____main__), 0, 0, 1, 1}, {&__pyx_n_s____test__, __pyx_k____test__, sizeof(__pyx_k____test__), 0, 0, 1, 1}, {&__pyx_n_s___cdec, __pyx_k___cdec, sizeof(__pyx_k___cdec), 0, 0, 1, 1}, + {&__pyx_n_s__beam_alpha, __pyx_k__beam_alpha, sizeof(__pyx_k__beam_alpha), 0, 0, 1, 1}, {&__pyx_n_s__config, __pyx_k__config, sizeof(__pyx_k__config), 0, 0, 1, 1}, {&__pyx_n_s__decode, __pyx_k__decode, sizeof(__pyx_k__decode), 0, 0, 1, 1}, - {&__pyx_n_s__decoder, __pyx_k__decoder, sizeof(__pyx_k__decoder), 0, 0, 1, 1}, + {&__pyx_n_s__density, __pyx_k__density, sizeof(__pyx_k__density), 0, 0, 1, 1}, + {&__pyx_n_s__dot, __pyx_k__dot, sizeof(__pyx_k__dot), 0, 0, 1, 1}, {&__pyx_n_s__encode, __pyx_k__encode, sizeof(__pyx_k__encode), 0, 0, 1, 1}, + {&__pyx_n_s__enumerate, __pyx_k__enumerate, sizeof(__pyx_k__enumerate), 0, 0, 1, 1}, {&__pyx_n_s__eval, __pyx_k__eval, sizeof(__pyx_k__eval), 0, 0, 1, 1}, {&__pyx_n_s__grammar, __pyx_k__grammar, sizeof(__pyx_k__grammar), 0, 0, 1, 1}, + {&__pyx_n_s__inp, __pyx_k__inp, sizeof(__pyx_k__inp), 0, 0, 1, 1}, {&__pyx_n_s__open, __pyx_k__open, sizeof(__pyx_k__open), 0, 0, 1, 1}, - {&__pyx_n_s__plf_tuple, __pyx_k__plf_tuple, sizeof(__pyx_k__plf_tuple), 0, 0, 1, 1}, + {&__pyx_n_s__plf, __pyx_k__plf, sizeof(__pyx_k__plf), 0, 0, 1, 1}, {&__pyx_n_s__range, __pyx_k__range, sizeof(__pyx_k__range), 0, 0, 1, 1}, {&__pyx_n_s__self, __pyx_k__self, sizeof(__pyx_k__self), 0, 0, 1, 1}, {&__pyx_n_s__sentence, __pyx_k__sentence, sizeof(__pyx_k__sentence), 0, 0, 1, 1}, @@ -4864,10 +8075,15 @@ static __Pyx_StringTabEntry __pyx_string_tab[] = { }; static int __Pyx_InitCachedBuiltins(void) { __pyx_builtin_Exception = __Pyx_GetName(__pyx_b, __pyx_n_s__Exception); if (!__pyx_builtin_Exception) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 12; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_builtin_KeyError = __Pyx_GetName(__pyx_b, __pyx_n_s__KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_builtin_range = __Pyx_GetName(__pyx_b, __pyx_n_s__range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 35; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_builtin_open = __Pyx_GetName(__pyx_b, __pyx_n_s__open); if (!__pyx_builtin_open) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_builtin_eval = __Pyx_GetName(__pyx_b, __pyx_n_s__eval); if (!__pyx_builtin_eval) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 151; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_KeyError = __Pyx_GetName(__pyx_b, __pyx_n_s__KeyError); if (!__pyx_builtin_KeyError) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 10; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_range = __Pyx_GetName(__pyx_b, __pyx_n_s__range); if (!__pyx_builtin_range) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 20; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_ValueError = __Pyx_GetName(__pyx_b, __pyx_n_s__ValueError); if (!__pyx_builtin_ValueError) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_NotImplemented = __Pyx_GetName(__pyx_b, __pyx_n_s__NotImplemented); if (!__pyx_builtin_NotImplemented) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_eval = __Pyx_GetName(__pyx_b, __pyx_n_s__eval); if (!__pyx_builtin_eval) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 82; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_enumerate = __Pyx_GetName(__pyx_b, __pyx_n_s__enumerate); if (!__pyx_builtin_enumerate) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 9; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_TypeError = __Pyx_GetName(__pyx_b, __pyx_n_s__TypeError); if (!__pyx_builtin_TypeError) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_IndexError = __Pyx_GetName(__pyx_b, __pyx_n_s__IndexError); if (!__pyx_builtin_IndexError) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_builtin_open = __Pyx_GetName(__pyx_b, __pyx_n_s__open); if (!__pyx_builtin_open) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;} return 0; __pyx_L1_error:; return -1; @@ -4877,109 +8093,207 @@ static int __Pyx_InitCachedConstants(void) { __Pyx_RefNannyDeclarations __Pyx_RefNannySetupContext("__Pyx_InitCachedConstants", 0); - /* "_cdec.pyx":53 + /* "/Users/vchahun/Sandbox/cdec/python/src/vectors.pxi":69 + * elif op == 3: # != + * return not (self == other) + * raise NotImplemented('comparison not implemented for SparseVector') # <<<<<<<<<<<<<< * - * def read_weights(self, cfg): - * with open(cfg) as fp: # <<<<<<<<<<<<<< - * for line in fp: - * fname, value = line.split() - */ - __pyx_k_tuple_1 = PyTuple_New(3); if (unlikely(!__pyx_k_tuple_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_k_tuple_1); - __Pyx_INCREF(Py_None); - PyTuple_SET_ITEM(__pyx_k_tuple_1, 0, Py_None); - __Pyx_GIVEREF(Py_None); - __Pyx_INCREF(Py_None); - PyTuple_SET_ITEM(__pyx_k_tuple_1, 1, Py_None); - __Pyx_GIVEREF(Py_None); - __Pyx_INCREF(Py_None); - PyTuple_SET_ITEM(__pyx_k_tuple_1, 2, Py_None); - __Pyx_GIVEREF(Py_None); - __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_1)); - - /* "_cdec.pyx":62 - * if grammar: - * self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) - * inp = sentence.strip().encode('utf8') # <<<<<<<<<<<<<< - * cdef decoder.BasicObserver observer = decoder.BasicObserver() - * self.dec.Decode(string(<char *>inp), &observer) + * def __len__(self): */ - __pyx_k_tuple_2 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_2)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 62; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_k_tuple_2); - __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); - PyTuple_SET_ITEM(__pyx_k_tuple_2, 0, ((PyObject *)__pyx_n_s__utf8)); - __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); - __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_2)); + __pyx_k_tuple_3 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_3)) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 69; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_3); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_2)); + PyTuple_SET_ITEM(__pyx_k_tuple_3, 0, ((PyObject *)__pyx_kp_s_2)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_2)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_3)); - /* "_cdec.pyx":85 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":17 * hypergraph.ViterbiESentence(self.hg[0], &trans) * cdef str sentence = GetString(trans).c_str() * return sentence.decode('utf8') # <<<<<<<<<<<<<< * * def viterbi_tree(self): */ - __pyx_k_tuple_3 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_3)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 85; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_k_tuple_3); + __pyx_k_tuple_4 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_4)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 17; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_4); __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); - PyTuple_SET_ITEM(__pyx_k_tuple_3, 0, ((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_4, 0, ((PyObject *)__pyx_n_s__utf8)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); - __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_3)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_4)); - /* "_cdec.pyx":90 - * assert (self.hg != NULL) + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":21 + * def viterbi_tree(self): * cdef str tree = hypergraph.ViterbiETree(self.hg[0]).c_str() * return tree.decode('utf8') # <<<<<<<<<<<<<< * - * def kbest(self, size): + * def viterbi_source_tree(self): */ - __pyx_k_tuple_4 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_4)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 90; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_k_tuple_4); + __pyx_k_tuple_5 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_5)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_5); __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); - PyTuple_SET_ITEM(__pyx_k_tuple_4, 0, ((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_5, 0, ((PyObject *)__pyx_n_s__utf8)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); - __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_4)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_5)); - /* "_cdec.pyx":102 - * if not derivation: break - * tree = GetString(derivation._yield).c_str() - * yield tree.decode('utf8') # <<<<<<<<<<<<<< - * del derivations + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":25 + * def viterbi_source_tree(self): + * cdef str tree = hypergraph.ViterbiFTree(self.hg[0]).c_str() + * return tree.decode('utf8') # <<<<<<<<<<<<<< * + * def viterbi_features(self): */ - __pyx_k_tuple_5 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_5)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 102; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_k_tuple_5); + __pyx_k_tuple_6 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_6)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 25; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_6); __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); - PyTuple_SET_ITEM(__pyx_k_tuple_5, 0, ((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_6, 0, ((PyObject *)__pyx_n_s__utf8)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); - __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_5)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_6)); - /* "_cdec.pyx":115 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":41 * if not derivation: break * sentence = GetString(derivation._yield).c_str() * yield sentence.decode('utf8') # <<<<<<<<<<<<<< * del derivations * */ - __pyx_k_tuple_6 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_6)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 115; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_k_tuple_6); + __pyx_k_tuple_7 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_7)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 41; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_7); __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); - PyTuple_SET_ITEM(__pyx_k_tuple_6, 0, ((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_7, 0, ((PyObject *)__pyx_n_s__utf8)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); - __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_6)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_7)); + + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":53 + * if not derivation: break + * tree = GetString(derivation._yield).c_str() + * yield tree.decode('utf8') # <<<<<<<<<<<<<< + * del derivations + * + */ + __pyx_k_tuple_8 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_8)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 53; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_8); + __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_8, 0, ((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_8)); - /* "_cdec.pyx":132 + /* "/Users/vchahun/Sandbox/cdec/python/src/hypergraph.pxi":65 * for k in range(hypos.size()): * sentence = GetString(hypos[0][k].words).c_str() * yield sentence.decode('utf8') # <<<<<<<<<<<<<< * del hypos * */ - __pyx_k_tuple_7 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_7)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 132; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __Pyx_GOTREF(__pyx_k_tuple_7); + __pyx_k_tuple_9 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_9)) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 65; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_9); __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); - PyTuple_SET_ITEM(__pyx_k_tuple_7, 0, ((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_9, 0, ((PyObject *)__pyx_n_s__utf8)); __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); - __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_7)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_9)); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":13 + * else: + * if isinstance(inp, unicode): + * inp = inp.encode('utf8') # <<<<<<<<<<<<<< + * if not isinstance(inp, str): + * raise TypeError('Cannot create lattice from %s' % type(inp)) + */ + __pyx_k_tuple_12 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_12)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 13; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_12); + __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_12, 0, ((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_12)); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":21 + * def __getitem__(self, int index): + * if not 0 <= index < len(self): + * raise IndexError('lattice index out of range') # <<<<<<<<<<<<<< + * arcs = [] + * cdef vector[lattice.LatticeArc] arc_vector = self.lattice[0][index] + */ + __pyx_k_tuple_15 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_15)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 21; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_15); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_14)); + PyTuple_SET_ITEM(__pyx_k_tuple_15, 0, ((PyObject *)__pyx_kp_s_14)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_14)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_15)); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":30 + * arc = &arc_vector[i] + * label = TDConvert(arc.label) + * arcs.append((label.decode('utf8'), arc.cost, arc.dist2next)) # <<<<<<<<<<<<<< + * return tuple(arcs) + * + */ + __pyx_k_tuple_16 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_16)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 30; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_16); + __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_16, 0, ((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_16)); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":35 + * def __setitem__(self, int index, tuple arcs): + * if not 0 <= index < len(self): + * raise IndexError('lattice index out of range') # <<<<<<<<<<<<<< + * cdef lattice.LatticeArc* arc + * for (label, cost, dist2next) in arcs: + */ + __pyx_k_tuple_17 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_17)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 35; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_17); + __Pyx_INCREF(((PyObject *)__pyx_kp_s_14)); + PyTuple_SET_ITEM(__pyx_k_tuple_17, 0, ((PyObject *)__pyx_kp_s_14)); + __Pyx_GIVEREF(((PyObject *)__pyx_kp_s_14)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_17)); + + /* "/Users/vchahun/Sandbox/cdec/python/src/lattice.pxi":39 + * for (label, cost, dist2next) in arcs: + * if isinstance(label, unicode): + * label = label.encode('utf8') # <<<<<<<<<<<<<< + * arc = new lattice.LatticeArc(TDConvert(<char *>label), cost, dist2next) + * self.lattice[0][index].push_back(arc[0]) + */ + __pyx_k_tuple_18 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_18)) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 39; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_18); + __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_18, 0, ((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_18)); + + /* "_cdec.pyx":31 + * + * def read_weights(self, cfg): + * with open(cfg) as fp: # <<<<<<<<<<<<<< + * for line in fp: + * fname, value = line.split() + */ + __pyx_k_tuple_19 = PyTuple_New(3); if (unlikely(!__pyx_k_tuple_19)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 31; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_19); + __Pyx_INCREF(Py_None); + PyTuple_SET_ITEM(__pyx_k_tuple_19, 0, Py_None); + __Pyx_GIVEREF(Py_None); + __Pyx_INCREF(Py_None); + PyTuple_SET_ITEM(__pyx_k_tuple_19, 1, Py_None); + __Pyx_GIVEREF(Py_None); + __Pyx_INCREF(Py_None); + PyTuple_SET_ITEM(__pyx_k_tuple_19, 2, Py_None); + __Pyx_GIVEREF(Py_None); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_19)); + + /* "_cdec.pyx":38 + * def translate(self, sentence, grammar=None): + * if isinstance(sentence, unicode): + * inp = sentence.strip().encode('utf8') # <<<<<<<<<<<<<< + * elif isinstance(sentence, str): + * inp = sentence.strip() + */ + __pyx_k_tuple_20 = PyTuple_New(1); if (unlikely(!__pyx_k_tuple_20)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __Pyx_GOTREF(__pyx_k_tuple_20); + __Pyx_INCREF(((PyObject *)__pyx_n_s__utf8)); + PyTuple_SET_ITEM(__pyx_k_tuple_20, 0, ((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_n_s__utf8)); + __Pyx_GIVEREF(((PyObject *)__pyx_k_tuple_20)); __Pyx_RefNannyFinishContext(); return 0; __pyx_L1_error:; @@ -4989,6 +8303,8 @@ static int __Pyx_InitCachedConstants(void) { static int __Pyx_InitGlobals(void) { if (__Pyx_InitStrings(__pyx_string_tab) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_0 = PyInt_FromLong(0); if (unlikely(!__pyx_int_0)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; + __pyx_int_1 = PyInt_FromLong(1); if (unlikely(!__pyx_int_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;}; return 0; __pyx_L1_error:; return -1; @@ -5061,33 +8377,40 @@ PyMODINIT_FUNC PyInit__cdec(void) /*--- Variable export code ---*/ /*--- Function export code ---*/ /*--- Type init code ---*/ - if (PyType_Ready(&__pyx_type_5_cdec_Weights) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - if (__Pyx_SetAttrString(__pyx_m, "Weights", (PyObject *)&__pyx_type_5_cdec_Weights) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_ptype_5_cdec_Weights = &__pyx_type_5_cdec_Weights; - if (PyType_Ready(&__pyx_type_5_cdec_Decoder) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - if (__Pyx_SetAttrString(__pyx_m, "Decoder", (PyObject *)&__pyx_type_5_cdec_Decoder) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 38; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_ptype_5_cdec_Decoder = &__pyx_type_5_cdec_Decoder; - if (PyType_Ready(&__pyx_type_5_cdec_Hypergraph) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - if (__Pyx_SetAttrString(__pyx_m, "Hypergraph", (PyObject *)&__pyx_type_5_cdec_Hypergraph) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 71; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyType_Ready(&__pyx_type_5_cdec_DenseVector) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "DenseVector", (PyObject *)&__pyx_type_5_cdec_DenseVector) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_5_cdec_DenseVector = &__pyx_type_5_cdec_DenseVector; + if (PyType_Ready(&__pyx_type_5_cdec_SparseVector) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "SparseVector", (PyObject *)&__pyx_type_5_cdec_SparseVector) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_5_cdec_SparseVector = &__pyx_type_5_cdec_SparseVector; + if (PyType_Ready(&__pyx_type_5_cdec_Hypergraph) < 0) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "Hypergraph", (PyObject *)&__pyx_type_5_cdec_Hypergraph) < 0) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 4; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_ptype_5_cdec_Hypergraph = &__pyx_type_5_cdec_Hypergraph; - if (PyType_Ready(&__pyx_type_5_cdec_Lattice) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - if (__Pyx_SetAttrString(__pyx_m, "Lattice", (PyObject *)&__pyx_type_5_cdec_Lattice) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 139; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyType_Ready(&__pyx_type_5_cdec_Lattice) < 0) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "Lattice", (PyObject *)&__pyx_type_5_cdec_Lattice) < 0) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 3; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_ptype_5_cdec_Lattice = &__pyx_type_5_cdec_Lattice; - if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct____iter__) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 33; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (PyType_Ready(&__pyx_type_5_cdec_Decoder) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + if (__Pyx_SetAttrString(__pyx_m, "Decoder", (PyObject *)&__pyx_type_5_cdec_Decoder) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 15; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_5_cdec_Decoder = &__pyx_type_5_cdec_Decoder; + if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct____iter__) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 18; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __pyx_ptype_5_cdec___pyx_scope_struct____iter__ = &__pyx_type_5_cdec___pyx_scope_struct____iter__; - if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct_1_kbest) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 92; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_ptype_5_cdec___pyx_scope_struct_1_kbest = &__pyx_type_5_cdec___pyx_scope_struct_1_kbest; - if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct_2_kbest_tree) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 105; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_ptype_5_cdec___pyx_scope_struct_2_kbest_tree = &__pyx_type_5_cdec___pyx_scope_struct_2_kbest_tree; - if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct_3_sample) < 0) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 122; __pyx_clineno = __LINE__; goto __pyx_L1_error;} - __pyx_ptype_5_cdec___pyx_scope_struct_3_sample = &__pyx_type_5_cdec___pyx_scope_struct_3_sample; + if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct_1___iter__) < 0) {__pyx_filename = __pyx_f[1]; __pyx_lineno = 43; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_5_cdec___pyx_scope_struct_1___iter__ = &__pyx_type_5_cdec___pyx_scope_struct_1___iter__; + if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct_2_kbest) < 0) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 32; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_5_cdec___pyx_scope_struct_2_kbest = &__pyx_type_5_cdec___pyx_scope_struct_2_kbest; + if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct_3_kbest_tree) < 0) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 44; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_5_cdec___pyx_scope_struct_3_kbest_tree = &__pyx_type_5_cdec___pyx_scope_struct_3_kbest_tree; + if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct_4_sample) < 0) {__pyx_filename = __pyx_f[2]; __pyx_lineno = 56; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_5_cdec___pyx_scope_struct_4_sample = &__pyx_type_5_cdec___pyx_scope_struct_4_sample; + if (PyType_Ready(&__pyx_type_5_cdec___pyx_scope_struct_5___iter__) < 0) {__pyx_filename = __pyx_f[3]; __pyx_lineno = 50; __pyx_clineno = __LINE__; goto __pyx_L1_error;} + __pyx_ptype_5_cdec___pyx_scope_struct_5___iter__ = &__pyx_type_5_cdec___pyx_scope_struct_5___iter__; /*--- Type import code ---*/ /*--- Variable import code ---*/ /*--- Function import code ---*/ /*--- Execution code ---*/ /* "_cdec.pyx":10 - * cimport kbest as kb + * include "lattice.pxi" * * SetSilent(True) # <<<<<<<<<<<<<< * @@ -5119,7 +8442,7 @@ PyMODINIT_FUNC PyInit__cdec(void) /* "_cdec.pyx":1 * from libcpp.string cimport string # <<<<<<<<<<<<<< * from libcpp.vector cimport vector - * from cython.operator cimport dereference as deref + * from utils cimport * */ __pyx_t_1 = PyDict_New(); if (unlikely(!__pyx_t_1)) {__pyx_filename = __pyx_f[0]; __pyx_lineno = 1; __pyx_clineno = __LINE__; goto __pyx_L1_error;} __Pyx_GOTREF(((PyObject *)__pyx_t_1)); @@ -5177,141 +8500,6 @@ static PyObject *__Pyx_GetName(PyObject *dict, PyObject *name) { return result; } -static void __Pyx_RaiseDoubleKeywordsError( - const char* func_name, - PyObject* kw_name) -{ - PyErr_Format(PyExc_TypeError, - #if PY_MAJOR_VERSION >= 3 - "%s() got multiple values for keyword argument '%U'", func_name, kw_name); - #else - "%s() got multiple values for keyword argument '%s'", func_name, - PyString_AS_STRING(kw_name)); - #endif -} - -static int __Pyx_ParseOptionalKeywords( - PyObject *kwds, - PyObject **argnames[], - PyObject *kwds2, - PyObject *values[], - Py_ssize_t num_pos_args, - const char* function_name) -{ - PyObject *key = 0, *value = 0; - Py_ssize_t pos = 0; - PyObject*** name; - PyObject*** first_kw_arg = argnames + num_pos_args; - while (PyDict_Next(kwds, &pos, &key, &value)) { - name = first_kw_arg; - while (*name && (**name != key)) name++; - if (*name) { - values[name-argnames] = value; - } else { - #if PY_MAJOR_VERSION < 3 - if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) { - #else - if (unlikely(!PyUnicode_Check(key))) { - #endif - goto invalid_keyword_type; - } else { - for (name = first_kw_arg; *name; name++) { - #if PY_MAJOR_VERSION >= 3 - if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) && - PyUnicode_Compare(**name, key) == 0) break; - #else - if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) && - _PyString_Eq(**name, key)) break; - #endif - } - if (*name) { - values[name-argnames] = value; - } else { - for (name=argnames; name != first_kw_arg; name++) { - if (**name == key) goto arg_passed_twice; - #if PY_MAJOR_VERSION >= 3 - if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) && - PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice; - #else - if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) && - _PyString_Eq(**name, key)) goto arg_passed_twice; - #endif - } - if (kwds2) { - if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad; - } else { - goto invalid_keyword; - } - } - } - } - } - return 0; -arg_passed_twice: - __Pyx_RaiseDoubleKeywordsError(function_name, **name); - goto bad; -invalid_keyword_type: - PyErr_Format(PyExc_TypeError, - "%s() keywords must be strings", function_name); - goto bad; -invalid_keyword: - PyErr_Format(PyExc_TypeError, - #if PY_MAJOR_VERSION < 3 - "%s() got an unexpected keyword argument '%s'", - function_name, PyString_AsString(key)); - #else - "%s() got an unexpected keyword argument '%U'", - function_name, key); - #endif -bad: - return -1; -} - -static void __Pyx_RaiseArgtupleInvalid( - const char* func_name, - int exact, - Py_ssize_t num_min, - Py_ssize_t num_max, - Py_ssize_t num_found) -{ - Py_ssize_t num_expected; - const char *more_or_less; - if (num_found < num_min) { - num_expected = num_min; - more_or_less = "at least"; - } else { - num_expected = num_max; - more_or_less = "at most"; - } - if (exact) { - more_or_less = "exactly"; - } - PyErr_Format(PyExc_TypeError, - "%s() takes %s %"PY_FORMAT_SIZE_T"d positional argument%s (%"PY_FORMAT_SIZE_T"d given)", - func_name, more_or_less, num_expected, - (num_expected == 1) ? "" : "s", num_found); -} - -static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, - const char *name, int exact) -{ - if (!type) { - PyErr_Format(PyExc_SystemError, "Missing type object"); - return 0; - } - if (none_allowed && obj == Py_None) return 1; - else if (exact) { - if (Py_TYPE(obj) == type) return 1; - } - else { - if (PyObject_TypeCheck(obj, type)) return 1; - } - PyErr_Format(PyExc_TypeError, - "Argument '%s' has incorrect type (expected %s, got %s)", - name, type->tp_name, Py_TYPE(obj)->tp_name); - return 0; -} - static CYTHON_INLINE void __Pyx_ErrRestore(PyObject *type, PyObject *value, PyObject *tb) { #if CYTHON_COMPILING_IN_CPYTHON PyObject *tmp_type, *tmp_value, *tmp_tb; @@ -5465,6 +8653,141 @@ bad: } #endif +static int __Pyx_ArgTypeTest(PyObject *obj, PyTypeObject *type, int none_allowed, + const char *name, int exact) +{ + if (!type) { + PyErr_Format(PyExc_SystemError, "Missing type object"); + return 0; + } + if (none_allowed && obj == Py_None) return 1; + else if (exact) { + if (Py_TYPE(obj) == type) return 1; + } + else { + if (PyObject_TypeCheck(obj, type)) return 1; + } + PyErr_Format(PyExc_TypeError, + "Argument '%s' has incorrect type (expected %s, got %s)", + name, type->tp_name, Py_TYPE(obj)->tp_name); + return 0; +} + +static void __Pyx_RaiseDoubleKeywordsError( + const char* func_name, + PyObject* kw_name) +{ + PyErr_Format(PyExc_TypeError, + #if PY_MAJOR_VERSION >= 3 + "%s() got multiple values for keyword argument '%U'", func_name, kw_name); + #else + "%s() got multiple values for keyword argument '%s'", func_name, + PyString_AS_STRING(kw_name)); + #endif +} + +static int __Pyx_ParseOptionalKeywords( + PyObject *kwds, + PyObject **argnames[], + PyObject *kwds2, + PyObject *values[], + Py_ssize_t num_pos_args, + const char* function_name) +{ + PyObject *key = 0, *value = 0; + Py_ssize_t pos = 0; + PyObject*** name; + PyObject*** first_kw_arg = argnames + num_pos_args; + while (PyDict_Next(kwds, &pos, &key, &value)) { + name = first_kw_arg; + while (*name && (**name != key)) name++; + if (*name) { + values[name-argnames] = value; + } else { + #if PY_MAJOR_VERSION < 3 + if (unlikely(!PyString_CheckExact(key)) && unlikely(!PyString_Check(key))) { + #else + if (unlikely(!PyUnicode_Check(key))) { + #endif + goto invalid_keyword_type; + } else { + for (name = first_kw_arg; *name; name++) { + #if PY_MAJOR_VERSION >= 3 + if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) && + PyUnicode_Compare(**name, key) == 0) break; + #else + if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) && + _PyString_Eq(**name, key)) break; + #endif + } + if (*name) { + values[name-argnames] = value; + } else { + for (name=argnames; name != first_kw_arg; name++) { + if (**name == key) goto arg_passed_twice; + #if PY_MAJOR_VERSION >= 3 + if (PyUnicode_GET_SIZE(**name) == PyUnicode_GET_SIZE(key) && + PyUnicode_Compare(**name, key) == 0) goto arg_passed_twice; + #else + if (PyString_GET_SIZE(**name) == PyString_GET_SIZE(key) && + _PyString_Eq(**name, key)) goto arg_passed_twice; + #endif + } + if (kwds2) { + if (unlikely(PyDict_SetItem(kwds2, key, value))) goto bad; + } else { + goto invalid_keyword; + } + } + } + } + } + return 0; +arg_passed_twice: + __Pyx_RaiseDoubleKeywordsError(function_name, **name); + goto bad; +invalid_keyword_type: + PyErr_Format(PyExc_TypeError, + "%s() keywords must be strings", function_name); + goto bad; +invalid_keyword: + PyErr_Format(PyExc_TypeError, + #if PY_MAJOR_VERSION < 3 + "%s() got an unexpected keyword argument '%s'", + function_name, PyString_AsString(key)); + #else + "%s() got an unexpected keyword argument '%U'", + function_name, key); + #endif +bad: + return -1; +} + +static void __Pyx_RaiseArgtupleInvalid( + const char* func_name, + int exact, + Py_ssize_t num_min, + Py_ssize_t num_max, + Py_ssize_t num_found) +{ + Py_ssize_t num_expected; + const char *more_or_less; + if (num_found < num_min) { + num_expected = num_min; + more_or_less = "at least"; + } else { + num_expected = num_max; + more_or_less = "at most"; + } + if (exact) { + more_or_less = "exactly"; + } + PyErr_Format(PyExc_TypeError, + "%s() takes %s %"PY_FORMAT_SIZE_T"d positional argument%s (%"PY_FORMAT_SIZE_T"d given)", + func_name, more_or_less, num_expected, + (num_expected == 1) ? "" : "s", num_found); +} + static CYTHON_INLINE void __Pyx_RaiseNeedMoreValuesError(Py_ssize_t index) { PyErr_Format(PyExc_ValueError, "need more than %"PY_FORMAT_SIZE_T"d value%s to unpack", @@ -5492,6 +8815,8 @@ static int __Pyx_IternextUnpackEndCheck(PyObject *retval, Py_ssize_t expected) { return 0; } + + static double __Pyx__PyObject_AsDouble(PyObject* obj) { PyObject* float_value; if (Py_TYPE(obj)->tp_as_number && Py_TYPE(obj)->tp_as_number->nb_float) { diff --git a/python/src/_cdec.pyx b/python/src/_cdec.pyx index 664724dd..698a66f6 100644 --- a/python/src/_cdec.pyx +++ b/python/src/_cdec.pyx @@ -1,50 +1,28 @@ from libcpp.string cimport string from libcpp.vector cimport vector -from cython.operator cimport dereference as deref from utils cimport * -cimport hypergraph cimport decoder -cimport lattice -cimport kbest as kb + +include "vectors.pxi" +include "hypergraph.pxi" +include "lattice.pxi" SetSilent(True) class ParseFailed(Exception): pass -cdef class Weights: - cdef vector[weight_t]* weights - - def __cinit__(self, Decoder decoder): - self.weights = &decoder.dec.CurrentWeightVector() - - def __getitem__(self, char* fname): - cdef unsigned fid = FDConvert(fname) - if fid <= self.weights.size(): - return self.weights[0][fid] - raise KeyError(fname) - - def __setitem__(self, char* fname, float value): - cdef unsigned fid = FDConvert(<char *>fname) - if self.weights.size() <= fid: - self.weights.resize(fid + 1) - self.weights[0][fid] = value - - def __iter__(self): - cdef unsigned fid - for fid in range(1, self.weights.size()): - yield FDConvert(fid).c_str(), self.weights[0][fid] - cdef class Decoder: cdef decoder.Decoder* dec - cdef public Weights weights + cdef public DenseVector weights def __cinit__(self, char* config): decoder.register_feature_functions() cdef istringstream* config_stream = new istringstream(config) self.dec = new decoder.Decoder(config_stream) del config_stream - self.weights = Weights(self) + self.weights = DenseVector() + self.weights.vector = &self.dec.CurrentWeightVector() def __dealloc__(self): del self.dec @@ -55,11 +33,17 @@ cdef class Decoder: fname, value = line.split() self.weights[fname.strip()] = float(value) - # TODO: list, lattice translation - def translate(self, unicode sentence, grammar=None): + def translate(self, sentence, grammar=None): + if isinstance(sentence, unicode): + inp = sentence.strip().encode('utf8') + elif isinstance(sentence, str): + inp = sentence.strip() + elif isinstance(sentence, Lattice): + inp = str(sentence) # PLF format + else: + raise TypeError('Cannot translate input type %s' % type(sentence)) if grammar: self.dec.SetSentenceGrammarFromString(string(<char *> grammar)) - inp = sentence.strip().encode('utf8') cdef decoder.BasicObserver observer = decoder.BasicObserver() self.dec.Decode(string(<char *>inp), &observer) if observer.hypergraph == NULL: @@ -67,90 +51,3 @@ cdef class Decoder: cdef Hypergraph hg = Hypergraph() hg.hg = new hypergraph.Hypergraph(observer.hypergraph[0]) return hg - -cdef class Hypergraph: - cdef hypergraph.Hypergraph* hg - cdef MT19937* rng - - def __dealloc__(self): - del self.hg - if self.rng != NULL: - del self.rng - - def viterbi(self): - assert (self.hg != NULL) - cdef vector[WordID] trans - hypergraph.ViterbiESentence(self.hg[0], &trans) - cdef str sentence = GetString(trans).c_str() - return sentence.decode('utf8') - - def viterbi_tree(self): - assert (self.hg != NULL) - cdef str tree = hypergraph.ViterbiETree(self.hg[0]).c_str() - return tree.decode('utf8') - - def kbest(self, size): - assert (self.hg != NULL) - cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal](self.hg[0], size) - cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal].Derivation* derivation - cdef str tree - cdef unsigned k - for k in range(size): - derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) - if not derivation: break - tree = GetString(derivation._yield).c_str() - yield tree.decode('utf8') - del derivations - - def kbest_tree(self, size): - assert (self.hg != NULL) - cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ETreeTraversal](self.hg[0], size) - cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal].Derivation* derivation - cdef str sentence - cdef unsigned k - for k in range(size): - derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) - if not derivation: break - sentence = GetString(derivation._yield).c_str() - yield sentence.decode('utf8') - del derivations - - def intersect(self, Lattice lat): - assert (self.hg != NULL) - hypergraph.Intersect(lat.lattice[0], self.hg) - - def sample(self, unsigned n): - assert (self.hg != NULL) - cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() - if self.rng == NULL: - self.rng = new MT19937() - hypergraph.sample_hypotheses(self.hg[0], n, self.rng, hypos) - cdef str sentence - cdef unsigned k - for k in range(hypos.size()): - sentence = GetString(hypos[0][k].words).c_str() - yield sentence.decode('utf8') - del hypos - - # TODO: get feature expectations, get partition function ("inside" score) - # TODO: reweight the forest with different weights (Hypergraph::Reweight) - # TODO: inside-outside pruning - -cdef class Lattice: - cdef lattice.Lattice* lattice - - def __init__(self, tuple plf_tuple): - self.lattice = new lattice.Lattice() - cdef bytes plf = str(plf_tuple) - hypergraph.PLFtoLattice(string(<char *>plf), self.lattice) - - def __str__(self): - return hypergraph.AsPLF(self.lattice[0]).c_str() - - def __iter__(self): - return iter(eval(str(self))) - - def __dealloc__(self): - del self.lattice - -# TODO: wrap SparseVector diff --git a/python/src/decoder.pxd b/python/src/decoder.pxd index ab858841..e2678151 100644 --- a/python/src/decoder.pxd +++ b/python/src/decoder.pxd @@ -4,7 +4,7 @@ from hypergraph cimport Hypergraph from utils cimport istream, weight_t cdef extern from "decoder/ff_register.h": - cdef void register_feature_functions() + void register_feature_functions() cdef extern from "decoder/decoder.h": cdef cppclass SentenceMetadata: @@ -16,20 +16,20 @@ cdef extern from "decoder/decoder.h": cdef cppclass Decoder: Decoder(int argc, char** argv) Decoder(istream* config_file) - bint Decode(string input, DecoderObserver* observer) + bint Decode(string& inp, DecoderObserver* observer) # access this to either *read* or *write* to the decoder's last # weight vector (i.e., the weights of the finest past) vector[weight_t]& CurrentWeightVector() - void SetId(int id) - #const boost::program_options::variables_map& GetConf() const { return conf } + # void SetId(int id) + # const boost::program_options::variables_map& GetConf() const { return conf } # add grammar rules (currently only supported by SCFG decoders) # that will be used on subsequent calls to Decode. rules should be in standard # text format. This function does NOT read from a file. - void SetSupplementalGrammar(string grammar) - void SetSentenceGrammarFromString(string grammar_str) + void SetSupplementalGrammar(string& grammar) + void SetSentenceGrammarFromString(string& grammar_str) cdef extern from "observer.h": cdef cppclass BasicObserver(DecoderObserver): diff --git a/python/src/hypergraph.pxd b/python/src/hypergraph.pxd index 4aef6955..656b1d47 100644 --- a/python/src/hypergraph.pxd +++ b/python/src/hypergraph.pxd @@ -1,41 +1,73 @@ from libcpp.string cimport string from libcpp.vector cimport vector from utils cimport * -cimport lattice +from lattice cimport Lattice cdef extern from "decoder/hg.h": + cdef cppclass EdgeMask "std::vector<bool>": + EdgeMask(int size) + bint& operator[](int) + cdef cppclass Hypergraph: + cppclass Edge: + int id_ + int head_node_ # position in hg.nodes_ + SmallVector[unsigned] tail_nodes_ # positions in hg.nodes_ + #TRulePtr rule_ + FastSparseVector[weight_t] feature_values_ + LogVal[double] edge_prob_ + short int i_ + short int j_ + short int prev_i_ + short int prev_j_ cppclass Node: int id_ WordID cat_ WordID NT() - #EdgesVector in_edges_ - #EdgesVector out_edges_ + vector[Edge] in_edges_ + vector[Edge] out_edges_ Hypergraph(Hypergraph) vector[Node] nodes_ + vector[Edge] edges_ + void Reweight(vector[weight_t]& weights) + void Reweight(FastSparseVector& weights) + bint PruneInsideOutside(double beam_alpha, + double density, + EdgeMask* preserve_mask, + bint use_sum_prod_semiring, + double scale, + bint safe_inside) cdef extern from "decoder/viterbi.h": - cdef prob_t ViterbiESentence(Hypergraph hg, vector[WordID]* trans) - cdef string ViterbiETree(Hypergraph hg) + LogVal[double] ViterbiESentence(Hypergraph& hg, vector[WordID]* trans) + string ViterbiETree(Hypergraph& hg) + LogVal[double] ViterbiFSentence(Hypergraph& hg, vector[WordID]* trans) + string ViterbiFTree(Hypergraph& hg) + FastSparseVector[weight_t] ViterbiFeatures(Hypergraph& hg) + FastSparseVector[weight_t] ViterbiFeatures(Hypergraph& hg, + FastSparseVector[weight_t]* weights, + bint fatal_dotprod_disagreement) cdef extern from "decoder/hg_io.h" namespace "HypergraphIO": bint ReadFromJSON(istream* inp, Hypergraph* out) - bint WriteToJSON(Hypergraph hg, bint remove_rules, ostream* out) - - #void WriteAsCFG(Hypergraph hg) - #void WriteTarget(string base, unsigned sent_id, Hypergraph hg) - - void ReadFromPLF(string inp, Hypergraph* out, int line=*) - string AsPLF(Hypergraph hg, bint include_global_parentheses=*) - string AsPLF(lattice.Lattice lat, bint include_global_parentheses=*) - void PLFtoLattice(string plf, lattice.Lattice* pl) + bint WriteToJSON(Hypergraph& hg, bint remove_rules, ostream* out) + void ReadFromPLF(string& inp, Hypergraph* out, int line) + string AsPLF(Hypergraph& hg, bint include_global_parentheses) + void PLFtoLattice(string& plf, Lattice* pl) + string AsPLF(Lattice& lat, bint include_global_parentheses) cdef extern from "decoder/hg_intersect.h" namespace "HG": - bint Intersect(lattice.Lattice target, Hypergraph* hg) + bint Intersect(Lattice& target, Hypergraph* hg) cdef extern from "decoder/hg_sampler.h" namespace "HypergraphSampler": cdef cppclass Hypothesis: vector[WordID] words - SparseVector[double] fmap - prob_t model_score - void sample_hypotheses(Hypergraph hg, unsigned n, MT19937* rng, vector[Hypothesis]* hypos) + FastSparseVector[double] fmap + LogVal[double] model_score + void sample_hypotheses(Hypergraph& hg, + unsigned n, + MT19937* rng, + vector[Hypothesis]* hypos) + +cdef extern from "decoder/csplit.h" namespace "CompoundSplit": + int GetFullWordEdgeIndex(Hypergraph& forest) diff --git a/python/src/hypergraph.pxi b/python/src/hypergraph.pxi new file mode 100644 index 00000000..c226d105 --- /dev/null +++ b/python/src/hypergraph.pxi @@ -0,0 +1,92 @@ +cimport hypergraph +cimport kbest as kb + +cdef class Hypergraph: + cdef hypergraph.Hypergraph* hg + cdef MT19937* rng + + def __dealloc__(self): + del self.hg + if self.rng != NULL: + del self.rng + + def viterbi(self): + cdef vector[WordID] trans + hypergraph.ViterbiESentence(self.hg[0], &trans) + cdef str sentence = GetString(trans).c_str() + return sentence.decode('utf8') + + def viterbi_tree(self): + cdef str tree = hypergraph.ViterbiETree(self.hg[0]).c_str() + return tree.decode('utf8') + + def viterbi_source_tree(self): + cdef str tree = hypergraph.ViterbiFTree(self.hg[0]).c_str() + return tree.decode('utf8') + + def viterbi_features(self): + cdef SparseVector fmap = SparseVector() + fmap.vector = new FastSparseVector[weight_t](hypergraph.ViterbiFeatures(self.hg[0])) + return fmap + + def kbest(self, size): + cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal](self.hg[0], size) + cdef kb.KBestDerivations[vector[WordID], kb.ESentenceTraversal].Derivation* derivation + cdef str sentence + cdef unsigned k + for k in range(size): + derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) + if not derivation: break + sentence = GetString(derivation._yield).c_str() + yield sentence.decode('utf8') + del derivations + + def kbest_tree(self, size): + cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal]* derivations = new kb.KBestDerivations[vector[WordID], kb.ETreeTraversal](self.hg[0], size) + cdef kb.KBestDerivations[vector[WordID], kb.ETreeTraversal].Derivation* derivation + cdef str tree + cdef unsigned k + for k in range(size): + derivation = derivations.LazyKthBest(self.hg.nodes_.size() - 1, k) + if not derivation: break + tree = GetString(derivation._yield).c_str() + yield tree.decode('utf8') + del derivations + + def sample(self, unsigned n): + cdef vector[hypergraph.Hypothesis]* hypos = new vector[hypergraph.Hypothesis]() + if self.rng == NULL: + self.rng = new MT19937() + hypergraph.sample_hypotheses(self.hg[0], n, self.rng, hypos) + cdef str sentence + cdef unsigned k + for k in range(hypos.size()): + sentence = GetString(hypos[0][k].words).c_str() + yield sentence.decode('utf8') + del hypos + + # TODO richer k-best/sample output (feature vectors, trees?) + + def intersect(self, Lattice lat): + return hypergraph.Intersect(lat.lattice[0], self.hg) + + def prune(self, beam_alpha=0, density=0, **kwargs): + cdef hypergraph.EdgeMask* preserve_mask = NULL + if 'csplit_preserve_full_word' in kwargs: + preserve_mask = new hypergraph.EdgeMask(self.hg.edges_.size()) + preserve_mask[0][hypergraph.GetFullWordEdgeIndex(self.hg[0])] = True + self.hg.PruneInsideOutside(beam_alpha, density, preserve_mask, False, 1, False) + + def lattice(self): # TODO direct hg -> lattice conversion in cdec + cdef str plf = hypergraph.AsPLF(self.hg[0], True).c_str() + return Lattice(eval(plf)) + + def reweight(self, weights): + if isinstance(weights, SparseVector): + self.hg.Reweight((<SparseVector> weights).vector[0]) + elif isinstance(weights, DenseVector): + self.hg.Reweight((<DenseVector> weights).vector[0]) + else: + raise ValueError('cannot reweight hypergraph with %s' % type(weights)) + + # TODO get feature expectations, get partition function ("inside" score) diff --git a/python/src/kbest.pxd b/python/src/kbest.pxd index e339714a..f34ac5ed 100644 --- a/python/src/kbest.pxd +++ b/python/src/kbest.pxd @@ -1,16 +1,18 @@ from libcpp.vector cimport vector from utils cimport WordID -cimport hypergraph +from hypergraph cimport Hypergraph cdef extern from "decoder/viterbi.h": cdef cppclass ESentenceTraversal: pass cdef cppclass ETreeTraversal: pass + cdef cppclass FTreeTraversal: + pass cdef extern from "decoder/kbest.h" namespace "KBest": cdef cppclass KBestDerivations[T, Traversal]: cppclass Derivation: T _yield "yield" - KBestDerivations(hypergraph.Hypergraph hg, unsigned k) + KBestDerivations(Hypergraph& hg, unsigned k) Derivation* LazyKthBest(unsigned v, unsigned k) diff --git a/python/src/lattice.pxd b/python/src/lattice.pxd index bdfaba80..3a4bc22f 100644 --- a/python/src/lattice.pxd +++ b/python/src/lattice.pxd @@ -1,4 +1,5 @@ from libcpp.vector cimport vector +from libcpp.string cimport string from utils cimport WordID cdef extern from "decoder/lattice.h": @@ -9,8 +10,12 @@ cdef extern from "decoder/lattice.h": LatticeArc() LatticeArc(WordID w, double c, int i) - cdef cppclass Lattice: # (vector[vector[LatticeArc]]) + cdef cppclass Lattice(vector): # (vector[vector[LatticeArc]]) Lattice() Lattice(unsigned t) - Lattice(unsigned t, vector[LatticeArc] v) + Lattice(unsigned t, vector[LatticeArc]& v) bint IsSentence() + vector[LatticeArc]& operator[](unsigned) + +cdef extern from "decoder/lattice.h" namespace "LatticeTools": + void ConvertTextToLattice(string& text, Lattice* pl) diff --git a/python/src/lattice.pxi b/python/src/lattice.pxi new file mode 100644 index 00000000..493c6dcd --- /dev/null +++ b/python/src/lattice.pxi @@ -0,0 +1,56 @@ +cimport lattice + +cdef class Lattice: + cdef lattice.Lattice* lattice + + def __init__(self, inp): + if isinstance(inp, tuple): + self.lattice = new lattice.Lattice(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) + + def __getitem__(self, int index): + if not 0 <= index < len(self): + raise IndexError('lattice index out of range') + arcs = [] + cdef vector[lattice.LatticeArc] arc_vector = self.lattice[0][index] + cdef lattice.LatticeArc* arc + cdef str label + cdef unsigned i + for i in range(arc_vector.size()): + arc = &arc_vector[i] + label = TDConvert(arc.label) + arcs.append((label.decode('utf8'), arc.cost, arc.dist2next)) + return tuple(arcs) + + def __setitem__(self, int index, tuple arcs): + if not 0 <= index < len(self): + 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) + self.lattice[0][index].push_back(arc[0]) + del arc + + def __len__(self): + return self.lattice.size() + + def __str__(self): + return hypergraph.AsPLF(self.lattice[0], True).c_str() + + def __iter__(self): + cdef unsigned i + for i in range(len(self)): + yield self[i] + + def __dealloc__(self): + del self.lattice diff --git a/python/src/utils.pxd b/python/src/utils.pxd index 786cd265..3d518524 100644 --- a/python/src/utils.pxd +++ b/python/src/utils.pxd @@ -1,5 +1,6 @@ from libcpp.string cimport string from libcpp.vector cimport vector +from libcpp.pair cimport pair cdef extern from "<iostream>" namespace "std": cdef cppclass istream: @@ -8,30 +9,60 @@ cdef extern from "<iostream>" namespace "std": pass cdef cppclass istringstream(istream): istringstream(char*) + ostream cout cdef extern from "utils/weights.h": ctypedef double weight_t cdef extern from "utils/logval.h": cdef cppclass LogVal[T]: - pass + double as_float() -cdef extern from "utils/prob.h": - cdef cppclass prob_t: - pass + double log(LogVal[double]&) cdef extern from "utils/wordid.h": ctypedef int WordID -cdef extern from "utils/sparse_vector.h": - cdef cppclass SparseVector[T]: +cdef extern from "utils/small_vector.h": + cdef cppclass SmallVector[T]: pass +cdef extern from "utils/sparse_vector.h": + cdef cppclass FastSparseVector[T]: + cppclass const_iterator: + const_iterator(FastSparseVector[T]&, bint is_end) + pair[unsigned, T]* ptr "operator->" () + const_iterator& operator++() + bint operator==(const_iterator&) + bint operator!=(const_iterator&) + FastSparseVector() + FastSparseVector(FastSparseVector[T]&) + const_iterator begin() + const_iterator end() + void init_vector(vector[T]* vp) + T value(unsigned k) + void set_value(unsigned k, T& v) + size_t size() + bint nonzero(unsigned k) + bint operator==(FastSparseVector[T]&) + T dot(vector[weight_t]&) # cython bug when [T] + T dot(FastSparseVector[T]&) + + FastSparseVector[weight_t] operator+(FastSparseVector[weight_t]&, FastSparseVector[weight_t]&) + FastSparseVector[weight_t] operator-(FastSparseVector[weight_t]&, FastSparseVector[weight_t]&) + ostream operator<<(ostream& out, FastSparseVector[weight_t]& v) + +cdef extern from "utils/weights.h" namespace "Weights": + void InitSparseVector(vector[weight_t]& dv, FastSparseVector[weight_t]* sv) + cdef extern from "utils/tdict.cc" namespace "TD": - cdef string GetString(vector[WordID] st) + string GetString(vector[WordID]& st) + unsigned NumWords() + WordID TDConvert "TD::Convert" (char*) + char* TDConvert "TD::Convert" (WordID) cdef extern from "utils/verbose.h": - cdef void SetSilent(bint) + void SetSilent(bint) cdef extern from "utils/fdict.h" namespace "FD": WordID FDConvert "FD::Convert" (char*) @@ -39,15 +70,9 @@ cdef extern from "utils/fdict.h" namespace "FD": cdef extern from "utils/filelib.h": cdef cppclass ReadFile: - ReadFile(string) + ReadFile(string&) istream* stream() cdef extern from "utils/sampler.h": cdef cppclass MT19937: pass - -""" -cdef extern from "<boost/shared_ptr.hpp>" namespace "boost": - cdef cppclass shared_ptr[T]: - void reset(T*) -""" diff --git a/python/src/vectors.pxi b/python/src/vectors.pxi new file mode 100644 index 00000000..74d3a0bd --- /dev/null +++ b/python/src/vectors.pxi @@ -0,0 +1,101 @@ +from cython.operator cimport preincrement as pinc + +cdef class DenseVector: + cdef vector[weight_t]* vector + + def __getitem__(self, char* fname): + cdef unsigned fid = FDConvert(fname) + if fid <= self.vector.size(): + return self.vector[0][fid] + raise KeyError(fname) + + def __setitem__(self, char* fname, float value): + cdef unsigned fid = FDConvert(<char *>fname) + if self.vector.size() <= fid: + self.vector.resize(fid + 1) + self.vector[0][fid] = value + + def __iter__(self): + cdef unsigned fid + for fid in range(1, self.vector.size()): + yield FDConvert(fid).c_str(), self.vector[0][fid] + + def dot(self, SparseVector other): + return other.dot(self) + + def tosparse(self): + cdef SparseVector sparse = SparseVector() + sparse.vector = new FastSparseVector[weight_t]() + InitSparseVector(self.vector[0], sparse.vector) + return sparse + +cdef class SparseVector: + cdef FastSparseVector[weight_t]* vector + + def __getitem__(self, char* fname): + cdef unsigned fid = FDConvert(fname) + return self.vector.value(fid) + + def __setitem__(self, char* fname, float value): + cdef unsigned fid = FDConvert(<char *>fname) + self.vector.set_value(fid, value) + + def __iter__(self): + cdef FastSparseVector[weight_t].const_iterator* it = new FastSparseVector[weight_t].const_iterator(self.vector[0], False) + cdef str fname + for i in range(self.vector.size()): + fname = FDConvert(it[0].ptr().first).c_str() + yield (fname, it[0].ptr().second) + pinc(it[0]) + + def dot(self, other): + if isinstance(other, DenseVector): + return self.vector.dot((<DenseVector> other).vector[0]) + elif isinstance(other, SparseVector): + return self.vector.dot((<SparseVector> other).vector[0]) + raise ValueError('cannot take the dot product of %s and SparseVector' % type(other)) + + def todense(self): + cdef DenseVector dense = DenseVector() + dense.vector = new vector[weight_t]() + self.vector.init_vector(dense.vector) + return dense + + def __richcmp__(SparseVector self, SparseVector other, int op): + if op == 2: # == + return self.vector[0] == other.vector[0] + elif op == 3: # != + return not (self == other) + raise NotImplemented('comparison not implemented for SparseVector') + + def __len__(self): + return self.vector.size() + + def __contains__(self, char* fname): + return self.vector.nonzero(FDConvert(fname)) + + def __iadd__(SparseVector self, SparseVector other): + self.vector[0] += other.vector[0] + return self + + def __isub__(SparseVector self, SparseVector other): + self.vector[0] -= other.vector[0] + return self + + def __imul__(SparseVector self, float scalar): + self.vector[0] *= scalar + return self + + def __idiv__(SparseVector self, float scalar): + self.vector[0] /= scalar + return self + + def __add__(SparseVector self, SparseVector other): + cdef SparseVector result = SparseVector() + result.vector = new FastSparseVector[weight_t](self.vector[0] + other.vector[0]) + return result + + def __sub__(SparseVector self, SparseVector other): + cdef SparseVector result = SparseVector() + result.vector = new FastSparseVector[weight_t](self.vector[0] - other.vector[0]) + return result |