jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
nearest_neighbor_base.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 <utility>
19 #include <vector>
20 
21 #include "exception.hpp"
23 #include "../storage/column_table.hpp"
24 
25 using std::pair;
26 using std::string;
27 using std::vector;
28 using jubatus::util::lang::shared_ptr;
29 
30 namespace jubatus {
31 namespace core {
32 namespace nearest_neighbor {
33 
35  shared_ptr<storage::column_table> table,
36  const std::string& id)
37  : my_id_(id),
38  mixable_table_(new framework::mixable_versioned_table) {
39  mixable_table_->set_model(table);
40 }
41 
42 void nearest_neighbor_base::get_all_row_ids(vector<string>& ids) const {
43  vector<string> ret;
44  shared_ptr<const storage::column_table> table = get_const_table();
45  ret.reserve(table->size());
46  for (size_t i = 0; i < table->size(); ++i) {
47  ret.push_back(table->get_key(i));
48  }
49  ret.swap(ids);
50 }
51 
53  mixable_table_->get_model()->clear();
54 }
55 
57  const common::sfv_t& query,
58  vector<pair<string, float> >& ids,
59  uint64_t ret_num) const {
60  neighbor_row(query, ids, ret_num);
61  for (size_t i = 0; i < ids.size(); ++i) {
62  ids[i].second = calc_similarity(ids[i].second);
63  }
64 }
65 
67  const string& query_id,
68  vector<pair<string, float> >& ids,
69  uint64_t ret_num) const {
70  neighbor_row(query_id, ids, ret_num);
71  for (size_t i = 0; i < ids.size(); ++i) {
72  ids[i].second = calc_similarity(ids[i].second);
73  }
74 }
75 
77  get_const_table()->pack(packer);
78 }
79 
80 void nearest_neighbor_base::unpack(msgpack::object o) {
81  get_table()->unpack(o);
82 }
83 
85  return mixable_table_.get();
86 }
87 
88 } // namespace nearest_neighbor
89 } // namespcae core
90 } // namespace jubatus
jubatus::util::lang::shared_ptr< storage::column_table > get_table()
jubatus::util::lang::shared_ptr< framework::mixable_versioned_table > mixable_table_
virtual void neighbor_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, uint64_t ret_num) const =0
void get_all_row_ids(std::vector< std::string > &ids) const
msgpack::packer< jubatus_packer > packer
Definition: bandit_base.hpp:31
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
jubatus::util::lang::shared_ptr< const storage::column_table > get_const_table() const
nearest_neighbor_base(jubatus::util::lang::shared_ptr< storage::column_table > table, const std::string &id)
virtual void similar_row(const common::sfv_t &query, std::vector< std::pair< std::string, float > > &ids, uint64_t ret_num) const