Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
abstract-file-backed-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 //
37 
38 #ifndef RERANKER_ABSTRACT_FILE_BACKED_FEATURE_EXTRACTOR_H_
39 #define RERANKER_ABSTRACT_FILE_BACKED_FEATURE_EXTRACTOR_H_
40 
41 #include <iostream>
42 #include <fstream>
43 
44 #include "tokenizer.H"
45 #include "feature-extractor.H"
46 
47 namespace reranker {
48 
49 using std::cout;
50 using std::istream;
51 using std::ifstream;
52 
58  public:
61  }
64  delete is_;
65  }
66 
86  virtual void RegisterInitializers(Initializers &initializers) {
87  bool required = true;
88  initializers.Add("filename", &filename_, required);
89  }
90 
99  virtual void Init(const Environment *env, const string &arg) {
100  delete is_;
101  is_ = new ifstream(filename_.c_str());
102  }
103 
105  virtual void Extract(Candidate &candidate,
106  FeatureVector<int,double> &features) = 0;
107 
109  virtual void ExtractSymbolic(Candidate &candidate,
110  FeatureVector<string,double> &symbolic_features)
111  = 0;
112 
113  virtual void Reset() {
114  delete is_;
115  is_ = new ifstream(filename_.c_str());
116  }
117 
118 
127  virtual void Extract(CandidateSet &candidate_set) {
128  for (CandidateSet::iterator it = candidate_set.begin();
129  it != candidate_set.end();
130  ++it) {
131  Candidate &candidate = *(*it);
132  ReadFromStream();
133  Extract(candidate, candidate.mutable_features());
134  ExtractSymbolic(candidate, candidate.mutable_symbolic_features());
135  }
136  }
137 
140  virtual long line_number() const { return line_number_; }
141 
142  protected:
145  virtual void ReadFromStream() {
146  // TODO(dbikel): Do error checking here for is_->good().
147  // Emit massive warnings when something goes wrong.
148  // TODO(dbikel): Keep track of number of lines read.
149  if (is_->good()) {
150  getline(*is_, line_);
151  ++line_number_;
152  } else {
153  cerr << "AbstractFileBackedFeatureExtractor: error: no more input\n"
154  << "\t(number of lines read so far: " << line_number_ << ")" << endl;
155  }
156  }
157 
158  // data members
160  string filename_;
162  istream *is_;
164  string line_;
169 };
170 
171 } // namespace reranker
172 
173 #endif
Provides the reranker::FeatureExtractor interface.
vector< shared_ptr< Candidate > >::iterator iterator
Definition: candidate-set.H:73
Provides the Tokenizer class.
A very simple tokenizer class.
Definition: tokenizer.H:49
virtual long line_number() const
Returns the current number of lines read by this feature extractor from the underlying stream...
string filename_
The name of the file backing this feature extractor.
void Add(const string &name, T *member, bool required=false)
Definition: factory.H:225
This class makes it easy for concrete subclasses to extract features based on input from a file...
virtual void RegisterInitializers(Initializers &initializers)
Registers the data member to be initialized when an instance of this class is constructed by a Factor...
A class to hold a set of candidates, either for training or test.
Definition: candidate-set.H:62
virtual void Extract(CandidateSet &candidate_set)
Extracts features for all the candidates in the specified CandidateSet.
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.
Tokenizer tokenizer_
A simple whitespace tokenizer for use by concrete subclasses.
string line_
The last line read by this feature extractor.
virtual void ExtractSymbolic(Candidate &candidate, FeatureVector< string, double > &symbolic_features)=0
Extracts symbolic features for the specified candidate, where each feature is represented by a string...
const_iterator begin() const
Definition: candidate-set.H:86
virtual void Reset()
Indicates to this instance that iteration over candidate sets on which features are being extracted h...
istream * is_
The stream created from the file backing this feature extractor.
long line_number_
The number of lines read so far by this feature extractor.
const_iterator end() const
Definition: candidate-set.H:88
virtual void Init(const Environment *env, const string &arg)
This method is guaranteed to be inokved by a Factory after invoking the RegisterInitializers method j...
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...
A container for all the member initializers for a particular Factory-constructible instance...
Definition: factory.H:203