2014-07-21 14:04:43 +04:00
|
|
|
/***********************************************************************
|
|
|
|
Moses - factored phrase-based language decoder
|
|
|
|
Copyright (C) 2014- 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
|
|
|
|
***********************************************************************/
|
2015-05-01 00:26:30 +03:00
|
|
|
#pragma once
|
2014-07-21 14:04:43 +04:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <boost/scoped_ptr.hpp>
|
|
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
|
|
|
|
#include "ForestRescore.h"
|
|
|
|
#include "Hypergraph.h"
|
|
|
|
#include "HypPackEnumerator.h"
|
|
|
|
#include "MiraFeatureVector.h"
|
|
|
|
#include "MiraWeightVector.h"
|
|
|
|
|
|
|
|
//
|
|
|
|
// Used by batch mira to get the hope, fear and model hypothesis. This wraps
|
|
|
|
// the n-best list and lattice/hypergraph implementations
|
|
|
|
//
|
|
|
|
|
2015-01-14 14:07:42 +03:00
|
|
|
namespace MosesTuning
|
|
|
|
{
|
2014-07-21 14:04:43 +04:00
|
|
|
|
2015-05-13 12:21:43 +03:00
|
|
|
|
|
|
|
/** Initialise weights from files. Returns weight vector and number of dense features */
|
|
|
|
std::pair<MiraWeightVector*,size_t>
|
|
|
|
InitialiseWeights(const std::string& denseInitFile, const std::string& sparseInitFile,
|
2015-05-15 20:09:38 +03:00
|
|
|
const std::string& type, bool verbose);
|
2015-05-13 12:21:43 +03:00
|
|
|
|
2014-09-17 17:14:11 +04:00
|
|
|
class Scorer;
|
|
|
|
|
2014-07-21 14:04:43 +04:00
|
|
|
/** To be filled in by the decoder */
|
|
|
|
struct HopeFearData {
|
|
|
|
MiraFeatureVector modelFeatures;
|
|
|
|
MiraFeatureVector hopeFeatures;
|
|
|
|
MiraFeatureVector fearFeatures;
|
2015-01-14 14:07:42 +03:00
|
|
|
|
2014-07-21 14:04:43 +04:00
|
|
|
std::vector<float> modelStats;
|
|
|
|
std::vector<float> hopeStats;
|
|
|
|
|
|
|
|
ValType hopeBleu;
|
|
|
|
ValType fearBleu;
|
|
|
|
|
|
|
|
bool hopeFearEqual;
|
|
|
|
};
|
|
|
|
|
|
|
|
//Abstract base class
|
2015-01-14 14:07:42 +03:00
|
|
|
class HopeFearDecoder
|
|
|
|
{
|
2014-07-21 14:04:43 +04:00
|
|
|
public:
|
|
|
|
//iterator methods
|
|
|
|
virtual void reset() = 0;
|
|
|
|
virtual void next() = 0;
|
|
|
|
virtual bool finished() = 0;
|
|
|
|
|
2014-09-17 17:14:11 +04:00
|
|
|
virtual ~HopeFearDecoder() {};
|
|
|
|
|
2014-07-21 14:04:43 +04:00
|
|
|
/**
|
|
|
|
* Calculate hope, fear and model hypotheses
|
|
|
|
**/
|
|
|
|
virtual void HopeFear(
|
2015-01-14 14:07:42 +03:00
|
|
|
const std::vector<ValType>& backgroundBleu,
|
|
|
|
const MiraWeightVector& wv,
|
|
|
|
HopeFearData* hopeFear
|
|
|
|
) = 0;
|
2014-07-21 14:04:43 +04:00
|
|
|
|
|
|
|
/** Max score decoding */
|
|
|
|
virtual void MaxModel(const AvgWeightVector& wv, std::vector<ValType>* stats)
|
2015-01-14 22:21:11 +03:00
|
|
|
= 0;
|
2014-07-21 14:04:43 +04:00
|
|
|
|
|
|
|
/** Calculate bleu on training set */
|
|
|
|
ValType Evaluate(const AvgWeightVector& wv);
|
|
|
|
|
2014-09-17 17:14:11 +04:00
|
|
|
protected:
|
|
|
|
Scorer* scorer_;
|
2014-07-21 14:04:43 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** Gets hope-fear from nbest lists */
|
2015-01-14 14:07:42 +03:00
|
|
|
class NbestHopeFearDecoder : public virtual HopeFearDecoder
|
|
|
|
{
|
2014-07-21 14:04:43 +04:00
|
|
|
public:
|
|
|
|
NbestHopeFearDecoder(const std::vector<std::string>& featureFiles,
|
2015-01-14 14:07:42 +03:00
|
|
|
const std::vector<std::string>& scoreFiles,
|
|
|
|
bool streaming,
|
|
|
|
bool no_shuffle,
|
|
|
|
bool safe_hope,
|
|
|
|
Scorer* scorer
|
|
|
|
);
|
2014-07-21 14:04:43 +04:00
|
|
|
|
|
|
|
virtual void reset();
|
|
|
|
virtual void next();
|
|
|
|
virtual bool finished();
|
|
|
|
|
|
|
|
virtual void HopeFear(
|
2015-01-14 14:07:42 +03:00
|
|
|
const std::vector<ValType>& backgroundBleu,
|
|
|
|
const MiraWeightVector& wv,
|
|
|
|
HopeFearData* hopeFear
|
|
|
|
);
|
2014-07-21 14:04:43 +04:00
|
|
|
|
|
|
|
virtual void MaxModel(const AvgWeightVector& wv, std::vector<ValType>* stats);
|
|
|
|
|
|
|
|
private:
|
|
|
|
boost::scoped_ptr<HypPackEnumerator> train_;
|
|
|
|
bool safe_hope_;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Gets hope-fear from hypergraphs */
|
2015-01-14 14:07:42 +03:00
|
|
|
class HypergraphHopeFearDecoder : public virtual HopeFearDecoder
|
|
|
|
{
|
2014-07-21 14:04:43 +04:00
|
|
|
public:
|
|
|
|
HypergraphHopeFearDecoder(
|
2015-01-14 14:07:42 +03:00
|
|
|
const std::string& hypergraphDir,
|
|
|
|
const std::vector<std::string>& referenceFiles,
|
|
|
|
size_t num_dense,
|
|
|
|
bool streaming,
|
|
|
|
bool no_shuffle,
|
|
|
|
bool safe_hope,
|
|
|
|
size_t hg_pruning,
|
|
|
|
const MiraWeightVector& wv,
|
|
|
|
Scorer* scorer_
|
|
|
|
);
|
2014-07-21 14:04:43 +04:00
|
|
|
|
|
|
|
virtual void reset();
|
|
|
|
virtual void next();
|
|
|
|
virtual bool finished();
|
|
|
|
|
|
|
|
virtual void HopeFear(
|
2015-01-14 14:07:42 +03:00
|
|
|
const std::vector<ValType>& backgroundBleu,
|
|
|
|
const MiraWeightVector& wv,
|
|
|
|
HopeFearData* hopeFear
|
|
|
|
);
|
2014-07-21 14:04:43 +04:00
|
|
|
|
|
|
|
virtual void MaxModel(const AvgWeightVector& wv, std::vector<ValType>* stats);
|
|
|
|
|
|
|
|
private:
|
|
|
|
size_t num_dense_;
|
|
|
|
//maps sentence Id to graph ptr
|
|
|
|
typedef std::map<size_t, boost::shared_ptr<Graph> > GraphColl;
|
|
|
|
GraphColl graphs_;
|
2014-08-04 23:51:45 +04:00
|
|
|
std::vector<size_t> sentenceIds_;
|
|
|
|
std::vector<size_t>::const_iterator sentenceIdIter_;
|
2014-07-21 14:04:43 +04:00
|
|
|
ReferenceSet references_;
|
|
|
|
Vocab vocab_;
|
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|