2012-02-26 21:53:08 +04:00
|
|
|
#include "InterpolatedScorer.h"
|
2012-03-10 12:12:34 +04:00
|
|
|
#include "ScorerFactory.h"
|
2012-02-26 21:53:08 +04:00
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2012-12-06 20:39:22 +04:00
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
// TODO: This is too long. Consider creating a function for
|
|
|
|
// initialization such as Init().
|
|
|
|
InterpolatedScorer::InterpolatedScorer(const string& name, const string& config)
|
2013-05-29 21:16:15 +04:00
|
|
|
: Scorer(name,config)
|
2012-02-26 21:53:08 +04:00
|
|
|
{
|
|
|
|
// name would be: HAMMING,BLEU or similar
|
|
|
|
string scorers = name;
|
|
|
|
while (scorers.length() > 0) {
|
|
|
|
string scorertype = "";
|
2012-03-10 12:12:34 +04:00
|
|
|
getNextPound(scorers, scorertype,",");
|
|
|
|
Scorer *scorer = ScorerFactory::getScorer(scorertype,config);
|
|
|
|
m_scorers.push_back(scorer);
|
2012-02-26 21:53:08 +04:00
|
|
|
}
|
2012-03-10 12:12:34 +04:00
|
|
|
if (m_scorers.size() == 0) {
|
2012-02-26 21:53:08 +04:00
|
|
|
throw runtime_error("There are no scorers");
|
|
|
|
}
|
2012-03-10 12:12:34 +04:00
|
|
|
cerr << "Number of scorers: " << m_scorers.size() << endl;
|
2012-02-26 21:53:08 +04:00
|
|
|
|
|
|
|
//TODO debug this
|
|
|
|
string wtype = getConfig("weights","");
|
|
|
|
//Default weights set to uniform ie. if two weights 0.5 each
|
|
|
|
//weights should add to 1
|
|
|
|
if (wtype.length() == 0) {
|
2012-03-10 12:12:34 +04:00
|
|
|
float weight = 1.0 / m_scorers.size() ;
|
2012-02-26 21:53:08 +04:00
|
|
|
//cout << " Default weights:" << weight << endl;
|
2012-03-10 12:12:34 +04:00
|
|
|
for (size_t i = 0; i < m_scorers.size(); i ++) {
|
|
|
|
m_scorer_weights.push_back(weight);
|
2012-02-26 21:53:08 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
float tot=0;
|
|
|
|
//cout << "Defined weights:" << endl;
|
|
|
|
while (wtype.length() > 0) {
|
|
|
|
string scoreweight = "";
|
|
|
|
getNextPound(wtype,scoreweight,"+");
|
|
|
|
float weight = atof(scoreweight.c_str());
|
2012-03-10 12:12:34 +04:00
|
|
|
m_scorer_weights.push_back(weight);
|
2012-02-26 21:53:08 +04:00
|
|
|
tot += weight;
|
|
|
|
//cout << " :" << weight ;
|
|
|
|
}
|
|
|
|
//cout << endl;
|
2012-03-10 12:12:34 +04:00
|
|
|
if (tot != float(1)) { // TODO: fix this checking in terms of readability.
|
|
|
|
for (vector<float>::iterator it = m_scorer_weights.begin();
|
|
|
|
it != m_scorer_weights.end(); ++it) {
|
2012-02-26 21:53:08 +04:00
|
|
|
*it /= tot;
|
|
|
|
}
|
|
|
|
}
|
2012-02-27 09:30:37 +04:00
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
if (m_scorers.size() != m_scorer_weights.size()) {
|
2012-02-27 01:52:20 +04:00
|
|
|
throw runtime_error("The number of weights does not equal the number of scorers!");
|
|
|
|
}
|
2012-02-26 21:53:08 +04:00
|
|
|
}
|
|
|
|
cerr << "The weights for the interpolated scorers are: " << endl;
|
2012-03-10 12:12:34 +04:00
|
|
|
for (vector<float>::iterator it = m_scorer_weights.begin(); it < m_scorer_weights.end(); it++) {
|
2012-02-26 21:53:08 +04:00
|
|
|
cerr << *it << " " ;
|
|
|
|
}
|
2012-02-27 01:23:57 +04:00
|
|
|
cerr <<endl;
|
2012-02-26 21:53:08 +04:00
|
|
|
}
|
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
bool InterpolatedScorer::useAlignment() const
|
|
|
|
{
|
2012-07-10 12:25:00 +04:00
|
|
|
//cout << "InterpolatedScorer::useAlignment" << endl;
|
|
|
|
for (vector<Scorer*>::const_iterator itsc = m_scorers.begin(); itsc < m_scorers.end(); itsc++) {
|
|
|
|
if ((*itsc)->useAlignment()) {
|
|
|
|
//cout <<"InterpolatedScorer::useAlignment Returning true"<<endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2012-02-26 21:53:08 +04:00
|
|
|
void InterpolatedScorer::setScoreData(ScoreData* data)
|
|
|
|
{
|
|
|
|
size_t last = 0;
|
|
|
|
m_score_data = data;
|
2012-03-10 12:12:34 +04:00
|
|
|
for (ScopedVector<Scorer>::iterator itsc = m_scorers.begin();
|
|
|
|
itsc != m_scorers.end(); ++itsc) {
|
2012-02-26 21:53:08 +04:00
|
|
|
int numScoresScorer = (*itsc)->NumberOfScores();
|
2012-03-10 12:28:38 +04:00
|
|
|
ScoreData* newData =new ScoreData(*itsc);
|
2012-02-26 21:53:08 +04:00
|
|
|
for (size_t i = 0; i < data->size(); i++) {
|
|
|
|
ScoreArray scoreArray = data->get(i);
|
|
|
|
ScoreArray newScoreArray;
|
|
|
|
size_t numNBest = scoreArray.size();
|
|
|
|
//cout << " Datasize " << data->size() << " NumNBest " << numNBest << endl ;
|
|
|
|
for (size_t j = 0; j < numNBest ; j++) {
|
|
|
|
ScoreStats scoreStats = data->get(i, j);
|
|
|
|
//cout << "Scorestats " << scoreStats << " i " << i << " j " << j << endl;
|
|
|
|
ScoreStats newScoreStats;
|
|
|
|
for (size_t k = last; k < size_t(numScoresScorer + last); k++) {
|
|
|
|
ScoreStatsType score = scoreStats.get(k);
|
|
|
|
newScoreStats.add(score);
|
|
|
|
}
|
|
|
|
//cout << " last " << last << " NumScores " << numScoresScorer << "newScorestats " << newScoreStats << endl;
|
|
|
|
newScoreArray.add(newScoreStats);
|
|
|
|
}
|
2012-12-06 20:39:22 +04:00
|
|
|
newScoreArray.setIndex(i);
|
2012-02-26 21:53:08 +04:00
|
|
|
newData->add(newScoreArray);
|
|
|
|
}
|
|
|
|
//newData->dump();
|
2012-02-27 09:30:37 +04:00
|
|
|
|
|
|
|
// NOTE: This class takes the ownership of the heap allocated
|
|
|
|
// ScoreData objects to avoid the memory leak issues.
|
|
|
|
m_scorers_score_data.push_back(newData);
|
|
|
|
|
2012-02-26 21:53:08 +04:00
|
|
|
(*itsc)->setScoreData(newData);
|
|
|
|
last += numScoresScorer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** The interpolated scorer calls a vector of scorers and combines them with
|
|
|
|
weights **/
|
|
|
|
void InterpolatedScorer::score(const candidates_t& candidates, const diffs_t& diffs,
|
2012-02-27 09:30:37 +04:00
|
|
|
statscores_t& scores) const
|
2012-02-26 21:53:08 +04:00
|
|
|
{
|
|
|
|
//cout << "*******InterpolatedScorer::score" << endl;
|
|
|
|
size_t scorerNum = 0;
|
2012-03-10 12:12:34 +04:00
|
|
|
for (ScopedVector<Scorer>::const_iterator itsc = m_scorers.begin();
|
|
|
|
itsc != m_scorers.end(); ++itsc) {
|
2012-02-26 21:53:08 +04:00
|
|
|
//int numScores = (*itsc)->NumberOfScores();
|
|
|
|
statscores_t tscores;
|
|
|
|
(*itsc)->score(candidates,diffs,tscores);
|
|
|
|
size_t inc = 0;
|
2012-03-10 12:12:34 +04:00
|
|
|
for (statscores_t::iterator itstatsc = tscores.begin();
|
|
|
|
itstatsc != tscores.end(); ++itstatsc) {
|
2012-02-26 21:53:08 +04:00
|
|
|
//cout << "Scores " << (*itstatsc) << endl;
|
2012-03-10 12:12:34 +04:00
|
|
|
float weight = m_scorer_weights[scorerNum];
|
2012-02-26 21:53:08 +04:00
|
|
|
if (weight == 0) {
|
|
|
|
stringstream msg;
|
|
|
|
msg << "No weights for scorer" << scorerNum ;
|
|
|
|
throw runtime_error(msg.str());
|
|
|
|
}
|
|
|
|
if (scorerNum == 0) {
|
|
|
|
scores.push_back(weight * (*itstatsc));
|
|
|
|
} else {
|
|
|
|
scores[inc] += weight * (*itstatsc);
|
|
|
|
}
|
|
|
|
//cout << "Scorer:" << scorerNum << " scoreNum:" << inc << " score: " << (*itstatsc) << " weight:" << weight << endl;
|
|
|
|
inc++;
|
|
|
|
|
|
|
|
}
|
|
|
|
scorerNum++;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-17 17:14:11 +04:00
|
|
|
/** Interpolated scorer gets a vector of sufficient statistics, calls all scorers with corresponding statistics,
|
|
|
|
and combines them with weights **/
|
|
|
|
float InterpolatedScorer::calculateScore(const std::vector<ScoreStatsType>& totals) const
|
|
|
|
{
|
|
|
|
size_t scorerNum = 0;
|
|
|
|
size_t last = 0;
|
|
|
|
float score = 0;
|
|
|
|
for (ScopedVector<Scorer>::const_iterator itsc = m_scorers.begin();
|
|
|
|
itsc != m_scorers.end(); ++itsc) {
|
|
|
|
int numScoresScorer = (*itsc)->NumberOfScores();
|
|
|
|
std::vector<ScoreStatsType> totals_scorer(totals.begin()+last, totals.begin()+last+numScoresScorer);
|
|
|
|
score += (*itsc)->calculateScore(totals_scorer) * m_scorer_weights[scorerNum];
|
|
|
|
last += numScoresScorer;
|
|
|
|
scorerNum++;
|
|
|
|
}
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float InterpolatedScorer::getReferenceLength(const std::vector<ScoreStatsType>& totals) const
|
|
|
|
{
|
2015-01-14 14:07:42 +03:00
|
|
|
size_t scorerNum = 0;
|
|
|
|
size_t last = 0;
|
|
|
|
float refLen = 0;
|
|
|
|
for (ScopedVector<Scorer>::const_iterator itsc = m_scorers.begin();
|
|
|
|
itsc != m_scorers.end(); ++itsc) {
|
|
|
|
int numScoresScorer = (*itsc)->NumberOfScores();
|
|
|
|
std::vector<ScoreStatsType> totals_scorer(totals.begin()+last, totals.begin()+last+numScoresScorer);
|
|
|
|
refLen += (*itsc)->getReferenceLength(totals_scorer) * m_scorer_weights[scorerNum];
|
|
|
|
last += numScoresScorer;
|
|
|
|
scorerNum++;
|
2014-09-17 17:14:11 +04:00
|
|
|
}
|
2015-01-14 14:07:42 +03:00
|
|
|
return refLen;
|
|
|
|
}
|
2014-09-17 17:14:11 +04:00
|
|
|
|
2012-02-26 21:53:08 +04:00
|
|
|
void InterpolatedScorer::setReferenceFiles(const vector<string>& referenceFiles)
|
|
|
|
{
|
2012-03-10 12:12:34 +04:00
|
|
|
for (ScopedVector<Scorer>::iterator itsc = m_scorers.begin();
|
|
|
|
itsc != m_scorers.end(); ++itsc) {
|
2012-02-26 21:53:08 +04:00
|
|
|
(*itsc)->setReferenceFiles(referenceFiles);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void InterpolatedScorer::prepareStats(size_t sid, const string& text, ScoreStats& entry)
|
|
|
|
{
|
|
|
|
stringstream buff;
|
2012-07-10 12:25:00 +04:00
|
|
|
string align = text;
|
2013-03-20 02:08:28 +04:00
|
|
|
string sentence = text;
|
2012-07-10 12:25:00 +04:00
|
|
|
size_t alignmentData = text.find("|||");
|
|
|
|
//Get sentence and alignment parts
|
|
|
|
if(alignmentData != string::npos) {
|
|
|
|
getNextPound(align,sentence, "|||");
|
|
|
|
}
|
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
int i = 0;
|
2012-07-10 12:25:00 +04:00
|
|
|
for (ScopedVector<Scorer>::iterator itsc = m_scorers.begin(); itsc != m_scorers.end(); ++itsc) {
|
2012-02-26 21:53:08 +04:00
|
|
|
ScoreStats tempEntry;
|
2012-07-10 12:25:00 +04:00
|
|
|
if ((*itsc)->useAlignment()) {
|
|
|
|
(*itsc)->prepareStats(sid, text, tempEntry);
|
2013-05-29 21:16:15 +04:00
|
|
|
} else {
|
2012-07-10 12:25:00 +04:00
|
|
|
(*itsc)->prepareStats(sid, sentence, tempEntry);
|
|
|
|
}
|
2012-02-26 21:53:08 +04:00
|
|
|
if (i > 0) buff << " ";
|
|
|
|
buff << tempEntry;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
//cout << " Scores for interpolated: " << buff << endl;
|
|
|
|
string str = buff.str();
|
|
|
|
entry.set(str);
|
|
|
|
}
|
2012-02-28 05:27:23 +04:00
|
|
|
|
|
|
|
void InterpolatedScorer::setFactors(const string& factors)
|
|
|
|
{
|
|
|
|
if (factors.empty()) return;
|
|
|
|
|
|
|
|
vector<string> fsplit;
|
|
|
|
split(factors, ',', fsplit);
|
|
|
|
|
2012-03-10 12:12:34 +04:00
|
|
|
if (fsplit.size() != m_scorers.size())
|
|
|
|
throw runtime_error("Number of factor specifications does not equal number of interpolated scorers.");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_scorers.size(); ++i) {
|
|
|
|
m_scorers[i]->setFactors(fsplit[i]);
|
2012-02-28 05:27:23 +04:00
|
|
|
}
|
|
|
|
}
|
2012-05-09 21:21:41 +04:00
|
|
|
|
|
|
|
void InterpolatedScorer::setFilter(const string& filterCommand)
|
|
|
|
{
|
2013-05-29 21:16:15 +04:00
|
|
|
if (filterCommand.empty()) return;
|
2012-05-12 18:11:33 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
vector<string> csplit;
|
|
|
|
split(filterCommand, ',', csplit);
|
2012-05-12 18:11:33 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
if (csplit.size() != m_scorers.size())
|
|
|
|
throw runtime_error("Number of command specifications does not equal number of interpolated scorers.");
|
2012-05-12 18:11:33 +04:00
|
|
|
|
2013-05-29 21:16:15 +04:00
|
|
|
for (size_t i = 0; i < m_scorers.size(); ++i) {
|
|
|
|
m_scorers[i]->setFilter(csplit[i]);
|
|
|
|
}
|
2012-05-09 21:21:41 +04:00
|
|
|
}
|
2012-06-30 23:23:45 +04:00
|
|
|
|
|
|
|
}
|