summaryrefslogtreecommitdiff
path: root/python/src/mteval.pxi
diff options
context:
space:
mode:
authorPatrick Simianer <p@simianer.de>2012-07-06 11:45:32 +0200
committerPatrick Simianer <p@simianer.de>2012-07-06 11:45:32 +0200
commit45a1af0ff9c164978f91b2734fb24c45551aa25c (patch)
treea3cb13ccc47386b2e51e1fb50fa9a96d5d7fa905 /python/src/mteval.pxi
parent4ec3625b3a1aa9cb417f8a551ad6723626a4c656 (diff)
parent757f56e391bd2e1d7442ab38fc98aff00d064d38 (diff)
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'python/src/mteval.pxi')
-rw-r--r--python/src/mteval.pxi21
1 files changed, 17 insertions, 4 deletions
diff --git a/python/src/mteval.pxi b/python/src/mteval.pxi
index 9afb6fe1..d90eb9a6 100644
--- a/python/src/mteval.pxi
+++ b/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:
@@ -73,7 +84,9 @@ cdef class CandidateSet:
def __len__(self):
return self.cs.size()
- def __getitem__(self, unsigned k):
+ def __getitem__(self,int k):
+ if not 0 <= k < self.cs.size():
+ raise IndexError('candidate set index out of range')
cdef Candidate candidate = Candidate()
candidate.candidate = &self.cs[0][k]
candidate.score = self.metric.ComputeScore(self.cs[0][k].eval_feats)