File size: 4,288 Bytes
158b61b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
/*
* reordering_classes.h
* Utility classes for lexical reordering table scoring
*
* Created by: Sara Stymne - Linköping University
* Machine Translation Marathon 2010, Dublin
*/
#pragma once
#include <vector>
#include <string>
#include <fstream>
#include "util/string_piece.hh"
#include "../OutputFileStream.h"
enum ORIENTATION {MONO, SWAP, DRIGHT, DLEFT, OTHER, NOMONO};
//Keeps the counts for the different reordering types
//(Instantiated in 1-3 instances, one for each type of model (hier, phrase, wbe))
class ModelScore
{
private:
std::vector<double> count_fe_prev;
std::vector<double> count_fe_next;
std::vector<double> count_f_prev;
std::vector<double> count_f_next;
protected:
virtual ORIENTATION getType(const StringPiece& s);
public:
ModelScore();
virtual ~ModelScore();
void add_example(const StringPiece& previous, const StringPiece& next, float weight);
void reset_fe();
void reset_f();
const std::vector<double>& get_scores_fe_prev() const;
const std::vector<double>& get_scores_fe_next() const;
const std::vector<double>& get_scores_f_prev() const;
const std::vector<double>& get_scores_f_next() const;
static ModelScore* createModelScore(const std::string& modeltype);
};
class ModelScoreMSLR : public ModelScore
{
protected:
virtual ORIENTATION getType(const StringPiece& s);
};
class ModelScoreLR : public ModelScore
{
protected:
virtual ORIENTATION getType(const StringPiece& s);
};
class ModelScoreMSD : public ModelScore
{
protected:
virtual ORIENTATION getType(const StringPiece& s);
};
class ModelScoreMonotonicity : public ModelScore
{
protected:
virtual ORIENTATION getType(const StringPiece& s);
};
//Class for calculating total counts, and to calculate smoothing
class Scorer
{
public:
virtual ~Scorer() {}
virtual void score(const std::vector<double>&, std::vector<double>&) const = 0;
virtual void createSmoothing(const std::vector<double>&, double, std::vector<double>&) const = 0;
virtual void createConstSmoothing(double, std::vector<double>&) const = 0;
};
class ScorerMSLR : public Scorer
{
public:
virtual void score(const std::vector<double>&, std::vector<double>&) const;
virtual void createSmoothing(const std::vector<double>&, double, std::vector<double>&) const;
virtual void createConstSmoothing(double, std::vector<double>&) const;
};
class ScorerMSD : public Scorer
{
public:
virtual void score(const std::vector<double>&, std::vector<double>&) const;
virtual void createSmoothing(const std::vector<double>&, double, std::vector<double>&) const;
virtual void createConstSmoothing(double, std::vector<double>&) const;
};
class ScorerMonotonicity : public Scorer
{
public:
virtual void score(const std::vector<double>&, std::vector<double>&) const;
virtual void createSmoothing(const std::vector<double>&, double, std::vector<double>&) const;
virtual void createConstSmoothing(double, std::vector<double>&) const;
};
class ScorerLR : public Scorer
{
public:
virtual void score(const std::vector<double>&, std::vector<double>&) const;
virtual void createSmoothing(const std::vector<double>&, double, std::vector<double>&) const;
virtual void createConstSmoothing(double, std::vector<double>&) const;
};
//Class for representing each model
//Contains a modelscore and scorer (which can be of different model types (mslr, msd...)),
//and file handling.
//This class also keeps track of bidirectionality, and which language to condition on
class Model
{
private:
ModelScore* modelscore;
Scorer* scorer;
std::string filename;
Moses::OutputFileStream outputFile;
bool fe;
bool previous;
bool next;
std::vector<double> smoothing_prev;
std::vector<double> smoothing_next;
static void split_config(const std::string& config, std::string& dir,
std::string& lang, std::string& orient);
public:
Model(ModelScore* ms, Scorer* sc, const std::string& dir,
const std::string& lang, const std::string& fn);
~Model();
static Model* createModel(ModelScore*, const std::string&, const std::string&);
void createSmoothing(double w);
void createConstSmoothing(double w);
void score_fe(const std::string& f, const std::string& e);
void score_f(const std::string& f);
void zipFile();
};
|