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-writer.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_WRITER_H_
38 #define RERANKER_FEATURE_VECTOR_WRITER_H_
39 
40 #include <string>
41 
42 #include "../proto/model.pb.h"
43 #include "feature-vector.H"
44 #include "symbol-table.H"
45 
46 namespace reranker {
47 
48 using confusion_learning::FeatureMessage;
49 using confusion_learning::FeatureVecMessage;
50 using confusion_learning::FeatureMessage_FeatureType;
51 
52 using std::string;
53 
59 template <typename FV>
61  public:
75  void SerializeFeature(const typename FV::key_type &uid,
76  const typename FV::mapped_type &value,
77  FeatureMessage_FeatureType feature_type,
78  FeatureMessage *feature_message,
79  Symbols *symbols = NULL) const {
80  feature_message->set_id(uid);
81  feature_message->set_type(feature_type);
82  feature_message->set_value(value);
83  if (symbols != NULL) {
84  const string &name = symbols->GetSymbol(uid);
85  if (name != "") {
86  feature_message->set_name(name);
87  }
88  }
89  }
90 
104  void Write(const FV &features,
105  FeatureMessage_FeatureType feature_type,
106  FeatureVecMessage *fv_message,
107  Symbols *symbols = NULL) const {
108  for (typename FV::const_iterator feature_it = features.begin();
109  feature_it != features.end();
110  ++feature_it) {
111  FeatureMessage *feature_message = fv_message->add_feature();
112  SerializeFeature(feature_it->first,
113  feature_it->second,
114  feature_type,
115  feature_message,
116  symbols);
117  }
118  }
119 };
120 
123 template <typename V>
125  public:
126  void Write(const FeatureVector<string,V> &features,
127  confusion_learning::FeatureMessage_FeatureType feature_type,
128  FeatureVecMessage *fv_message) const {
129  typedef typename FeatureVector<string,V>::const_iterator const_iterator;
130  for (const_iterator feature_it = features.begin();
131  feature_it != features.end();
132  ++feature_it) {
133  FeatureMessage *feature_message = fv_message->add_feature();
134  // Note that, crucially, we use FeatureMessage::set_name and not
135  // FeatureMessage::set_id, as above.
136  feature_message->set_name(feature_it->first);
137  feature_message->set_type(feature_type);
138  feature_message->set_value(feature_it->second);
139  }
140  }
141 };
142 
143 } // namespace reranker
144 
145 #endif
146 
A class to serialize FeatureVector instances to FeatureVecMessage instances.
Provides the reranker::Symbols interface as well as the reranker::StaticSymbolTable implementation...
const_iterator end() const
Returns a const iterator pointing to the end of the feature-value pairs of this feature vector...
const_iterator begin() const
Returns a const iterator pointing to the first of the feature-value pairs of this feature vector...
FeatureMap::const_iterator const_iterator
The type of const iterator for the feature-weight pairs in this vector.
An interface specifying a converter from symbols (strings) to int indices.
Definition: symbol-table.H:57
Defines the reranker::FeatureVector class, which, as it happens, is used to store feature vectors...
void SerializeFeature(const typename FV::key_type &uid, const typename FV::mapped_type &value, FeatureMessage_FeatureType feature_type, FeatureMessage *feature_message, Symbols *symbols=NULL) const
Serializes a single feature-value pair to a FeatureMessage.
void Write(const FV &features, FeatureMessage_FeatureType feature_type, FeatureVecMessage *fv_message, Symbols *symbols=NULL) const
Serializes the specified feature vector to the specified FeatureVecMessage protocol buffer message...
A class to represent a feature vector, where features are represented by unique identifiers, and feature values are represented by the template type.
void Write(const FeatureVector< string, V > &features, confusion_learning::FeatureMessage_FeatureType feature_type, FeatureVecMessage *fv_message) const