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
|
#include "ff_basic.h"
#include "fast_lexical_cast.hpp"
#include "hg.h"
using namespace std;
// Hiero and Joshua use log_10(e) as the value, so I do to
WordPenalty::WordPenalty(const string& param) :
fid_(FD::Convert("WordPenalty")),
value_(-1.0 / log(10)) {
if (!param.empty()) {
cerr << "Warning WordPenalty ignoring parameter: " << param << endl;
}
}
void WordPenalty::TraversalFeaturesImpl(const SentenceMetadata& smeta,
const Hypergraph::Edge& edge,
const std::vector<const void*>& ant_states,
SparseVector<double>* features,
SparseVector<double>* estimated_features,
void* state) const {
(void) smeta;
(void) ant_states;
(void) state;
(void) estimated_features;
features->set_value(fid_, edge.rule_->EWords() * value_);
}
SourceWordPenalty::SourceWordPenalty(const string& param) :
fid_(FD::Convert("SourceWordPenalty")),
value_(-1.0 / log(10)) {
if (!param.empty()) {
cerr << "Warning SourceWordPenalty ignoring parameter: " << param << endl;
}
}
void SourceWordPenalty::TraversalFeaturesImpl(const SentenceMetadata& smeta,
const Hypergraph::Edge& edge,
const std::vector<const void*>& ant_states,
SparseVector<double>* features,
SparseVector<double>* estimated_features,
void* state) const {
(void) smeta;
(void) ant_states;
(void) state;
(void) estimated_features;
features->set_value(fid_, edge.rule_->FWords() * value_);
}
ArityPenalty::ArityPenalty(const std::string& param) {
string fname = "Arity_";
unsigned MAX=DEFAULT_MAX_ARITY;
using namespace boost;
if (!param.empty())
MAX=lexical_cast<unsigned>(param);
for (unsigned i = 0; i <= MAX; ++i) {
WordID fid=FD::Convert(fname+lexical_cast<string>(i));
fids_.push_back(fid);
}
// pretty up features vector in case FD was frozen. doesn't change anything
while (!fids_.empty() && fids_.back()==0) fids_.pop_back();
}
void ArityPenalty::TraversalFeaturesImpl(const SentenceMetadata& smeta,
const Hypergraph::Edge& edge,
const std::vector<const void*>& ant_states,
SparseVector<double>* features,
SparseVector<double>* estimated_features,
void* state) const {
(void) smeta;
(void) ant_states;
(void) state;
(void) estimated_features;
unsigned a=edge.Arity();
if (a < fids_.size()) features->set_value(fids_[a], 1.0);
}
|