jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
inverted_index.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 "inverted_index.hpp"
18 
19 #include <algorithm>
20 #include <cmath>
21 #include <string>
22 #include <utility>
23 #include <vector>
24 #include "../common/exception.hpp"
25 #include "../common/vector_util.hpp"
26 #include "../storage/inverted_index_storage.hpp"
27 
28 using std::pair;
29 using std::string;
30 using std::vector;
31 
32 namespace jubatus {
33 namespace core {
34 namespace recommender {
35 
37  : mixable_storage_() {
38  typedef storage::inverted_index_storage ii_storage;
39  typedef storage::mixable_inverted_index_storage mii_storage;
40  jubatus::util::lang::shared_ptr<ii_storage> p(new ii_storage);
41  mixable_storage_.reset(new mii_storage(p));
42 }
43 
45 }
46 
48  const common::sfv_t& query,
49  std::vector<std::pair<std::string, float> >& ids,
50  size_t ret_num) const {
51  ids.clear();
52  if (ret_num == 0) {
53  return;
54  }
55  mixable_storage_->get_model()->calc_scores(query, ids, ret_num);
56 }
57 
59  const common::sfv_t& query,
60  vector<pair<string, float> >& ids,
61  size_t ret_num) const {
62  similar_row(query, ids, ret_num);
63  for (size_t i = 0; i < ids.size(); ++i) {
64  ids[i].second = 1 - ids[i].second;
65  }
66 }
67 
69  orig_.clear();
70  mixable_storage_->get_model()->clear();
71 }
72 
73 void inverted_index::clear_row(const std::string& id) {
74  vector<pair<string, float> > columns;
75  orig_.get_row(id, columns);
77  for (size_t i = 0; i < columns.size(); ++i) {
78  inv.remove(columns[i].first, id);
79  }
80  orig_.remove_row(id);
81 }
82 
83 void inverted_index::update_row(const std::string& id, const sfv_diff_t& diff) {
84  orig_.set_row(id, diff);
86  for (size_t i = 0; i < diff.size(); ++i) {
87  inv.set(diff[i].first, id, diff[i].second);
88  }
89 }
90 
91 void inverted_index::get_all_row_ids(std::vector<std::string>& ids) const {
92  mixable_storage_->get_model()->get_all_column_ids(ids); // inv.column = row
93 }
94 
95 string inverted_index::type() const {
96  return string("inverted_index");
97 }
98 
100  packer.pack_array(2);
101  orig_.pack(packer);
102  mixable_storage_->get_model()->pack(packer);
103 }
104 
105 void inverted_index::unpack(msgpack::object o) {
106  if (o.type != msgpack::type::ARRAY || o.via.array.size != 2) {
107  throw msgpack::type_error();
108  }
109  orig_.unpack(o.via.array.ptr[0]);
110  mixable_storage_->get_model()->unpack(o.via.array.ptr[1]);
111 }
112 
114  return mixable_storage_.get();
115 }
116 
117 } // namespace recommender
118 } // namespace core
119 } // namespace jubatus
framework::mixable * get_mixable() const
void get_row(const std::string &row, std::vector< std::pair< std::string, float > > &columns) const
void set(const std::string &row, const std::string &column, float val)
void get_all_row_ids(std::vector< std::string > &ids) const
core::common::sfv_t sfv_diff_t
void pack(framework::packer &packer) const
void neighbor_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const
void pack(framework::packer &packer) const
void similar_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, size_t ret_num) const
void remove(const std::string &row, const std::string &column)
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
void update_row(const std::string &id, const sfv_diff_t &diff)
core::storage::sparse_matrix_storage orig_
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
jubatus::util::lang::shared_ptr< storage::mixable_inverted_index_storage > mixable_storage_
void set_row(const std::string &row, const std::vector< std::pair< std::string, float > > &columns)