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
|
#include "FeatureRegistry.h"
#include "../TranslationModel/Memory/PhraseTableMemory.h"
#include "../TranslationModel/CompactPT/PhraseTableCompact.h"
#include "../TranslationModel/ProbingPT/ProbingPT.h"
#include "../TranslationModel/UnknownWordPenalty.h"
#include "../TranslationModel/Transliteration.h"
#include "../LM/KENLM.h"
#include "../LM/KENLMBatch.h"
#include "../LM/LanguageModel.h"
#include "../LM/GPULM.h"
#include "Distortion.h"
#include "LexicalReordering/LexicalReordering.h"
#include "PhrasePenalty.h"
#include "WordPenalty.h"
#include "OSM/OpSequenceModel.h"
#include "SkeletonStatefulFF.h"
#include "SkeletonStatelessFF.h"
using namespace std;
namespace Moses2
{
template<class F>
class DefaultFeatureFactory: public FeatureFactory
{
public:
FeatureFunction *Create(size_t startInd, const std::string &line)
{
return new F(startInd, line);
}
};
////////////////////////////////////////////////////////////////////
class KenFactory: public FeatureFactory
{
public:
FeatureFunction *Create(size_t startInd, const std::string &line)
{
ConstructKenLM(startInd, line);
}
};
////////////////////////////////////////////////////////////////////
FeatureRegistry::FeatureRegistry()
{
// Feature with same name as class
#define MOSES_FNAME(name) Add(#name, new DefaultFeatureFactory< name >());
// Feature with different name than class.
#define MOSES_FNAME2(name, type) Add(name, new DefaultFeatureFactory< type >());
MOSES_FNAME2("PhraseDictionaryCompact", PhraseTableCompact);
MOSES_FNAME2("PhraseDictionaryMemory", PhraseTableMemory);
MOSES_FNAME(ProbingPT);
MOSES_FNAME2("PhraseDictionaryTransliteration", Transliteration);
MOSES_FNAME(UnknownWordPenalty);
Add("KENLM", new KenFactory());
MOSES_FNAME(KENLMBatch);
MOSES_FNAME(GPULM);
MOSES_FNAME(LanguageModel);
MOSES_FNAME(Distortion);
MOSES_FNAME(LexicalReordering);
MOSES_FNAME(PhrasePenalty);
MOSES_FNAME(WordPenalty);
MOSES_FNAME(OpSequenceModel);
MOSES_FNAME(SkeletonStatefulFF);
MOSES_FNAME(SkeletonStatelessFF);
}
FeatureRegistry::~FeatureRegistry()
{
}
void FeatureRegistry::Add(const std::string &name, FeatureFactory *factory)
{
std::pair<std::string, boost::shared_ptr<FeatureFactory> > to_ins(name,
boost::shared_ptr<FeatureFactory>(factory));
if (!registry_.insert(to_ins).second) {
cerr << "Duplicate feature name " << name << endl;
abort();
}
}
FeatureFunction *FeatureRegistry::Construct(size_t startInd,
const std::string &name, const std::string &line)
{
Map::iterator i = registry_.find(name);
if (i == registry_.end()) {
cerr << "Feature name " << name << " is not registered.";
abort();
}
FeatureFactory *fact = i->second.get();
FeatureFunction *ff = fact->Create(startInd, line);
return ff;
}
void FeatureRegistry::PrintFF() const
{
std::vector<std::string> ffs;
std::cerr << "Available feature functions:" << std::endl;
Map::const_iterator iter;
for (iter = registry_.begin(); iter != registry_.end(); ++iter) {
const std::string &ffName = iter->first;
ffs.push_back(ffName);
}
std::vector<std::string>::const_iterator iterVec;
std::sort(ffs.begin(), ffs.end());
for (iterVec = ffs.begin(); iterVec != ffs.end(); ++iterVec) {
const std::string &ffName = *iterVec;
std::cerr << ffName << " ";
}
std::cerr << std::endl;
}
}
|