2015-10-23 22:53:36 +03:00
|
|
|
/*
|
|
|
|
* Scores.cpp
|
|
|
|
*
|
|
|
|
* Created on: 23 Oct 2015
|
|
|
|
* Author: hieu
|
|
|
|
*/
|
|
|
|
|
2015-10-24 04:02:50 +03:00
|
|
|
#include <vector>
|
2015-10-23 22:53:36 +03:00
|
|
|
#include <cstddef>
|
2015-10-27 00:11:47 +03:00
|
|
|
#include <stdio.h>
|
2015-10-23 22:53:36 +03:00
|
|
|
#include "Scores.h"
|
2015-10-24 04:02:50 +03:00
|
|
|
#include "FeatureFunction.h"
|
2015-10-28 19:33:08 +03:00
|
|
|
#include "FeatureFunctions.h"
|
2015-10-24 14:39:15 +03:00
|
|
|
#include "Util.h"
|
2015-10-24 16:36:30 +03:00
|
|
|
#include "Weights.h"
|
2015-10-26 00:20:55 +03:00
|
|
|
#include "System.h"
|
2015-10-24 04:02:50 +03:00
|
|
|
#include "moses/Util.h"
|
|
|
|
|
|
|
|
using namespace std;
|
2015-10-23 22:53:36 +03:00
|
|
|
|
2015-10-28 19:11:12 +03:00
|
|
|
Scores::Scores(MemPool &pool, size_t numScores)
|
2015-10-24 16:36:30 +03:00
|
|
|
:m_total(0)
|
2015-10-23 22:53:36 +03:00
|
|
|
{
|
2015-10-24 05:32:30 +03:00
|
|
|
m_scores = new (pool.Allocate<SCORE>(numScores)) SCORE[numScores];
|
2015-10-24 14:39:15 +03:00
|
|
|
Init<SCORE>(m_scores, numScores, 0);
|
2015-10-23 22:53:36 +03:00
|
|
|
}
|
|
|
|
|
2015-10-29 02:26:17 +03:00
|
|
|
Scores::Scores(MemPool &pool,
|
|
|
|
size_t numScores,
|
|
|
|
const Scores &origScores)
|
2015-10-27 00:11:47 +03:00
|
|
|
:m_total(origScores.m_total)
|
|
|
|
{
|
|
|
|
m_scores = new (pool.Allocate<SCORE>(numScores)) SCORE[numScores];
|
|
|
|
memcpy(m_scores, origScores.m_scores, sizeof(SCORE) * numScores);
|
|
|
|
}
|
|
|
|
|
2015-10-23 22:53:36 +03:00
|
|
|
Scores::~Scores() {
|
2015-10-27 00:11:47 +03:00
|
|
|
|
2015-10-23 22:53:36 +03:00
|
|
|
}
|
|
|
|
|
2015-10-29 02:26:17 +03:00
|
|
|
void Scores::PlusEquals(const System &system,
|
|
|
|
const FeatureFunction &featureFunction,
|
|
|
|
const std::vector<SCORE> &scores)
|
2015-10-24 04:02:50 +03:00
|
|
|
{
|
2015-10-24 16:36:30 +03:00
|
|
|
assert(scores.size() == featureFunction.GetNumScores());
|
|
|
|
|
2015-10-26 00:20:55 +03:00
|
|
|
const Weights &weights = system.GetWeights();
|
2015-10-24 16:36:30 +03:00
|
|
|
|
2015-10-24 04:02:50 +03:00
|
|
|
size_t ffStartInd = featureFunction.GetStartInd();
|
2015-10-24 16:36:30 +03:00
|
|
|
for (size_t i = 0; i < scores.size(); ++i) {
|
|
|
|
SCORE incrScore = scores[i];
|
|
|
|
m_scores[ffStartInd + i] += incrScore;
|
|
|
|
|
2015-10-30 18:35:06 +03:00
|
|
|
//cerr << "ffStartInd=" << ffStartInd << " " << i << endl;
|
2015-10-24 16:36:30 +03:00
|
|
|
SCORE weight = weights[ffStartInd + i];
|
|
|
|
m_total += incrScore * weight;
|
2015-10-24 04:02:50 +03:00
|
|
|
}
|
|
|
|
}
|
2015-10-24 16:36:30 +03:00
|
|
|
|
2015-10-29 02:26:17 +03:00
|
|
|
void Scores::PlusEquals(const System &system,
|
|
|
|
const FeatureFunction &featureFunction,
|
|
|
|
const SCORE &score)
|
|
|
|
{
|
2015-10-29 04:08:03 +03:00
|
|
|
assert(featureFunction.GetNumScores() == 1);
|
2015-10-29 02:26:17 +03:00
|
|
|
|
|
|
|
const Weights &weights = system.GetWeights();
|
|
|
|
|
|
|
|
size_t ffStartInd = featureFunction.GetStartInd();
|
|
|
|
m_scores[ffStartInd] += score;
|
|
|
|
|
|
|
|
SCORE weight = weights[ffStartInd];
|
|
|
|
m_total += score * weight;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-29 21:15:12 +03:00
|
|
|
void Scores::PlusEquals(const System &system, const Scores &other)
|
2015-10-27 00:11:47 +03:00
|
|
|
{
|
2015-10-27 20:00:38 +03:00
|
|
|
size_t numScores = system.GetFeatureFunctions().GetNumScores();
|
2015-10-27 00:11:47 +03:00
|
|
|
for (size_t i = 0; i < numScores; ++i) {
|
2015-10-29 21:15:12 +03:00
|
|
|
m_scores[i] += other.m_scores[i];
|
2015-10-27 00:11:47 +03:00
|
|
|
}
|
2015-10-29 21:15:12 +03:00
|
|
|
m_total += other.m_total;
|
2015-10-27 00:11:47 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-10-29 20:21:54 +03:00
|
|
|
void Scores::CreateFromString(const std::string &str,
|
|
|
|
const FeatureFunction &featureFunction,
|
|
|
|
const System &system,
|
|
|
|
bool transformScores)
|
2015-10-24 16:36:30 +03:00
|
|
|
{
|
|
|
|
vector<SCORE> scores = Moses::Tokenize<SCORE>(str);
|
2015-10-29 20:21:54 +03:00
|
|
|
if (transformScores) {
|
|
|
|
std::transform(scores.begin(), scores.end(), scores.begin(), Moses::TransformScore);
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:58:02 +03:00
|
|
|
/*
|
2015-10-29 20:21:54 +03:00
|
|
|
std::copy(scores.begin(),scores.end(),
|
|
|
|
std::ostream_iterator<SCORE>(cerr," "));
|
2015-10-30 18:58:02 +03:00
|
|
|
*/
|
2015-10-29 20:21:54 +03:00
|
|
|
|
2015-10-29 02:26:17 +03:00
|
|
|
PlusEquals(system, featureFunction, scores);
|
2015-10-24 16:36:30 +03:00
|
|
|
}
|
2015-10-26 19:32:47 +03:00
|
|
|
|
2015-10-29 18:29:14 +03:00
|
|
|
void Scores::Debug(std::ostream &out, const FeatureFunctions &ffs) const
|
2015-10-28 19:33:08 +03:00
|
|
|
{
|
|
|
|
out << m_total << " = ";
|
|
|
|
size_t numScores = ffs.GetNumScores();
|
|
|
|
for (size_t i = 0; i < numScores; ++i) {
|
|
|
|
out << m_scores[i] << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-26 19:32:47 +03:00
|
|
|
std::ostream& operator<<(std::ostream &out, const Scores &obj)
|
|
|
|
{
|
|
|
|
out << obj.m_total;
|
|
|
|
|
|
|
|
if (obj.m_scores) {
|
|
|
|
// don't know num of scores
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|