Improved reporting of nbest scores via server interface.

This commit is contained in:
Ulrich Germann 2015-11-01 14:51:33 +00:00
parent ff1977c29e
commit e9b1c6019b
7 changed files with 79 additions and 9 deletions

View File

@ -1,3 +1,4 @@
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*-
/*
Moses - statistical machine translation system
Copyright (C) 2005-2015 University of Edinburgh

View File

@ -1,4 +1,4 @@
// $Id$
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*-
#include <vector>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/foreach.hpp>
@ -280,7 +280,9 @@ void ScoreComponentCollection::ZeroDenseFeatures(const FeatureFunction* sp)
}
//! get subset of scores that belong to a certain sparse ScoreProducer
FVector ScoreComponentCollection::GetVectorForProducer(const FeatureFunction* sp) const
FVector
ScoreComponentCollection::
GetVectorForProducer(const FeatureFunction* sp) const
{
FVector fv(s_denseVectorSize);
std::string prefix = sp->GetScoreProducerDescription() + FName::SEP;
@ -310,14 +312,16 @@ ScoreComponentCollection::
OutputAllFeatureScores(std::ostream &out, bool with_labels) const
{
std::string lastName = "";
const vector<const StatefulFeatureFunction*>& sff = StatefulFeatureFunction::GetStatefulFeatureFunctions();
const vector<const StatefulFeatureFunction*>& sff
= StatefulFeatureFunction::GetStatefulFeatureFunctions();
for( size_t i=0; i<sff.size(); i++ ) {
const StatefulFeatureFunction *ff = sff[i];
if (ff->IsTuneable()) {
OutputFeatureScores(out, ff, lastName, with_labels);
}
}
const vector<const StatelessFeatureFunction*>& slf = StatelessFeatureFunction::GetStatelessFeatureFunctions();
const vector<const StatelessFeatureFunction*>& slf
= StatelessFeatureFunction::GetStatelessFeatureFunctions();
for( size_t i=0; i<slf.size(); i++ ) {
const StatelessFeatureFunction *ff = slf[i];
if (ff->IsTuneable()) {

View File

@ -1,6 +1,4 @@
// -*- c++ -*-
// $Id$
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*-
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2006 University of Edinburgh
@ -90,7 +88,7 @@ class ScoreComponentCollection
{
friend std::ostream& operator<<(std::ostream& os, const ScoreComponentCollection& rhs);
friend void swap(ScoreComponentCollection &first, ScoreComponentCollection &second);
private:
FVector m_scores;

View File

@ -1,4 +1,4 @@
// -*- c++ -*-
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*-
#pragma once
#include <boost/smart_ptr/shared_ptr.hpp>

View File

@ -0,0 +1,48 @@
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*-
#ifdef HAVE_XMLRPC_C
#include "PackScores.h"
#include "moses/FF/StatefulFeatureFunction.h"
#include "moses/FF/StatelessFeatureFunction.h"
#include <boost/foreach.hpp>
namespace Moses {
void
PackScores(FeatureFunction const& ff, FVector const& S,
std::map<std::string, xmlrpc_c::value>& M)
{
std::vector<xmlrpc_c::value> v;
size_t N = ff.GetNumScoreComponents();
std::vector<xmlrpc_c::value> dense;
dense.reserve(N);
size_t o = ff.GetIndex();
for (size_t i = 0; i < N; ++i)
if (ff.IsTuneableComponent(i))
dense.push_back(xmlrpc_c::value_double(S[o+i]));
v.push_back(xmlrpc_c::value_array(dense));
std::map<std::string,xmlrpc_c::value> sparse;
typedef FVector::FNVmap::const_iterator iter;
for(iter m = S.cbegin(); m != S.cend(); ++m)
sparse[m->first.name()] = xmlrpc_c::value_double(m->second);
v.push_back(xmlrpc_c::value_struct(sparse));
M[ff.GetScoreProducerDescription()] = xmlrpc_c::value_array(v);
}
xmlrpc_c::value
PackScores(ScoreComponentCollection const& S)
{
std::map<std::string, xmlrpc_c::value> M;
typedef StatefulFeatureFunction SFFF;
typedef StatelessFeatureFunction SLFF;
BOOST_FOREACH(SFFF const* ff, SFFF::GetStatefulFeatureFunctions())
if (ff->IsTuneable())
PackScores(*ff, S.GetScoresVector(), M);
BOOST_FOREACH(SLFF const* ff, SLFF::GetStatelessFeatureFunctions())
if (ff->IsTuneable())
PackScores(*ff, S.GetScoresVector(), M);
return xmlrpc_c::value_struct(M);
}
}
#endif

16
moses/server/PackScores.h Normal file
View File

@ -0,0 +1,16 @@
// -*- mode: c++; indent-tabs-mode: nil; tab-width:2 -*-
#ifdef HAVE_XMLRPC_C
#pragma once
#include <xmlrpc-c/base.hpp>
#include "moses/FF/FeatureFunction.h"
#include "moses/ScoreComponentCollection.h"
namespace Moses {
xmlrpc_c::value
PackScores(ScoreComponentCollection const& S);
}
#endif

View File

@ -1,4 +1,5 @@
#include "TranslationRequest.h"
#include "PackScores.h"
#include "moses/ContextScope.h"
#include <boost/foreach.hpp>
#include "moses/Util.h"
@ -188,6 +189,8 @@ outputNBest(const Manager& manager, map<string, xmlrpc_c::value>& retData)
bool with_labels = m_options.nbest.include_feature_labels;
path->GetScoreBreakdown()->OutputAllFeatureScores(buf, with_labels);
nBestXmlItem["fvals"] = xmlrpc_c::value_string(buf.str());
nBestXmlItem["scores"] = PackScores(*path->GetScoreBreakdown());
}
// weighted score