Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
rank-feature-extractor.H
Go to the documentation of this file.
1 // Copyright 2012, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // -----------------------------------------------------------------------------
30 //
31 //
35 
36 #ifndef RERANKER_RANK_FEATURE_EXTRACTOR_H_
37 #define RERANKER_RANK_FEATURE_EXTRACTOR_H_
38 
39 #include <cmath>
40 #include <sstream>
41 
42 #include "feature-extractor.H"
43 
44 namespace reranker {
45 
46 using std::stringstream;
47 
52  public:
55  }
57  virtual ~RankFeatureExtractor() { }
58 
59  virtual void RegisterInitializers(Initializers &initializers) {
60  initializers.Add("add_score_factor", &add_score_factor_);
61  initializers.Add("add_score_difference", &add_score_difference_);
62  }
63 
64  virtual void Extract(Candidate &candidate,
65  FeatureVector<int,double> &features) { }
66 
73  virtual void ExtractSymbolic(Candidate &candidate,
74  FeatureVector<string,double> &symbolic_features)
75  {
76  stringstream ss;
77  ss << "orig_rank:" << candidate.index();
78  symbolic_features.IncrementWeight(ss.str(), 1.0);
79 
80  stringstream lg_rank_ss;
81  double lg_rank = floor(log((double)candidate.index() + 1.0) /
82  log(2.0) + 0.5);
83  lg_rank_ss << "lg_rank:" << lg_rank;
84  symbolic_features.IncrementWeight(lg_rank_ss.str(), 1.0);
85 
86  if (add_score_factor_) {
87  double top_baseline_score =
88  top_baseline_score_ == 0.0 ? 1e-10 : top_baseline_score_;
89  double score_factor = candidate.baseline_score() / top_baseline_score;
90  symbolic_features.IncrementWeight("score_factor", score_factor);
91  }
92  if (add_score_difference_) {
93  double score_difference =
94  candidate.baseline_score() - top_baseline_score_;
95  symbolic_features.IncrementWeight("score_difference", score_difference);
96  }
97  }
98 
101  virtual void Extract(CandidateSet &candidate_set) {
102  top_baseline_score_ = candidate_set.Get(0).baseline_score();
103  FeatureExtractor::Extract(candidate_set);
104  }
105 
106  private:
107  double top_baseline_score_;
108  bool add_score_factor_;
109  bool add_score_difference_;
110 };
111 
112 } // namespace reranker
113 
114 #endif
Provides the reranker::FeatureExtractor interface.
virtual void Extract(Candidate &candidate, FeatureVector< int, double > &features)=0
Extracts features for the specified candidate, where each feature is represented by an int for its un...
double baseline_score() const
Returns the baseline model score for this candidate.
Definition: candidate.H:133
virtual void ExtractSymbolic(Candidate &candidate, FeatureVector< string, double > &symbolic_features)
Extracts symbolic (or string) features.
Encodes the baseline rank as a boolean feature.
void Add(const string &name, T *member, bool required=false)
Definition: factory.H:225
RankFeatureExtractor()
Constructs an instance.
int index() const
Returns the index of this candidate relative to the other candidates.
Definition: candidate.H:121
A class to hold a set of candidates, either for training or test.
Definition: candidate-set.H:62
virtual void RegisterInitializers(Initializers &initializers)
A class to represent a candidate in a set of candidates that constitutes a training instance for a re...
Definition: candidate.H:60
An abstract base class/interface for all feature extractors.
virtual ~RankFeatureExtractor()
Destroys this instance.
virtual void Extract(Candidate &candidate, FeatureVector< int, double > &features)
Extracts features for the specified candidate, where each feature is represented by an int for its un...
V IncrementWeight(const K &uid, V by)
Increments the weight of the specified feature by the specified amount.
Candidate & Get(size_t idx)
virtual void Extract(CandidateSet &candidate_set)
Override the default Extract method so that it caches the score of the top-scoring hypothesis of the ...
A container for all the member initializers for a particular Factory-constructible instance...
Definition: factory.H:203