jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
nearest_neighbor_recommender.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2013 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 
18 
19 #include <string>
20 #include <utility>
21 #include <vector>
22 #include "jubatus/util/data/serialization.h"
23 #include "jubatus/util/lang/shared_ptr.h"
24 #include "../common/exception.hpp"
25 #include "../nearest_neighbor/nearest_neighbor_base.hpp"
26 #include "../unlearner/unlearner.hpp"
27 
28 using jubatus::util::lang::shared_ptr;
29 
30 namespace jubatus {
31 namespace core {
32 namespace recommender {
33 
35  public:
37  : recommender_(recommender) {
38  }
39 
40  void operator()(const std::string& id) {
42  }
43 
44  private:
46 };
47 
49  jubatus::util::lang::shared_ptr<nearest_neighbor::nearest_neighbor_base>
50  nearest_neighbor_engine)
51  : nearest_neighbor_engine_(nearest_neighbor_engine) {
52 }
53 
55  jubatus::util::lang::shared_ptr<nearest_neighbor::nearest_neighbor_base>
56  nearest_neighbor_engine,
57  jubatus::util::lang::shared_ptr<unlearner::unlearner_base> unlearner)
58  : nearest_neighbor_engine_(nearest_neighbor_engine),
59  unlearner_(unlearner) {
60  unlearner_->set_callback(unlearning_callback(this));
61 }
62 
64  const common::sfv_t& query,
65  std::vector<std::pair<std::string, float> >& ids,
66  size_t ret_num) const {
67  nearest_neighbor_engine_->similar_row(query, ids, ret_num);
68 }
69 
71  const common::sfv_t& query,
72  std::vector<std::pair<std::string, float> >& ids,
73  size_t ret_num) const {
74  nearest_neighbor_engine_->neighbor_row(query, ids, ret_num);
75 }
76 
78  orig_.clear();
79  nearest_neighbor_engine_->clear();
80  if (unlearner_) {
81  unlearner_->clear();
82  }
83 }
84 
85 void nearest_neighbor_recommender::clear_row(const std::string& id) {
86  orig_.remove_row(id);
87  get_table()->delete_row(id);
88  if (unlearner_) {
89  unlearner_->remove(id);
90  }
91 }
92 
96 void nearest_neighbor_recommender::unlearn_row(const std::string& id) {
97  orig_.remove_row(id);
98  get_table()->delete_row(id);
99 }
100 
102  const std::string& id,
103  const common::sfv_t& diff) {
104  if (unlearner_) {
105  if (!unlearner_->touch(id)) {
107  "no more space available to add new ID: " + id));
108  }
109  }
110  orig_.set_row(id, diff);
111  common::sfv_t row;
112  orig_.get_row(id, row);
113  nearest_neighbor_engine_->set_row(id, row);
114 }
115 
117  std::vector<std::string>& ids) const {
118  nearest_neighbor_engine_->get_all_row_ids(ids);
119 }
120 
122  return "nearest_neighbor_recommender:" + nearest_neighbor_engine_->type();
123 }
124 
126  return nearest_neighbor_engine_->get_mixable();
127 }
128 
130  packer.pack_array(2);
131  orig_.pack(packer);
132  nearest_neighbor_engine_->pack(packer);
133 }
134 
135 void nearest_neighbor_recommender::unpack(msgpack::object o) {
136  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
137  throw msgpack::type_error();
138  }
139  orig_.unpack(o.via.array.ptr[0]);
140  nearest_neighbor_engine_->unpack(o.via.array.ptr[1]);
141 }
142 
143 jubatus::util::lang::shared_ptr<storage::column_table>
145  return nearest_neighbor_engine_->get_table();
146 }
147 
148 jubatus::util::lang::shared_ptr<const storage::column_table>
150  return nearest_neighbor_engine_->get_const_table();
151 }
152 
153 jubatus::util::lang::shared_ptr<unlearner::unlearner_base>
155  return unlearner_;
156 }
157 
158 } // namespace recommender
159 } // namespace core
160 } // namespace jubatus
void get_row(const std::string &row, std::vector< std::pair< std::string, float > > &columns) const
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > get_unlearner()
jubatus::util::lang::shared_ptr< storage::column_table > get_table()
jubatus::util::lang::shared_ptr< unlearner::unlearner_base > unlearner_
void update_row(const std::string &id, const sfv_diff_t &diff)
void neighbor_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
jubatus::util::data::optional< std::string > unlearner
void pack(framework::packer &packer) const
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
void similar_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const
core::storage::sparse_matrix_storage orig_
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
nearest_neighbor_recommender(jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine)
jubatus::util::lang::shared_ptr< const storage::column_table > get_const_table() const
jubatus::util::lang::function< void(std::string)> unlearning_callback
void set_row(const std::string &row, const std::vector< std::pair< std::string, float > > &columns)
jubatus::util::lang::shared_ptr< nearest_neighbor::nearest_neighbor_base > nearest_neighbor_engine_