2011-10-06 15:00:48 +04:00
|
|
|
#include "MergeScorer.h"
|
2011-11-14 10:15:30 +04:00
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include "ScoreStats.h"
|
|
|
|
#include "TerScorer.h"
|
|
|
|
#include "BleuScorer.h"
|
|
|
|
#include "PerScorer.h"
|
|
|
|
#include "CderScorer.h"
|
|
|
|
|
2012-02-20 04:46:08 +04:00
|
|
|
#include "TER/tercalc.h"
|
|
|
|
#include "TER/terAlignment.h"
|
2011-10-06 15:00:48 +04:00
|
|
|
|
2012-05-10 02:51:05 +04:00
|
|
|
using namespace std;
|
2011-10-06 15:00:48 +04:00
|
|
|
using namespace TERCpp;
|
|
|
|
|
2012-06-30 23:23:45 +04:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
|
|
|
|
|
|
|
|
2011-11-12 05:28:08 +04:00
|
|
|
MergeScorer::MergeScorer(const string& config)
|
2012-03-10 12:12:34 +04:00
|
|
|
: StatisticsBasedScorer("MERGE", config) {}
|
|
|
|
|
2011-11-12 05:28:08 +04:00
|
|
|
MergeScorer::~MergeScorer() {}
|
2011-10-06 15:00:48 +04:00
|
|
|
|
|
|
|
void MergeScorer::setReferenceFiles(const vector<string>& referenceFiles)
|
|
|
|
{
|
2011-11-12 04:24:19 +04:00
|
|
|
throw runtime_error("MERGE Scorer can be used only in mert execution");
|
|
|
|
exit(0);
|
2011-10-06 15:00:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void MergeScorer::prepareStats(size_t sid, const string& text, ScoreStats& entry)
|
|
|
|
{
|
2011-11-12 04:24:19 +04:00
|
|
|
throw runtime_error("MergeScorer::prepareStats : MERGE Scorer can be used only in mert execution");
|
|
|
|
exit(0);
|
2011-10-06 15:00:48 +04:00
|
|
|
}
|
2011-11-12 04:24:19 +04:00
|
|
|
|
2011-10-06 15:00:48 +04:00
|
|
|
/*
|
|
|
|
float MergeScorer::calculateScore(const vector<int>& comps)
|
|
|
|
{
|
2011-11-12 04:24:19 +04:00
|
|
|
throw runtime_error("MergeScorer::calculateScore : MERGE Scorer can be used only in mert execution");
|
2011-10-06 15:00:48 +04:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
*/
|
2011-11-12 04:24:19 +04:00
|
|
|
|
2011-11-12 05:40:54 +04:00
|
|
|
float MergeScorer::calculateScore(const std::vector< int >& comps) const
|
2011-10-06 15:00:48 +04:00
|
|
|
{
|
2012-02-01 13:13:32 +04:00
|
|
|
float result = 0.0;
|
2011-11-12 04:24:19 +04:00
|
|
|
vector<int> vecLine;
|
|
|
|
vector<string> vecScorerType;
|
|
|
|
vector<float> weightsModifier;
|
2012-02-01 13:13:32 +04:00
|
|
|
int pos = 0;
|
|
|
|
int weightIncrement = 0;
|
|
|
|
string initfile = "merge.init";
|
2011-11-12 04:24:19 +04:00
|
|
|
string line;
|
|
|
|
ifstream opt(initfile.c_str());
|
2012-02-01 13:13:32 +04:00
|
|
|
float denom = 0.0;
|
|
|
|
|
2011-11-12 04:24:19 +04:00
|
|
|
if (opt.fail()) {
|
|
|
|
cerr<<"MergeScorer::calculateScore : could not open initfile: " << initfile << endl;
|
|
|
|
exit(3);
|
|
|
|
}
|
|
|
|
while (getline (opt, line)) {
|
|
|
|
vector<string> vecLine=stringToVector(line, " ");
|
2012-02-01 13:13:32 +04:00
|
|
|
if (vecLine.size() != 4) {
|
2011-11-12 04:24:19 +04:00
|
|
|
cerr<<"MergeScorer::calculateScore : Error in initfile: " << initfile << endl;
|
|
|
|
exit(4);
|
|
|
|
}
|
|
|
|
vecScorerType.push_back(vecLine.at(0));
|
|
|
|
weightsModifier.push_back(atof(vecLine.at(1).c_str()));
|
2012-02-01 13:13:32 +04:00
|
|
|
denom += abs(atof(vecLine.at(1).c_str()));
|
2011-11-12 04:24:19 +04:00
|
|
|
}
|
2012-02-01 13:13:32 +04:00
|
|
|
const int weights_modifier_size = static_cast<int>(weightsModifier.size());
|
|
|
|
for (weightIncrement = 0; weightIncrement < weights_modifier_size; weightIncrement++)
|
2011-11-12 04:24:19 +04:00
|
|
|
{
|
2012-02-01 13:13:32 +04:00
|
|
|
if (vecScorerType.at(weightIncrement).compare("BLEU") == 0)
|
2011-11-12 04:24:19 +04:00
|
|
|
{
|
2012-02-01 13:13:32 +04:00
|
|
|
BleuScorer* scorer01 = new BleuScorer("");
|
|
|
|
const float weight = weightsModifier.at(weightIncrement) / denom;
|
2011-11-12 04:24:19 +04:00
|
|
|
vecLine.clear();
|
2012-02-01 13:13:32 +04:00
|
|
|
const int num_scores = static_cast<int>(scorer01->NumberOfScores());
|
|
|
|
vecLine = subVector(comps, pos, pos + num_scores);
|
|
|
|
pos += num_scores;
|
|
|
|
result += weight * scorer01->calculateScore(vecLine);;
|
|
|
|
delete scorer01;
|
2011-11-12 04:24:19 +04:00
|
|
|
}
|
2012-02-01 13:13:32 +04:00
|
|
|
else if (vecScorerType.at(weightIncrement).compare("TER") == 0)
|
2011-11-12 04:24:19 +04:00
|
|
|
{
|
2012-02-01 13:13:32 +04:00
|
|
|
TerScorer* scorer02 = new TerScorer("");
|
|
|
|
const float weight = weightsModifier.at(weightIncrement) / denom;
|
2011-11-12 04:24:19 +04:00
|
|
|
vecLine.clear();
|
2012-02-01 13:13:32 +04:00
|
|
|
const int num_scores = static_cast<int>(scorer02->NumberOfScores());
|
|
|
|
vecLine = subVector(comps, pos, pos + num_scores);
|
|
|
|
pos += num_scores;
|
|
|
|
result += weight * scorer02->calculateScore(vecLine);
|
|
|
|
delete scorer02;
|
2011-11-12 04:24:19 +04:00
|
|
|
}
|
2012-02-01 13:13:32 +04:00
|
|
|
else if (vecScorerType.at(weightIncrement).compare("PER") == 0)
|
2011-11-12 04:24:19 +04:00
|
|
|
{
|
2012-02-01 13:13:32 +04:00
|
|
|
PerScorer* scorer03 = new PerScorer("");
|
|
|
|
const float weight = weightsModifier.at(weightIncrement) / denom;
|
2011-11-12 04:24:19 +04:00
|
|
|
vecLine.clear();
|
2012-02-01 13:13:32 +04:00
|
|
|
const int num_scores = static_cast<int>(scorer03->NumberOfScores());
|
|
|
|
vecLine = subVector(comps, pos, pos + num_scores);
|
|
|
|
pos += num_scores;
|
|
|
|
result += weight * scorer03->calculateScore(vecLine);
|
|
|
|
delete scorer03;
|
2011-11-12 04:24:19 +04:00
|
|
|
}
|
2012-02-01 13:13:32 +04:00
|
|
|
else if (vecScorerType.at(weightIncrement).compare("CER") == 0)
|
2011-11-12 04:24:19 +04:00
|
|
|
{
|
2012-02-01 13:13:32 +04:00
|
|
|
CderScorer* scorer04 = new CderScorer("");
|
|
|
|
const float weight = weightsModifier.at(weightIncrement) / denom;
|
2011-11-12 04:24:19 +04:00
|
|
|
vecLine.clear();
|
2012-02-01 13:13:32 +04:00
|
|
|
const int num_scores = static_cast<int>(scorer04->NumberOfScores());
|
|
|
|
vecLine = subVector(comps, pos, pos + num_scores);
|
|
|
|
pos += num_scores;
|
|
|
|
result += weight * scorer04->calculateScore(vecLine);
|
|
|
|
delete scorer04;
|
2011-11-12 04:24:19 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
throw runtime_error("MergeScorer::calculateScore : Scorer unknown");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2011-10-06 15:00:48 +04:00
|
|
|
}
|
2012-06-30 23:23:45 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|