Welcome to mirror list, hosted at ThFree Co, Russian Federation.

FeatureFunction.cpp « FF « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5de14defe223ae272167ba704fb1251386094e1f (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <stdexcept>

#include "util/exception.hh"

#include "FeatureFunction.h"
#include "moses/Hypothesis.h"
#include "moses/Manager.h"
#include "moses/TranslationOption.h"
#include "moses/TranslationTask.h"
#include "moses/Util.h"
#include "moses/FF/DistortionScoreProducer.h"

#include <boost/foreach.hpp>

using namespace std;

namespace Moses
{

multiset<string> FeatureFunction::description_counts;

std::vector<FeatureFunction*> FeatureFunction::s_staticColl;

FeatureFunction &FeatureFunction::FindFeatureFunction(const std::string& name)
{
  for (size_t i = 0; i < s_staticColl.size(); ++i) {
    FeatureFunction &ff = *s_staticColl[i];
    if (ff.GetScoreProducerDescription() == name) {
      return ff;
    }
  }

  throw "Unknown feature " + name;
}

void FeatureFunction::Destroy()
{
  RemoveAllInColl(s_staticColl);
}

void FeatureFunction::SetupAll(TranslationTask const& ttask)
{
  BOOST_FOREACH(FeatureFunction* ff, s_staticColl)
  ff->Setup(ttask);
}

FeatureFunction::
FeatureFunction(const std::string& line, bool registerNow)
  : m_tuneable(true)
  , m_requireSortingAfterSourceContext(false)
  , m_verbosity(std::numeric_limits<std::size_t>::max())
  , m_numScoreComponents(1)
  , m_index(0)
{
  m_numTuneableComponents = m_numScoreComponents;
  ParseLine(line);
  // if (registerNow) Register(); // now done in FeatureFactory::DefaultSetup()
  // TO DO: eliminate the registerNow parameter
}

FeatureFunction::FeatureFunction(size_t numScoreComponents, const std::string& line, bool registerNow)
  : m_tuneable(true)
  , m_requireSortingAfterSourceContext(false)
  , m_verbosity(std::numeric_limits<std::size_t>::max())
  , m_numScoreComponents(numScoreComponents)
  , m_index(0)
{
  m_numTuneableComponents = m_numScoreComponents;
  ParseLine(line);
  // if (registerNow) Register(); // now done in FeatureFactory::DefaultSetup()
  // TO DO: eliminate the registerNow parameter
}

void
FeatureFunction::
Register(FeatureFunction* ff)
{
  ScoreComponentCollection::RegisterScoreProducer(ff);
  s_staticColl.push_back(ff);
}

FeatureFunction::~FeatureFunction() {}

void FeatureFunction::ParseLine(const std::string &line)
{
  vector<string> toks = Tokenize(line);
  UTIL_THROW_IF2(toks.empty(), "Empty line");

  string nameStub = toks[0];

  set<string> keys;

  for (size_t i = 1; i < toks.size(); ++i) {
    vector<string> args = TokenizeFirstOnly(toks[i], "=");
    UTIL_THROW_IF2(args.size() != 2,
                   "Incorrect format for feature function arg: " << toks[i]);

    pair<set<string>::iterator,bool> ret = keys.insert(args[0]);
    UTIL_THROW_IF2(!ret.second, "Duplicate key in line " << line);

    if (args[0] == "num-features") {
      m_numScoreComponents = Scan<size_t>(args[1]);
      m_numTuneableComponents = m_numScoreComponents;
    } else if (args[0] == "name") {
      m_description = args[1];
    } else {
      m_args.push_back(args);
    }
  }

  // name
  if (m_description == "") {
    size_t index = description_counts.count(nameStub);

    string descr = SPrint(nameStub) + SPrint(index);

    description_counts.insert(nameStub);
    m_description = descr;
  }

}

void FeatureFunction::SetParameter(const std::string& key, const std::string& value)
{
  if (key == "tuneable") {
    m_tuneable = Scan<bool>(value);
  } else if (key == "tuneable-components") {
    UTIL_THROW_IF2(!m_tuneable, GetScoreProducerDescription()
                   << ": tuneable-components cannot be set if tuneable=false");
    SetTuneableComponents(value);
  } else if (key == "require-sorting-after-source-context") {
    m_requireSortingAfterSourceContext = Scan<bool>(value);
  } else if (key == "verbosity") {
    m_verbosity = Scan<size_t>(value);
  } else if (key == "filterable") { //ignore
  } else {
    UTIL_THROW2(GetScoreProducerDescription() << ": Unknown argument " << key << "=" << value);
  }
}

void FeatureFunction::ReadParameters()
{
  while (!m_args.empty()) {
    const vector<string> &args = m_args[0];
    SetParameter(args[0], args[1]);

    m_args.erase(m_args.begin());
  }
}

std::vector<float> FeatureFunction::DefaultWeights() const
{
  return std::vector<float>(this->m_numScoreComponents,1.0);
  // UTIL_THROW2(GetScoreProducerDescription() << ": No default weights");
}

void FeatureFunction::SetTuneableComponents(const std::string& value)
{
  std::vector<std::string> toks = Tokenize(value,",");
  UTIL_THROW_IF2(toks.empty(), GetScoreProducerDescription()
                 << ": Empty tuneable-components");
  UTIL_THROW_IF2(toks.size()!=m_numScoreComponents, GetScoreProducerDescription()
                 << ": tuneable-components value has to be a comma-separated list of "
                 << m_numScoreComponents << " boolean values");

  m_tuneableComponents.resize(m_numScoreComponents);
  m_numTuneableComponents = m_numScoreComponents;

  for (size_t i = 0; i < toks.size(); ++i) {
    m_tuneableComponents[i] = Scan<bool>(toks[i]);
    if (!m_tuneableComponents[i]) {
      --m_numTuneableComponents;
    }
  }
}

// void
// FeatureFunction
// ::InitializeForInput(ttasksptr const& ttask)
// {
//   InitializeForInput(*(ttask->GetSource().get()));
// }

void
FeatureFunction
::CleanUpAfterSentenceProcessing(ttasksptr const& ttask)
{
  CleanUpAfterSentenceProcessing(*(ttask->GetSource().get()));
}

size_t
FeatureFunction
::GetIndex() const
{
  return m_index;
}


/// set index
//  @return index of the next FF
size_t
FeatureFunction
::SetIndex(size_t const idx)
{
  m_index = idx;
  return this->GetNumScoreComponents() + idx;
}

}