2010-09-15 18:36:07 +04:00
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2009 University of Edinburgh
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
***********************************************************************/
|
|
|
|
|
|
|
|
#include "Decoder.h"
|
|
|
|
#include "Manager.h"
|
2010-09-17 14:17:27 +04:00
|
|
|
#include "Sentence.h"
|
2010-09-15 18:36:07 +04:00
|
|
|
#include "TranslationSystem.h"
|
2010-09-16 20:23:52 +04:00
|
|
|
#include "Phrase.h"
|
2010-09-17 14:17:27 +04:00
|
|
|
#include "TrellisPathList.h"
|
2010-09-16 20:23:52 +04:00
|
|
|
#include "DummyScoreProducers.h"
|
2010-09-15 18:36:07 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace Moses;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Mira {
|
|
|
|
|
2010-09-17 14:17:27 +04:00
|
|
|
//Decoder::~Decoder() {}
|
2010-09-15 18:36:07 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Allocates a char* and copies string into it.
|
|
|
|
**/
|
|
|
|
static char* strToChar(const string& s) {
|
|
|
|
char* c = new char[s.size()+1];
|
|
|
|
strcpy(c,s.c_str());
|
|
|
|
return c;
|
|
|
|
}
|
2010-09-16 20:23:52 +04:00
|
|
|
|
2010-09-15 18:36:07 +04:00
|
|
|
void initMoses(const string& inifile, int debuglevel, int argc, char** argv) {
|
2010-09-15 19:38:46 +04:00
|
|
|
static int BASE_ARGC = 5;
|
2010-09-15 18:36:07 +04:00
|
|
|
Parameter* params = new Parameter();
|
|
|
|
char ** mosesargv = new char*[BASE_ARGC + argc];
|
|
|
|
mosesargv[0] = strToChar("-f");
|
|
|
|
mosesargv[1] = strToChar(inifile);
|
|
|
|
mosesargv[2] = strToChar("-v");
|
|
|
|
stringstream dbgin;
|
|
|
|
dbgin << debuglevel;
|
|
|
|
mosesargv[3] = strToChar(dbgin.str());
|
2010-09-15 19:38:46 +04:00
|
|
|
mosesargv[4] = strToChar("-mbr"); //so we can do nbest
|
2010-09-15 18:36:07 +04:00
|
|
|
|
|
|
|
for (int i = 0; i < argc; ++i) {
|
|
|
|
mosesargv[BASE_ARGC + i] = argv[i];
|
|
|
|
}
|
|
|
|
params->LoadParam(BASE_ARGC + argc,mosesargv);
|
|
|
|
StaticData::LoadDataStatic(params);
|
|
|
|
for (int i = 0; i < BASE_ARGC; ++i) {
|
|
|
|
delete[] mosesargv[i];
|
|
|
|
}
|
|
|
|
delete[] mosesargv;
|
|
|
|
}
|
2010-09-17 12:33:22 +04:00
|
|
|
|
|
|
|
MosesDecoder::MosesDecoder()
|
|
|
|
: m_manager(NULL)
|
|
|
|
{
|
|
|
|
//force initialisation of the phrase dictionary
|
|
|
|
string source("hello");
|
2010-09-17 14:17:27 +04:00
|
|
|
vector<const ScoreComponentCollection*> featureScores;
|
|
|
|
vector<float> totalScores;
|
|
|
|
getNBest(source,1,featureScores,totalScores);
|
|
|
|
|
|
|
|
//Add the bleu feature
|
|
|
|
m_bleuScoreFeature = new BleuScoreFeature();
|
|
|
|
const TranslationSystem& system = StaticData::Instance().GetTranslationSystem
|
|
|
|
(TranslationSystem::DEFAULT);
|
|
|
|
(const_cast<TranslationSystem&>(system)).AddFeatureFunction(m_bleuScoreFeature);
|
2010-09-17 12:33:22 +04:00
|
|
|
}
|
2010-09-15 18:36:07 +04:00
|
|
|
|
2010-09-16 20:23:52 +04:00
|
|
|
void MosesDecoder::cleanup()
|
|
|
|
{
|
|
|
|
delete m_manager;
|
|
|
|
delete m_sentence;
|
|
|
|
}
|
|
|
|
|
2010-09-17 14:17:27 +04:00
|
|
|
void MosesDecoder::getNBest(const std::string& source,
|
|
|
|
size_t count,
|
|
|
|
vector<const ScoreComponentCollection*>& featureScores,
|
|
|
|
std::vector<float>& totalScores )
|
|
|
|
{
|
|
|
|
|
2010-09-15 19:38:46 +04:00
|
|
|
const StaticData &staticData = StaticData::Instance();
|
2010-09-16 20:23:52 +04:00
|
|
|
|
|
|
|
m_sentence = new Sentence(Input);
|
2010-09-15 19:38:46 +04:00
|
|
|
stringstream in(source + "\n");
|
|
|
|
const std::vector<FactorType> &inputFactorOrder = staticData.GetInputFactorOrder();
|
2010-09-16 20:23:52 +04:00
|
|
|
m_sentence->Read(in,inputFactorOrder);
|
2010-09-15 19:38:46 +04:00
|
|
|
const TranslationSystem& system = staticData.GetTranslationSystem
|
|
|
|
(TranslationSystem::DEFAULT);
|
2010-09-16 20:23:52 +04:00
|
|
|
|
2010-09-17 14:17:27 +04:00
|
|
|
m_manager = new Moses::Manager(*m_sentence, staticData.GetSearchAlgorithm(), &system);
|
2010-09-16 20:23:52 +04:00
|
|
|
m_manager->ProcessSentence();
|
2010-09-17 14:17:27 +04:00
|
|
|
TrellisPathList sentences;
|
2010-09-16 20:23:52 +04:00
|
|
|
m_manager->CalcNBest(count,sentences);
|
2010-09-17 11:35:31 +04:00
|
|
|
|
2010-09-17 14:17:27 +04:00
|
|
|
Moses::TrellisPathList::const_iterator iter;
|
2010-09-16 20:23:52 +04:00
|
|
|
for (iter = sentences.begin() ; iter != sentences.end() ; ++iter)
|
|
|
|
{
|
2010-09-17 14:17:27 +04:00
|
|
|
const Moses::TrellisPath &path = **iter;
|
2010-09-16 20:23:52 +04:00
|
|
|
cerr << path << endl << endl;
|
2010-09-17 11:35:31 +04:00
|
|
|
|
2010-09-17 14:17:27 +04:00
|
|
|
featureScores.push_back(&path.GetScoreBreakdown());
|
|
|
|
totalScores.push_back(path.GetTotalScore());
|
2010-09-16 20:23:52 +04:00
|
|
|
}
|
2010-09-17 11:35:31 +04:00
|
|
|
|
|
|
|
}
|
2010-09-17 14:17:27 +04:00
|
|
|
|
|
|
|
float MosesDecoder::getBleuScore(const ScoreComponentCollection& scores) {
|
|
|
|
return scores.GetScoreForProducer(m_bleuScoreFeature);
|
|
|
|
}
|
2010-09-17 16:54:58 +04:00
|
|
|
|
|
|
|
void MosesDecoder::setBleuScore(ScoreComponentCollection& scores, float bleu) {
|
|
|
|
scores.Assign(m_bleuScoreFeature,bleu);
|
|
|
|
}
|
|
|
|
|
|
|
|
ScoreComponentCollection MosesDecoder::getWeights() {
|
|
|
|
cerr << "TODO: Provide access to moses weights" << endl;
|
|
|
|
return ScoreComponentCollection();//StaticData::Instance().GetAllWeights());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MosesDecoder::setWeights(const ScoreComponentCollection& weights) {
|
|
|
|
cerr << "New weights: " << weights << endl;
|
|
|
|
cerr << "Updating: TODO " << endl;
|
|
|
|
}
|
2010-09-16 20:23:52 +04:00
|
|
|
|
2010-09-15 18:36:07 +04:00
|
|
|
}
|
2010-09-17 11:35:31 +04:00
|
|
|
|