Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
feature-vector-reader.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_FEATURE_VECTOR_READER_H_
38 #define RERANKER_FEATURE_VECTOR_READER_H_
39 
40 #include <cmath>
41 #include <string>
42 
43 #include "../proto/model.pb.h"
44 #include "feature-vector.H"
45 #include "symbol-table.H"
46 
47 namespace reranker {
48 
49 using confusion_learning::FeatureMessage;
50 using confusion_learning::FeatureVecMessage;
51 
52 using std::string;
53 
60 template <typename FV>
62  public:
73  void Read(const FeatureVecMessage &fv_message,
74  FV &features,
75  Symbols *symbols) const {
76  for (int i = 0; i < fv_message.feature_size(); ++i) {
77  const FeatureMessage &feature_msg = fv_message.feature(i);
78  int uid = feature_msg.id();
79  if (symbols != NULL &&
80  feature_msg.has_name() && ! feature_msg.name().empty()) {
81  uid = symbols->GetIndex(feature_msg.name());
82  }
83  double value = feature_msg.value();
84  if (std::isnan(value)) {
85  cerr << "FeatureVectorReader: WARNING: feature " << uid
86  << " has value that is NaN" << endl;
87  } else {
88  features.IncrementWeight(uid, value);
89  }
90  }
91  }
92 };
93 
96 template <typename V>
98  public:
99  void Read(const FeatureVecMessage &fv_message,
100  FeatureVector<string,V> &features,
101  Symbols *symbols) const {
102  for (int i = 0; i < fv_message.feature_size(); ++i) {
103  const FeatureMessage &feature_msg = fv_message.feature(i);
104  double value = feature_msg.value();
105  if (std::isnan(value)) {
106  cerr << "FeatureVectorReader: WARNING: feature " << feature_msg.name()
107  << " has value that is NaN" << endl;
108  } else {
109  features.IncrementWeight(feature_msg.name(), value);
110  }
111  }
112  }
113 };
114 
115 } // namespace reranker
116 
117 #endif
118 
Provides the reranker::Symbols interface as well as the reranker::StaticSymbolTable implementation...
void Read(const FeatureVecMessage &fv_message, FV &features, Symbols *symbols) const
Serializes the specified feature vector to the specified FeatureVecMessage protocol buffer message...
void Read(const FeatureVecMessage &fv_message, FeatureVector< string, V > &features, Symbols *symbols) const
virtual int GetIndex(const string &symbol)=0
Converts the specified symbol to a unique integer.
An interface specifying a converter from symbols (strings) to int indices.
Definition: symbol-table.H:57
V IncrementWeight(const K &uid, V by)
Increments the weight of the specified feature by the specified amount.
Defines the reranker::FeatureVector class, which, as it happens, is used to store feature vectors...
A class to de-serialize FeatureVector instances from FeatureVecMessage instances. ...
A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.