ModErn Text Analysis
META Enumerates Textual Applications
ranker_factory.h
Go to the documentation of this file.
1 
9 #ifndef META_RANKER_FACTORY_H_
10 #define META_RANKER_FACTORY_H_
11 
14 #include "meta/util/factory.h"
15 #include "meta/util/shim.h"
16 
17 namespace cpptoml
18 {
19 class table;
20 }
21 
22 namespace meta
23 {
24 namespace index
25 {
26 
33  : public util::factory<ranker_factory, ranker, const cpptoml::table&,
34  const cpptoml::table&>
35 {
36  public:
38  friend base_factory;
39 
40  std::unique_ptr<language_model_ranker>
41  create_lm(util::string_view identifier, const cpptoml::table& global,
42  const cpptoml::table& local)
43  {
44  auto rnk = base_factory::create(identifier, global, local);
45  if (auto der = dynamic_cast<language_model_ranker*>(rnk.get()))
46  {
47  rnk.release();
48  return std::unique_ptr<language_model_ranker>{der};
49  }
50  throw std::invalid_argument{identifier.to_string()
51  + " is not a language_model_ranker"};
52  }
53 
54  private:
59 
63  template <class Ranker>
64  void reg();
65 };
66 
70 std::unique_ptr<ranker> make_ranker(const cpptoml::table&);
71 
77 std::unique_ptr<ranker> make_ranker(const cpptoml::table& global,
78  const cpptoml::table& local);
79 
84 std::unique_ptr<language_model_ranker> make_lm_ranker(const cpptoml::table&);
85 
91 std::unique_ptr<language_model_ranker>
92 make_lm_ranker(const cpptoml::table& global, const cpptoml::table& local);
93 
100 template <class Ranker>
101 std::unique_ptr<ranker> make_ranker(const cpptoml::table&)
102 {
103  return make_unique<Ranker>();
104 }
105 
112 template <class Ranker>
113 std::unique_ptr<ranker> make_ranker(const cpptoml::table& global,
114  const cpptoml::table& local)
115 {
116  (void)global;
117  return make_ranker<Ranker>(local);
118 }
119 
125 class ranker_loader : public util::factory<ranker_loader, ranker, std::istream&>
126 {
127  public:
128  friend base_factory;
129 
130  std::unique_ptr<language_model_ranker>
131  create_lm(util::string_view identifier, std::istream& in)
132  {
133  auto rnk = base_factory::create(identifier, in);
134  if (auto lmr = dynamic_cast<language_model_ranker*>(rnk.get()))
135  {
136  rnk.release();
137  return std::unique_ptr<language_model_ranker>{lmr};
138  }
139  throw std::invalid_argument{
140  "loaded ranker is not a language_model_ranker"};
141  }
142 
143  private:
147  ranker_loader();
148 
152  template <class Ranker>
153  void reg();
154 };
155 
159 std::unique_ptr<ranker> load_ranker(std::istream&);
160 
164 std::unique_ptr<language_model_ranker> load_lm_ranker(std::istream&);
165 
171 template <class Ranker>
172 std::unique_ptr<ranker> load_ranker(std::istream& in)
173 {
174  return make_unique<Ranker>(in);
175 }
176 
181 template <class Ranker>
183 {
184  ranker_factory::get().add(Ranker::id, [](const cpptoml::table& global,
185  const cpptoml::table& local) {
186  return make_ranker<Ranker>(global, local);
187  });
188  ranker_loader::get().add(Ranker::id, load_ranker<Ranker>);
189 }
190 }
191 }
192 #endif
void register_ranker()
Registration method for rankers.
Definition: ranker_factory.h:182
Factory that is responsible for creating rankers from configuration files.
Definition: ranker_factory.h:32
Generic factory that can be subclassed to create factories for specific types.
Definition: factory.h:38
A non-owning reference to a string.
Definition: string_view.h:51
Factory that is responsible for loading rankers from streams.
Definition: ranker_factory.h:125
std::unique_ptr< language_model_ranker > make_lm_ranker(const cpptoml::table &global, const cpptoml::table &local)
Convenience method for creating a language_model_ranker using the factory.
Definition: ranker_factory.cpp:65
std::unique_ptr< ranker > make_ranker(const cpptoml::table &global, const cpptoml::table &local)
Convenience method for creating a ranker using the factory.
Definition: ranker_factory.h:113
The ModErn Text Analysis toolkit is a suite of natural language processing, classification, information retrieval, data mining, and other applications of text processing.
Definition: analyzer.h:25
std::unique_ptr< language_model_ranker > load_lm_ranker(std::istream &)
Convenience method for loading a language_model_ranker using the factory.
Definition: ranker_factory.cpp:100
Definition: analyzer.h:20
friend base_factory
Friend the base ranker factory.
Definition: ranker_factory.h:38
std::unique_ptr< ranker > load_ranker(std::istream &)
Convenience method for loading a ranker using the factory.
Definition: ranker_factory.h:172