mosesdecoder/contrib/other-builds/moses2/Scores.cpp

287 lines
6.7 KiB
C++
Raw Normal View History

2015-10-23 22:53:36 +03:00
/*
* Scores.cpp
*
* Created on: 23 Oct 2015
* Author: hieu
*/
2015-12-16 19:53:00 +03:00
#include <boost/foreach.hpp>
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 16:36:30 +03:00
#include "Weights.h"
2015-10-26 00:20:55 +03:00
#include "System.h"
2015-11-03 16:24:39 +03:00
#include "FF/FeatureFunction.h"
#include "FF/FeatureFunctions.h"
2015-11-11 19:23:49 +03:00
#include "legacy/Util2.h"
2015-10-24 04:02:50 +03:00
using namespace std;
2015-10-23 22:53:36 +03:00
2015-12-10 23:49:30 +03:00
namespace Moses2
{
2015-12-01 02:03:33 +03:00
Scores::Scores(const System &system, 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-12-01 02:03:33 +03:00
if (system.nbestSize) {
m_scores = new (pool.Allocate<SCORE>(numScores)) SCORE[numScores];
Init<SCORE>(m_scores, numScores, 0);
2015-12-01 02:03:33 +03:00
}
else {
m_scores = NULL;
2015-12-01 02:03:33 +03:00
}
2015-10-23 22:53:36 +03:00
}
Scores::Scores(const System &system,
MemPool &pool,
2015-10-29 02:26:17 +03:00
size_t numScores,
const Scores &origScores)
2015-10-27 00:11:47 +03:00
:m_total(origScores.m_total)
{
if (system.nbestSize) {
m_scores = new (pool.Allocate<SCORE>(numScores)) SCORE[numScores];
memcpy(m_scores, origScores.m_scores, sizeof(SCORE) * numScores);
2015-12-01 02:03:33 +03:00
}
else {
m_scores = NULL;
2015-12-01 02:03:33 +03:00
}
2015-10-27 00:11:47 +03:00
}
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
}
void Scores::Reset(const System &system)
2015-11-02 17:06:03 +03:00
{
if (system.nbestSize) {
size_t numScores = system.featureFunctions.GetNumScores();
Init<SCORE>(m_scores, numScores, 0);
2015-11-10 03:07:55 +03:00
}
2015-11-02 17:06:03 +03:00
m_total = 0;
}
2015-11-04 17:54:20 +03:00
void Scores::PlusEquals(const System &system,
const FeatureFunction &featureFunction,
const SCORE &score)
{
assert(featureFunction.GetNumScores() == 1);
2015-11-05 14:19:37 +03:00
const Weights &weights = system.weights;
2015-11-04 17:54:20 +03:00
size_t ffStartInd = featureFunction.GetStartInd();
if (system.nbestSize) {
m_scores[ffStartInd] += score;
2015-11-10 03:02:22 +03:00
}
2015-11-04 17:54:20 +03:00
SCORE weight = weights[ffStartInd];
m_total += score * weight;
2015-12-18 00:43:02 +03:00
}
void Scores::PlusEquals(const System &system,
const FeatureFunction &featureFunction,
const SCORE &score,
size_t offset)
{
assert(offset < featureFunction.GetNumScores());
2015-11-04 17:54:20 +03:00
2015-12-18 00:43:02 +03:00
const Weights &weights = system.weights;
size_t ffStartInd = featureFunction.GetStartInd();
if (system.nbestSize) {
m_scores[ffStartInd + offset] += score;
2015-12-18 00:43:02 +03:00
}
SCORE weight = weights[ffStartInd + offset];
m_total += score * weight;
2015-11-04 17:54:20 +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-11-05 14:19:37 +03:00
const Weights &weights = system.weights;
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];
if (system.nbestSize) {
m_scores[ffStartInd + i] += incrScore;
2015-11-10 03:02:22 +03:00
}
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-12-11 00:21:52 +03:00
void Scores::PlusEquals(const System &system,
const FeatureFunction &featureFunction,
const Vector<SCORE> &scores)
{
assert(scores.size() == featureFunction.GetNumScores());
const Weights &weights = system.weights;
size_t ffStartInd = featureFunction.GetStartInd();
for (size_t i = 0; i < scores.size(); ++i) {
SCORE incrScore = scores[i];
if (system.nbestSize) {
m_scores[ffStartInd + i] += incrScore;
2015-12-11 00:21:52 +03:00
}
//cerr << "ffStartInd=" << ffStartInd << " " << i << endl;
SCORE weight = weights[ffStartInd + i];
m_total += incrScore * weight;
}
}
2015-12-11 00:47:41 +03:00
void Scores::PlusEquals(const System &system,
const FeatureFunction &featureFunction,
SCORE scores[])
{
//assert(scores.size() == featureFunction.GetNumScores());
const Weights &weights = system.weights;
size_t ffStartInd = featureFunction.GetStartInd();
for (size_t i = 0; i < featureFunction.GetNumScores(); ++i) {
SCORE incrScore = scores[i];
if (system.nbestSize) {
m_scores[ffStartInd + i] += incrScore;
2015-12-11 00:47:41 +03:00
}
//cerr << "ffStartInd=" << ffStartInd << " " << i << endl;
SCORE weight = weights[ffStartInd + i];
m_total += incrScore * weight;
}
}
2015-11-04 17:54:20 +03:00
void Scores::PlusEquals(const System &system, const Scores &other)
{
2015-11-05 14:19:37 +03:00
size_t numScores = system.featureFunctions.GetNumScores();
if (system.nbestSize) {
2015-11-10 03:02:22 +03:00
for (size_t i = 0; i < numScores; ++i) {
m_scores[i] += other.m_scores[i];
2015-11-10 03:02:22 +03:00
}
2015-11-04 17:54:20 +03:00
}
m_total += other.m_total;
}
void Scores::Assign(const System &system,
2015-10-29 02:26:17 +03:00
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
2015-11-05 14:19:37 +03:00
const Weights &weights = system.weights;
2015-10-29 02:26:17 +03:00
size_t ffStartInd = featureFunction.GetStartInd();
2015-11-04 17:54:20 +03:00
if (system.nbestSize) {
assert(m_scores[ffStartInd] == 0);
m_scores[ffStartInd] = score;
2015-11-10 03:02:22 +03:00
}
2015-10-29 02:26:17 +03:00
SCORE weight = weights[ffStartInd];
m_total += score * weight;
}
2015-11-04 17:54:20 +03:00
void Scores::Assign(const System &system,
const FeatureFunction &featureFunction,
const std::vector<SCORE> &scores)
2015-10-27 00:11:47 +03:00
{
2015-11-04 17:54:20 +03:00
assert(scores.size() == featureFunction.GetNumScores());
2015-11-05 14:19:37 +03:00
const Weights &weights = system.weights;
2015-11-04 17:54:20 +03:00
size_t ffStartInd = featureFunction.GetStartInd();
for (size_t i = 0; i < scores.size(); ++i) {
SCORE incrScore = scores[i];
if (system.nbestSize) {
assert(m_scores[ffStartInd + i] == 0);
m_scores[ffStartInd + i] = incrScore;
2015-11-10 03:02:22 +03:00
}
2015-11-04 17:54:20 +03:00
//cerr << "ffStartInd=" << ffStartInd << " " << i << endl;
SCORE weight = weights[ffStartInd + i];
m_total += incrScore * weight;
}
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
{
2015-11-12 02:28:18 +03:00
vector<SCORE> scores = Tokenize<SCORE>(str);
2015-10-29 20:21:54 +03:00
if (transformScores) {
2015-11-13 13:40:55 +03:00
std::transform(scores.begin(), scores.end(), scores.begin(), TransformScore);
2015-12-18 15:02:02 +03:00
std::transform(scores.begin(), scores.end(), scores.begin(), FloorScore);
2015-10-29 20:21:54 +03:00
}
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
void Scores::Debug(std::ostream &out, const System &system) const
2015-10-28 19:33:08 +03:00
{
2016-01-06 19:35:24 +03:00
out << "total=" << m_total;
if (system.nbestSize) {
2016-01-06 19:35:24 +03:00
out << ", ";
BOOST_FOREACH(const FeatureFunction *ff, system.featureFunctions.GetFeatureFunctions()) {
2016-01-06 19:35:24 +03:00
out << ff->GetName() << "= ";
2015-12-16 19:53:00 +03:00
for (size_t i = ff->GetStartInd(); i < (ff->GetStartInd() + ff->GetNumScores()); ++i) {
out << m_scores[i] << " ";
2015-12-16 19:53:00 +03:00
}
}
2015-10-28 19:33:08 +03:00
}
}
2015-10-26 19:32:47 +03:00
std::ostream& operator<<(std::ostream &out, const Scores &obj)
{
out << obj.m_total;
return out;
}
2015-12-10 23:49:30 +03:00
// static functions to work out estimated scores
SCORE Scores::CalcWeightedScore(const System &system,
const FeatureFunction &featureFunction,
SCORE scores[])
{
SCORE ret = 0;
const Weights &weights = system.weights;
size_t ffStartInd = featureFunction.GetStartInd();
for (size_t i = 0; i < featureFunction.GetNumScores(); ++i) {
SCORE incrScore = scores[i];
//cerr << "ffStartInd=" << ffStartInd << " " << i << endl;
SCORE weight = weights[ffStartInd + i];
ret += incrScore * weight;
}
return ret;
}
SCORE Scores::CalcWeightedScore(const System &system,
const FeatureFunction &featureFunction,
SCORE score)
{
const Weights &weights = system.weights;
assert(featureFunction.GetNumScores() == 1);
size_t ffStartInd = featureFunction.GetStartInd();
SCORE weight = weights[ffStartInd];
SCORE ret = score * weight;
return ret;
}
2015-12-10 23:49:30 +03:00
}