Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
example-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 //
36 
37 #ifndef RERANKER_EXAMPLE_FEATURE_EXTRACTOR_H_
38 #define RERANKER_EXAMPLE_FEATURE_EXTRACTOR_H_
39 
40 #include <iostream>
41 #include <string>
42 #include <vector>
43 
44 #include "feature-extractor.H"
45 
46 namespace reranker {
47 
48 using std::cout;
49 using std::string;
50 using std::vector;
51 
60  public:
63  cout << "ExampleFeatureExtractor: creating instance!" << endl;
64  }
67 
68  virtual void RegisterInitializers(Initializers &initializers) {
69  initializers.Add("arg", &arg_);
70  initializers.Add("strvec", &strvec_);
71  initializers.Add("b", &b_);
72  }
73 
83  virtual void Init(const Environment *env, const string &arg) {
84  cout << "ExampleFeatureExtractor: set arg to \"" << arg_ << "\"" << endl;
85  cout << "ExampleFeatureExtractor: set strvec to {";
86  for (vector<string>::const_iterator it = strvec_.begin();
87  it != strvec_.end();
88  ++it) {
89  cout << "\"" << *it << "\", ";
90  }
91  cout << "}" << endl;
92  cout << "ExampleFeatureExtractor: set b to " << (b_ ? "true" : "false")
93  << endl;
94 
95  cout << "Environment during construction: " << endl;
96  env->Print(cout);
97  }
98 
109  virtual void Extract(Candidate &candidate,
110  FeatureVector<int,double> &features) {
111  features.IncrementWeight(0, 5.0);
112  features.IncrementWeight(27, 4.2);
113  }
114 
120  virtual void ExtractSymbolic(Candidate &candidate,
121  FeatureVector<string,double> &symbolic_features)
122  {
123  symbolic_features.IncrementWeight(arg_ + "foo", 3.2);
124  symbolic_features.IncrementWeight(arg_ + "bar", 6734.3);
125  const string &candidate_sentence = candidate.raw_data();
126  size_t first_space_idx = candidate_sentence.find_first_of(" ");
127  const string feature = candidate_sentence.substr(0, first_space_idx);
128  symbolic_features.IncrementWeight(arg_ + feature, 34.2);
129  }
130 
131  private:
132  // data members
133  string arg_;
134  vector<string> strvec_;
135  bool b_;
136 };
137 
139 
140 } // namespace reranker
141 
142 #endif
Provides the reranker::FeatureExtractor interface.
ExampleFeatureExtractor()
Constructs an instance, emitting that fact to cout.
const string & raw_data() const
Returns the raw data (typically the sentence) for this candidate.
Definition: candidate.H:143
void Add(const string &name, T *member, bool required=false)
Definition: factory.H:225
#define REGISTER_FEATURE_EXTRACTOR(TYPE)
Registers the FeatureExtractor with the specified subtype TYPE with the FeatureExtractor Factory...
virtual void Init(const Environment *env, const string &arg)
Initializes this instance with the specified argument string.
This class illustrates how to write a FeatureExtractor implementation.
An interface for an environment in which variables of various types are mapped to their values...
Definition: environment.H:125
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 void RegisterInitializers(Initializers &initializers)
V IncrementWeight(const K &uid, V by)
Increments the weight of the specified feature by the specified amount.
virtual ~ExampleFeatureExtractor()
Destroys this instance.
virtual void ExtractSymbolic(Candidate &candidate, FeatureVector< string, double > &symbolic_features)
Extracts symbolic (or string) features.
virtual void Print(ostream &os) const =0
Prints a human-readable string of all the variables in this environment, their types and...
virtual void Extract(Candidate &candidate, FeatureVector< int, double > &features)
Extracts “compiled” features.
A container for all the member initializers for a particular Factory-constructible instance...
Definition: factory.H:203