stubbing out of optimiser

git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/branches/mira-mtm5@3463 1f5c12ca-751b-0410-a591-d2e778427230
This commit is contained in:
bhaddow 2010-09-15 15:38:46 +00:00
parent ff88fbbd66
commit c01783869a
4 changed files with 124 additions and 24 deletions

View File

@ -40,7 +40,7 @@ namespace Mira {
void initMoses(const string& inifile, int debuglevel, int argc, char** argv) {
static int BASE_ARGC = 4;
static int BASE_ARGC = 5;
Parameter* params = new Parameter();
char ** mosesargv = new char*[BASE_ARGC + argc];
mosesargv[0] = strToChar("-f");
@ -49,6 +49,7 @@ namespace Mira {
stringstream dbgin;
dbgin << debuglevel;
mosesargv[3] = strToChar(dbgin.str());
mosesargv[4] = strToChar("-mbr"); //so we can do nbest
for (int i = 0; i < argc; ++i) {
mosesargv[BASE_ARGC + i] = argv[i];
@ -61,17 +62,17 @@ namespace Mira {
delete[] mosesargv;
}
void MosesDecoder::getNBest(const std::string& source) {
const StaticData &staticData = StaticData::Instance();
Sentence sentence(Input);
stringstream in(source + "\n");
const std::vector<FactorType> &inputFactorOrder = staticData.GetInputFactorOrder();
sentence.Read(in,inputFactorOrder);
const TranslationSystem& system = staticData.GetTranslationSystem
(TranslationSystem::DEFAULT);
Manager manager(sentence, staticData.GetSearchAlgorithm(), &system);
manager.ProcessSentence();
void MosesDecoder::getNBest(const std::string& source, size_t count, TrellisPathList& sentences) {
const StaticData &staticData = StaticData::Instance();
Sentence sentence(Input);
stringstream in(source + "\n");
const std::vector<FactorType> &inputFactorOrder = staticData.GetInputFactorOrder();
sentence.Read(in,inputFactorOrder);
const TranslationSystem& system = staticData.GetTranslationSystem
(TranslationSystem::DEFAULT);
Manager manager(sentence, staticData.GetSearchAlgorithm(), &system);
manager.ProcessSentence();
manager.CalcNBest(count,sentences);
}
}

View File

@ -1,6 +1,6 @@
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2009 University of Edinburgh
Copyright (C) 2010 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
@ -16,7 +16,8 @@ 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
***********************************************************************/
#pragma once
#ifndef _MIRA_DECODER_H_
#define _MIRA_DECODER_H_
#include <iostream>
#include <cstring>
@ -48,22 +49,23 @@ void initMoses(const std::string& inifile, int debuglevel, int argc=0, char** a
/**
* Wrapper around any decoder. Notice the moses specific return values!
* Wrapper around any decoder.
**/
class Decoder {
public:
virtual void getNBest(const std::string& source) = 0;
virtual void getNBest(const std::string& source, size_t count, Moses::TrellisPathList& sentences) = 0;
virtual ~Decoder();
};
/**
* Wraps moses decoder.
**/
class MosesDecoder : public virtual Decoder {
class MosesDecoder : public Decoder {
public:
MosesDecoder() {}
virtual void getNBest(const std::string& source);
virtual void getNBest(const std::string& source, size_t count, Moses::TrellisPathList& sentences);
};
} //namespace
#endif

View File

@ -17,20 +17,35 @@ 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 <cstdlib>
#include <ctime>
#include <string>
#include <vector>
#include <boost/program_options.hpp>
#include "FeatureVector.h"
#include "StaticData.h"
#include "TrellisPathList.h"
#include "Decoder.h"
#include "Optimiser.h"
using namespace Mira;
using namespace std;
using namespace Moses;
namespace po = boost::program_options;
bool loadSentences(const string& filename, vector<string>& sentences) {
ifstream in(filename.c_str());
if (!in) return false;
string line;
while(getline(in,line)) {
sentences.push_back(line);
}
return true;
}
int main(int argc, char** argv) {
bool help;
int verbosity;
@ -55,7 +70,7 @@ int main(int argc, char** argv) {
if (help) {
std::cout << "Usage: " + string(argv[0]) + " -f mosesini-file [options]" << std::endl;
std::cout << "Usage: " + string(argv[0]) + " -f mosesini-file -i input-file -r reference-file(s) [options]" << std::endl;
std::cout << desc << std::endl;
return 0;
}
@ -76,18 +91,51 @@ int main(int argc, char** argv) {
}
//load input and references
vector<string> inputSentences;
if (!loadSentences(inputFile, inputSentences)) {
cerr << "Error: Failed to load input sentences from " << inputFile << endl;
return 1;
}
vector< vector<string> > referenceSentences(referenceFiles.size());
for (size_t i = 0; i < referenceFiles.size(); ++i) {
if (!loadSentences(referenceFiles[i], referenceSentences[i])) {
cerr << "Error: Failed to load reference sentences from " << referenceFiles[i] << endl;
return 1;
}
if (referenceSentences[i].size() != inputSentences.size()) {
cerr << "Error: Input file length (" << inputSentences.size() <<
") != (" << referenceSentences[i].size() << ") length of reference file " << i <<
endl;
return 1;
}
}
//initialise moses
initMoses(mosesConfigFile, verbosity, argc, argv);
//load input and references
//Main loop:
//pick sentence
//run decoder
srand(time(NULL));
bool converged = false;
Decoder* decoder = new MosesDecoder();
Optimiser* optimiser = new DummyOptimiser();
while (!converged) {
//pick sentence (TODO: batch)
int sentenceId = rand() % inputSentences.size();
const string& input = inputSentences[sentenceId];
const vector<string>& refs = referenceSentences[sentenceId];
//run decoder (TODO: hope & fear)
TrellisPathList sentences;
decoder->getNBest(input, 100, sentences);
//run optimiser
//update moses weights
}
return 0;
exit(0);
}

49
mira/Optimiser.h Normal file
View File

@ -0,0 +1,49 @@
/***********************************************************************
Moses - factored phrase-based language decoder
Copyright (C) 2010 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
***********************************************************************/
#ifndef _MIRA_OPTIMISER_H_
#define _MIRA_OPTIMISER_H_
#include <vector>
#include "ScoreComponentCollection.h"
namespace Mira {
class Optimiser {
public:
virtual void updateWeights(const Moses::ScoreComponentCollection& currWeights,
const std::vector< std::vector<Moses::ScoreComponentCollection> >& scores,
const std::vector<float>& losses,
const Moses::ScoreComponentCollection oracleScores,
Moses::ScoreComponentCollection& newWeights) = 0;
};
class DummyOptimiser : public Optimiser {
public:
virtual void updateWeights(const Moses::ScoreComponentCollection& currWeights,
const std::vector< std::vector<Moses::ScoreComponentCollection> >& scores,
const std::vector<float>& losses,
const Moses::ScoreComponentCollection oracleScores,
Moses::ScoreComponentCollection& newWeights) {/* do nothing */}
};
}
#endif