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

79 lines
1.5 KiB
C++
Raw Normal View History

2015-10-24 16:36:30 +03:00
/*
* Weights.cpp
*
* Created on: 24 Oct 2015
* Author: hieu
*/
2015-10-27 20:35:42 +03:00
#include <cassert>
#include <string>
#include <vector>
2015-11-03 16:24:39 +03:00
#include "FF/FeatureFunction.h"
#include "FF/FeatureFunctions.h"
2015-10-24 16:36:30 +03:00
#include "Weights.h"
2015-11-11 19:23:49 +03:00
#include "legacy/Util2.h"
2015-10-27 20:35:42 +03:00
using namespace std;
2015-10-24 16:36:30 +03:00
2015-12-10 23:49:30 +03:00
namespace Moses2
{
2015-10-29 21:15:12 +03:00
Weights::Weights()
{
2016-03-31 23:00:16 +03:00
// TODO Auto-generated constructor stub
2015-10-24 16:36:30 +03:00
}
2016-03-31 23:00:16 +03:00
Weights::~Weights()
{
// TODO Auto-generated destructor stub
2015-10-24 16:36:30 +03:00
}
2015-10-29 21:15:12 +03:00
void Weights::Init(const FeatureFunctions &ffs)
{
2016-03-31 23:00:16 +03:00
size_t totalNumScores = ffs.GetNumScores();
//cerr << "totalNumScores=" << totalNumScores << endl;
m_weights.resize(totalNumScores, 1);
2015-10-29 21:15:12 +03:00
}
void Weights::Debug(std::ostream &out, const FeatureFunctions &ffs) const
{
2016-03-31 23:00:16 +03:00
size_t numScores = ffs.GetNumScores();
for (size_t i = 0; i < numScores; ++i) {
out << m_weights[i] << " ";
}
2015-10-29 21:15:12 +03:00
}
2015-10-26 19:32:47 +03:00
std::ostream& operator<<(std::ostream &out, const Weights &obj)
{
2016-03-31 23:00:16 +03:00
return out;
2015-10-26 19:32:47 +03:00
}
2015-10-27 20:35:42 +03:00
2016-03-31 23:00:16 +03:00
void Weights::CreateFromString(const FeatureFunctions &ffs,
const std::string &line)
2015-10-27 20:35:42 +03:00
{
2016-03-31 23:00:16 +03:00
std::vector<std::string> toks = Tokenize(line);
assert(toks.size());
2015-10-27 20:35:42 +03:00
2016-03-31 23:00:16 +03:00
string ffName = toks[0];
assert(ffName[ffName.size() - 1] == '=');
2015-10-27 20:35:42 +03:00
2016-03-31 23:00:16 +03:00
ffName = ffName.substr(0, ffName.size() - 1);
//cerr << "ffName=" << ffName << endl;
2015-10-27 20:35:42 +03:00
2016-03-31 23:00:16 +03:00
const FeatureFunction *ff = ffs.FindFeatureFunction(ffName);
assert(ff);
size_t startInd = ff->GetStartInd();
size_t numScores = ff->GetNumScores();
assert(numScores == toks.size() - 1);
2015-10-27 20:35:42 +03:00
2016-03-31 23:00:16 +03:00
for (size_t i = 0; i < numScores; ++i) {
SCORE score = Scan<SCORE>(toks[i + 1]);
m_weights[i + startInd] = score;
}
2015-10-27 20:35:42 +03:00
}
2015-12-10 23:49:30 +03:00
}