Reranker Framework (ReFr)
Reranking framework for structure prediction and discriminative language modeling
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
model-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_MODEL_READER_H_
38 #define RERANKER_MODEL_READER_H_
39 
40 #include <string>
41 
42 #include "../proto/model.pb.h"
43 #include "factory.H"
44 #include "model-proto-reader.H"
45 
46 namespace reranker {
47 
48 using confusion_learning::ModelMessage;
49 using std::string;
50 
55 class ModelReader {
56  public:
57  ModelReader(int verbosity = 0) : verbosity_(verbosity) { }
58 
59  shared_ptr<Model> Read(const string &filename,
60  bool compressed, bool use_base64) {
61  ConfusionProtoIO proto_reader(filename, ConfusionProtoIO::READ,
62  compressed, use_base64);
63  ModelMessage model_message;
64  if (verbosity_ >= 1) {
65  cerr << "ModelReader: reading serialized model from \""
66  << filename << "\"...";
67  cerr.flush();
68  }
69  if (!proto_reader.Read(&model_message)) {
70  cerr << "ModelReader: unable to read serialized model from \""
71  << filename << "\"." << endl;
72  return shared_ptr<Model>();
73  }
74  shared_ptr<ModelProtoReader> model_reader =
75  GetModelProtoReader(model_message);
76  shared_ptr<Model> model =
77  model_factory_.CreateOrDie(model_message.model_spec(),"model spec");
78  if (model.get() == NULL) {
79  return model;
80  }
81  model_reader->ReadFeatures(*(proto_reader.inputstream()), model.get());
82 
83  if (verbosity_ >= 1) {
84  cerr << "done." << endl
85  << "Loaded model \"" << model->name() << "\"." << endl;
86  }
87 
88  return model;
89  }
90 
91  shared_ptr<Model> Read(const ModelMessage &model_message) {
92  shared_ptr<ModelProtoReader> model_proto_reader =
93  GetModelProtoReader(model_message);
94  shared_ptr<Model> model =
95  model_factory_.CreateOrDie(model_message.model_spec(), "model spec");
96  model_proto_reader->Read(model_message, model.get());
97  return model;
98  }
99  private:
100  shared_ptr<ModelProtoReader>
101  GetModelProtoReader(const ModelMessage &model_message) {
102  if (!model_message.has_reader_spec()) {
103  cerr << "ModelReader: no reader_spec in ModelMessage" << endl;
104  return shared_ptr<ModelProtoReader>();
105  }
106  string model_reader_spec = model_message.reader_spec();
107  Factory<ModelProtoReader> model_proto_reader_factory;
108  return
109  model_proto_reader_factory_.CreateOrDie(model_reader_spec,
110  "model proto reader spec");
111  }
112 
113  Factory<Model> model_factory_;
114  Factory<ModelProtoReader> model_proto_reader_factory_;
115  int verbosity_;
116 };
117 
118 } // namespace reranker
119 
120 #endif
shared_ptr< Model > Read(const string &filename, bool compressed, bool use_base64)
Definition: model-reader.H:59
ModelReader(int verbosity=0)
Definition: model-reader.H:57
Interface for de-serializer for reranker::Model instances from ModelMessage instances.
shared_ptr< Model > Read(const ModelMessage &model_message)
Definition: model-reader.H:91
Provides a generic dynamic object factory.
Knows how to create Model instances that have been serialized to a file.
Definition: model-reader.H:55