Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[python] Add convenience methods

  • Loading branch information...
commit 757f56e391bd2e1d7442ab38fc98aff00d064d38 1 parent 32a8d92
@vchahun vchahun authored
View
4,730 python/src/_cdec.cpp
3,339 additions, 1,391 deletions not shown
View
18 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] = (<DenseVector> weights).vector[0]
+ elif isinstance(weights, SparseVector):
+ self.weights.vector.clear()
+ ((<SparseVector> 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:
View
12 python/src/lattice.pxi
@@ -54,3 +54,15 @@ cdef class Lattice:
def __dealloc__(self):
del self.lattice
+
+ def todot(self):
+ def lines():
+ yield 'digraph lattice {'
+ yield 'rankdir = LR;'
+ yield 'node [shape=circle];'
+ for i in range(len(self)):
+ for label, weight, delta in self[i]:
+ yield '%d -> %d [label="%s"];' % (i, i+delta, label.replace('"', '\\"'))
+ yield '%d [shape=doublecircle]' % len(self)
+ yield '}'
+ return '\n'.join(lines()).encode('utf8')
View
17 python/src/mteval.pxi
@@ -10,6 +10,15 @@ cdef char* as_str(sentence, error_msg='Cannot convert type %s to str'):
raise TypeError(error_msg % type(sentence))
return ret
+cdef SufficientStats as_stats(x, y):
+ if isinstance(x, SufficientStats):
+ return x
+ elif x == 0 and isinstance(y, SufficientStats):
+ stats = SufficientStats()
+ stats.stats = new mteval.SufficientStats()
+ stats.metric = (<SufficientStats> y).metric
+ return stats
+
cdef class Candidate:
cdef mteval.Candidate* candidate
cdef public float score
@@ -50,10 +59,12 @@ cdef class SufficientStats:
self.stats[0] += other.stats[0]
return self
- def __add__(SufficientStats x, SufficientStats y):
+ def __add__(x, y):
+ cdef SufficientStats sx = as_stats(x, y)
+ cdef SufficientStats sy = as_stats(y, x)
cdef SufficientStats result = SufficientStats()
- result.stats = new mteval.SufficientStats(mteval.add(x.stats[0], y.stats[0]))
- result.metric = x.metric
+ result.stats = new mteval.SufficientStats(mteval.add(sx.stats[0], sy.stats[0]))
+ result.metric = sx.metric
return result
cdef class CandidateSet:
View
6 python/src/vectors.pxi
@@ -80,6 +80,12 @@ cdef class SparseVector:
def __contains__(self, char* fname):
return self.vector.nonzero(FDConvert(fname))
+
+ def __neg__(self):
+ cdef SparseVector result = SparseVector()
+ result.vector = new FastSparseVector[weight_t](self.vector[0])
+ result.vector[0] *= -1.0
+ return result
def __iadd__(SparseVector self, SparseVector other):
self.vector[0] += other.vector[0]

0 comments on commit 757f56e

Please sign in to comment.
Something went wrong with that request. Please try again.