From 757f56e391bd2e1d7442ab38fc98aff00d064d38 Mon Sep 17 00:00:00 2001 From: Victor Chahuneau Date: Thu, 5 Jul 2012 16:53:26 -0400 Subject: [python] Add convenience methods --- python/src/_cdec.pyx | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'python/src/_cdec.pyx') diff --git a/python/src/_cdec.pyx b/python/src/_cdec.pyx index 6fafbbc0..cccfec0b 100644 --- a/python/src/_cdec.pyx +++ b/python/src/_cdec.pyx @@ -14,7 +14,7 @@ class ParseFailed(Exception): pass cdef class Decoder: cdef decoder.Decoder* dec - cdef public DenseVector weights + cdef DenseVector weights def __cinit__(self, char* config): decoder.register_feature_functions() @@ -27,6 +27,22 @@ cdef class Decoder: def __dealloc__(self): del self.dec + property weights: + def __get__(self): + return self.weights + + def __set__(self, weights): + if isinstance(weights, DenseVector): + self.weights.vector[0] = ( weights).vector[0] + elif isinstance(weights, SparseVector): + self.weights.vector.clear() + (( weights).vector[0]).init_vector(self.weights.vector) + elif isinstance(weights, dict): + for fname, fval in weights.items(): + self.weights[fname] = fval + else: + raise TypeError('cannot initialize weights with %s' % type(weights)) + def read_weights(self, cfg): with open(cfg) as fp: for line in fp: -- cgit v1.2.3