jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
recommender_mock_storage.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 <sstream>
18 #include <string>
19 #include <utility>
20 #include <vector>
22 
23 using std::istringstream;
24 using std::ostringstream;
25 using std::pair;
26 using std::string;
27 using std::vector;
28 
29 namespace jubatus {
30 namespace core {
31 namespace recommender {
32 
34 }
35 
37  const common::sfv_t& query,
38  const vector<pair<string, float> >& ids) {
39  similar_relation_[query] = ids;
40 }
41 
43  const common::sfv_t& query,
44  const vector<pair<string, float> >& ids) {
45  neighbor_relation_[query] = ids;
46 }
47 
49  const common::sfv_t& query,
50  vector<pair<string, float> >& ids,
51  size_t ret_num) const {
52  get_relation(query, similar_relation_, ret_num, ids);
53 }
54 
56  const common::sfv_t& query,
57  vector<pair<string, float> >& ids,
58  size_t ret_num) const {
59  get_relation(query, neighbor_relation_, ret_num, ids);
60 }
61 
63  const common::sfv_t& to) {
66 }
67 
69  similar_relation_.erase(query);
70  neighbor_relation_.erase(query);
71 }
72 
74  similar_relation_.clear();
75  neighbor_relation_.clear();
76 }
77 
79  return "recommender_mock_storage";
80 }
81 
83  packer.pack(*this);
84 }
85 
86 void recommender_mock_storage::unpack(msgpack::object o) {
87  o.convert(this);
88 }
89 
91  diff = *this;
92 }
93 
95  const recommender_mock_storage& mixed_diff) {
96  *this = mixed_diff;
97  return true;
98 }
99 
101  const recommender_mock_storage& lhs,
102  recommender_mock_storage& rhs) const {
105 }
106 
107 // private
108 
109 // static
111  const common::sfv_t& query,
112  const relation_type& relmap,
113  size_t ret_num,
114  vector<pair<string, float> >& ids) {
115  ids.clear();
116 
117  relation_type::const_iterator it = relmap.find(query);
118  if (it != relmap.end()) {
119  ids = it->second;
120  }
121 
122  if (ids.size() > ret_num) {
123  ids.resize(ret_num);
124  }
125 }
126 
127 // static
129  const common::sfv_t& from,
130  const common::sfv_t& to,
131  relation_type& relmap) {
132  relation_type::iterator it = relmap.find(from);
133  if (it != relmap.end()) {
134  relation_type::mapped_type val;
135  val.swap(it->second);
136  relmap.erase(it);
137  relmap[to].swap(val);
138  } else {
139  relmap[to]; // add default value
140  }
141 }
142 
143 // static
145  const relation_type& from,
146  relation_type& to) {
147  for (relation_type::const_iterator it = from.begin(); it != from.end();
148  ++it) {
149  to[it->first] = it->second;
150  }
151 }
152 
153 } // namespace recommender
154 } // namespace core
155 } // namespace jubatus
void update(const common::sfv_t &from, const common::sfv_t &to)
static void get_relation(const common::sfv_t &query, const relation_type &relmap, size_t ret_num, std::vector< std::pair< std::string, float > > &ids)
void set_neighbor_items(const common::sfv_t &query, const std::vector< std::pair< std::string, float > > &ids)
void set_similar_items(const common::sfv_t &query, const std::vector< std::pair< std::string, float > > &ids)
bool put_diff(const recommender_mock_storage &mixed_diff)
static void update_relation_key(const common::sfv_t &from, const common::sfv_t &to, relation_type &relmap)
void similar_items_similarity(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const
void mix(const recommender_mock_storage &lhs, recommender_mock_storage &rhs) const
void neighbor_items_distance(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
static void mix_relation(const relation_type &from, relation_type &to)
std::map< common::sfv_t, std::vector< std::pair< std::string, float > > > relation_type
void get_diff(recommender_mock_storage &diff) const