jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
recommender_factory.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2011 Preferred Networks and Nippon Telegraph and Telephone Corporation.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License version 2.1 as published by the Free Software Foundation.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 
17 #include <string>
18 #include "jubatus/util/data/string/utility.h"
19 #include "jubatus/util/lang/shared_ptr.h"
20 #include "jubatus/util/text/json.h"
21 #include "../common/exception.hpp"
22 #include "../common/jsonconfig.hpp"
23 #include "../nearest_neighbor/nearest_neighbor_factory.hpp"
24 #include "../storage/column_table.hpp"
25 #include "../unlearner/unlearner_factory.hpp"
26 #include "recommender_factory.hpp"
27 #include "recommender.hpp"
28 
29 using std::string;
30 using jubatus::util::text::json::json;
31 using jubatus::util::lang::shared_ptr;
32 using jubatus::util::data::string::starts_with;
35 
36 namespace jubatus {
37 namespace core {
38 namespace recommender {
39 namespace {
40 
41 const std::string NEAREST_NEIGHBOR_PREFIX("nearest_neighbor_recommender:");
42 struct nearest_neighbor_recommender_config {
43  std::string method;
45  jubatus::util::data::optional<std::string> unlearner;
46  jubatus::util::data::optional<config> unlearner_parameter;
47 
48  template<typename Ar>
49  void serialize(Ar& ar) {
50  ar & JUBA_MEMBER(method) & JUBA_MEMBER(parameter) &
51  JUBA_MEMBER(unlearner) & JUBA_MEMBER(unlearner_parameter);
52  }
53 };
54 } // namespace
55 
56 shared_ptr<recommender_base> recommender_factory::create_recommender(
57  const string& name,
58  const config& param,
59  const string& id) {
60  if (name == "inverted_index") {
61  // inverted_index doesn't have parameter
62  return shared_ptr<recommender_base>(new inverted_index);
63  } else if (name == "minhash") {
64  return shared_ptr<recommender_base>(
65  new minhash(config_cast_check<minhash::config>(param)));
66  } else if (name == "lsh") {
67  return shared_ptr<recommender_base>(
68  new lsh(config_cast_check<lsh::config>(param)));
69  } else if (name == "euclid_lsh") {
70  return shared_ptr<recommender_base>(
71  new euclid_lsh(config_cast_check<euclid_lsh::config>(param)));
72  } else if (name == "nearest_neighbor_recommender") {
73  nearest_neighbor_recommender_config conf =
74  config_cast_check<nearest_neighbor_recommender_config>(param);
75  shared_ptr<storage::column_table> table(new storage::column_table);
76  shared_ptr<nearest_neighbor::nearest_neighbor_base>
77  nearest_neighbor_engine(nearest_neighbor::create_nearest_neighbor(
78  conf.method, conf.parameter, table, id));
79  if (conf.unlearner) {
80  if (!conf.unlearner_parameter) {
81  throw JUBATUS_EXCEPTION(
83  "unlearner is set but unlearner_parameter is not found"));
84  }
85  shared_ptr<unlearner::unlearner_base> unl(unlearner::create_unlearner(
86  *conf.unlearner, common::jsonconfig::config(
87  *conf.unlearner_parameter)));
88  return shared_ptr<recommender_base>(
89  new nearest_neighbor_recommender(nearest_neighbor_engine, unl));
90  }
91  return shared_ptr<recommender_base>(
92  new nearest_neighbor_recommender(nearest_neighbor_engine));
93  } else {
95  }
96 }
97 
98 } // namespace recommender
99 } // namespace core
100 } // namespace jubatus
101 
T config_cast_check(const config &c)
Definition: cast.hpp:311
void serialize(member_collector &mem, jubatus::util::data::serialization::named_value< T > &v)
Definition: cast.hpp:54
config parameter
static jubatus::util::lang::shared_ptr< recommender_base > create_recommender(const std::string &name, const common::jsonconfig::config &param, const std::string &id)
shared_ptr< unlearner_base > create_unlearner(const std::string &name, const common::jsonconfig::config &config)
jubatus::util::data::optional< config > unlearner_parameter
jubatus::util::data::optional< std::string > unlearner
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
std::string method
shared_ptr< nearest_neighbor_base > create_nearest_neighbor(const std::string &name, const common::jsonconfig::config &config, shared_ptr< storage::column_table > table, const std::string &id)