jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
recommender_mock.cpp
Go to the documentation of this file.
1 // Jubatus: Online machine learning framework for distributed environment
2 // Copyright (C) 2012 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 <utility>
19 #include <vector>
20 #include "recommender_mock.hpp"
21 
22 using std::istream;
23 using std::ostream;
24 using std::pair;
25 using std::string;
26 using std::vector;
27 
28 namespace jubatus {
29 namespace core {
30 namespace recommender {
31 
33 
35  : mixable_storage_(new mixable_recommender_mock_storage(
36  model_ptr(new recommender_mock_storage))) {
37 }
38 
40 }
41 
43  const common::sfv_t& query,
44  const vector<pair<string, float> >& ids) {
45  mixable_storage_->get_model()->set_similar_items(query, ids);
46 }
47 
49  const string& id,
50  const vector<pair<string, float> >& ids) {
51  common::sfv_t query;
52  decode_row(id, query);
53  set_similar_relation(query, ids);
54 }
55 
57  const common::sfv_t& query,
58  const vector<pair<string, float> >& ids) {
59  mixable_storage_->get_model()->set_neighbor_items(query, ids);
60 }
61 
63  const string& id,
64  const vector<pair<string, float> >& ids) {
65  common::sfv_t query;
66  decode_row(id, query);
67  set_neighbor_relation(query, ids);
68 }
69 
71  const common::sfv_t& query,
72  vector<pair<string, float> >& ids,
73  size_t ret_num) const {
74  mixable_storage_->get_model()->similar_items_similarity(query, ids, ret_num);
75 }
76 
78  const common::sfv_t& query,
79  vector<pair<string, float> >& ids,
80  size_t ret_num) const {
81  mixable_storage_->get_model()->neighbor_items_distance(query, ids, ret_num);
82 }
83 
85  mixable_storage_->get_model()->clear();
86  orig_.clear();
87 }
88 
89 void recommender_mock::clear_row(const string& id) {
90  common::sfv_t sfv;
91  decode_row(id, sfv);
92  mixable_storage_->get_model()->remove(sfv);
93 
94  orig_.remove_row(id);
95 }
96 
97 void recommender_mock::update_row(const string& id, const sfv_diff_t& diff) {
98  common::sfv_t old_sfv;
99  orig_.get_row(id, old_sfv);
100 
101  orig_.set_row(id, diff);
102  common::sfv_t new_sfv;
103  orig_.get_row(id, new_sfv);
104 
105  mixable_storage_->get_model()->update(old_sfv, new_sfv);
106 }
107 
108 void recommender_mock::get_all_row_ids(vector<string>& ids) const {
109  orig_.get_all_row_ids(ids);
110 }
111 
112 string recommender_mock::type() const {
113  return "recommender_mock";
114 }
115 
117  orig_.pack(packer);
118  mixable_storage_->get_model()->pack(packer);
119 }
120 
121 void recommender_mock::unpack(msgpack::object o) {
122  std::vector<msgpack::object> mems;
123  o.convert(&mems);
124  if (mems.size() != 2) {
125  throw msgpack::type_error();
126  }
127  orig_.unpack(mems[0]);
128  mixable_storage_->get_model()->unpack(mems[1]);
129 }
130 
132  return mixable_storage_.get();
133 }
134 
135 } // namespace recommender
136 } // namespace core
137 } // namespace jubatus
void pack(framework::packer &packer) const
void get_row(const std::string &row, std::vector< std::pair< std::string, float > > &columns) const
void set_similar_relation(const common::sfv_t &query, const std::vector< std::pair< std::string, float > > &ids)
void decode_row(const std::string &id, common::sfv_t &ret) const
virtual void similar_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const
jubatus::util::lang::shared_ptr< Model > model_ptr
core::common::sfv_t sfv_diff_t
virtual void clear_row(const std::string &id)
void pack(framework::packer &packer) const
virtual void neighbor_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const
virtual void get_all_row_ids(std::vector< std::string > &ids) const
void set_neighbor_relation(const common::sfv_t &query, const std::vector< std::pair< std::string, float > > &ids)
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
core::storage::sparse_matrix_storage orig_
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
void get_all_row_ids(std::vector< std::string > &ids) const
jubatus::util::lang::shared_ptr< mixable_recommender_mock_storage > mixable_storage_
virtual void update_row(const std::string &id, const sfv_diff_t &diff)
void set_row(const std::string &row, const std::vector< std::pair< std::string, float > > &columns)
storage::mixable_lsh_index_storage::model_ptr model_ptr
Definition: euclid_lsh.cpp:90