diff options
author | Kenneth Heafield <github@kheafield.com> | 2012-08-03 07:46:54 -0400 |
---|---|---|
committer | Kenneth Heafield <github@kheafield.com> | 2012-08-03 07:46:54 -0400 |
commit | be1ab0a8937f9c5668ea5e6c31b798e87672e55e (patch) | |
tree | a13aad60ab6cced213401bce6a38ac885ba171ba /python/src/py_scorer.h | |
parent | e5d6f4ae41009c26978ecd62668501af9762b0bc (diff) | |
parent | 9fe0219562e5db25171cce8776381600ff9a5649 (diff) |
Merge branch 'master' of github.com:redpony/cdec
Diffstat (limited to 'python/src/py_scorer.h')
-rw-r--r-- | python/src/py_scorer.h | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/python/src/py_scorer.h b/python/src/py_scorer.h new file mode 100644 index 00000000..22dc9fee --- /dev/null +++ b/python/src/py_scorer.h @@ -0,0 +1,44 @@ +#include "ns.h" +#include "tdict.h" + +typedef float (*MetricScoreCallback)(void*, SufficientStats* stats); +typedef void (*MetricStatsCallback)(void*, + std::string *hyp, + std::vector<std::string> *refs, + SufficientStats* out); + +struct PythonEvaluationMetric : public EvaluationMetric { + + PythonEvaluationMetric(const std::string& id) : EvaluationMetric(id) {} + + static EvaluationMetric* Instance(const std::string& id, + void* obj, + MetricStatsCallback statscb, + MetricScoreCallback scorecb) { + PythonEvaluationMetric* metric = new PythonEvaluationMetric(id); + metric->pymetric = obj; + metric->_compute_score = scorecb; + metric->_compute_sufficient_stats = statscb; + return metric; + } + + float ComputeScore(const SufficientStats& stats) const { + SufficientStats stats_(stats); + return _compute_score(pymetric, &stats_); + } + + void ComputeSufficientStatistics(const std::vector<WordID>& hyp, + const std::vector<std::vector<WordID> >& refs, + SufficientStats* out) const { + std::string hyp_(TD::GetString(hyp)); + std::vector<std::string> refs_; + for(unsigned i = 0; i < refs.size(); ++i) { + refs_.push_back(TD::GetString(refs[i])); + } + _compute_sufficient_stats(pymetric, &hyp_, &refs_, out); + } + + void* pymetric; + MetricStatsCallback _compute_sufficient_stats; + MetricScoreCallback _compute_score; +}; |