28 #include "../common/exception.hpp"
29 #include "../common/jsonconfig.hpp"
30 #include "../recommender/euclid_lsh.hpp"
31 #include "../recommender/recommender_factory.hpp"
33 using jubatus::util::data::unordered_map;
34 using jubatus::util::data::unordered_set;
35 using jubatus::util::lang::shared_ptr;
37 using std::istringstream;
40 using std::numeric_limits;
42 using std::ostringstream;
56 reverse_nearest_neighbor_num(DEFAULT_REVERSE_NN_NUM) {
62 nn_engine_(recommender::recommender_factory::create_recommender(
65 recommender::euclid_lsh::
config())),
"")) {
69 shared_ptr<recommender::recommender_base> nn_engine)
70 : neighbor_num_(DEFAULT_NEIGHBOR_NUM),
71 reverse_nn_num_(DEFAULT_REVERSE_NN_NUM),
72 nn_engine_(nn_engine) {
77 shared_ptr<recommender::recommender_base> nn_engine)
79 reverse_nn_num_(config.reverse_nearest_neighbor_num),
80 nn_engine_(nn_engine) {
91 unordered_map<string, float>& neighbor_lrd)
const {
92 vector<pair<string, float> > neighbors;
104 unordered_map<string, float>& neighbor_lrd)
const {
105 vector<pair<string, float> > neighbors;
109 for (
size_t i = 0; i < neighbors.size(); ++i) {
110 if (neighbors[i].first ==
id) {
111 swap(neighbors[i], neighbors.back());
112 neighbors.pop_back();
136 unordered_set<string> update_set;
141 if (!query.empty()) {
149 update_set.insert(row);
155 return "lof_storage";
172 return it->second.kdist;
189 return it->second.lrd;
202 for (
size_t i = 0; i < ids.size(); ++i) {
205 for (
size_t i = 0; i < ids.size(); ++i) {
211 shared_ptr<recommender::recommender_base> nn_engine) {
220 for (lof_table_t::const_iterator it = mixed_diff.begin();
221 it != mixed_diff.end(); ++it) {
233 for (lof_table_t::const_iterator it = lhs.begin(); it != lhs.end(); ++it) {
251 return entry.
kdist < 0;
261 const vector<pair<string, float> >& neighbors,
262 unordered_map<string, float>& neighbor_lrd)
const {
263 if (neighbors.empty()) {
264 return numeric_limits<float>::infinity();
268 neighbor_lrd.clear();
269 for (
size_t i = 0; i < neighbors.size(); ++i) {
270 neighbor_lrd[neighbors[i].first] =
get_lrd(neighbors[i].first);
274 float sum_reachability = 0;
275 for (
size_t i = 0; i < neighbors.size(); ++i) {
276 sum_reachability += max(neighbors[i].second,
get_kdist(neighbors[i].first));
280 if (sum_reachability == 0) {
281 return numeric_limits<float>::infinity();
284 return neighbors.size() / sum_reachability;
292 unordered_set<string>& nn)
const {
293 vector<pair<string, float> > neighbors;
296 for (
size_t i = 0; i < neighbors.size(); ++i) {
297 nn.insert(neighbors[i].first);
307 typedef unordered_map<string, vector<pair<string, float> > >
308 rows_to_neighbors_type;
310 rows_to_neighbors_type rows_to_neighbors;
311 for (unordered_set<string>::const_iterator it = rows.begin();
312 it != rows.end(); ++it) {
316 for (rows_to_neighbors_type::const_iterator it = rows_to_neighbors.begin();
317 it != rows_to_neighbors.end(); ++it) {
320 for (rows_to_neighbors_type::const_iterator it = rows_to_neighbors.begin();
321 it != rows_to_neighbors.end(); ++it) {
330 vector<pair<string, float> > neighbors;
341 const vector<pair<string, float> >& neighbors) {
342 if (!neighbors.empty()) {
351 vector<pair<string, float> > neighbors;
360 const string& row,
const vector<pair<string, float> >& neighbors) {
361 if (neighbors.empty()) {
366 const size_t length = min(neighbors.size(), size_t(
neighbor_num_));
367 float sum_reachability = 0;
368 for (
size_t i = 0; i < length; ++i) {
369 sum_reachability += max(neighbors[i].second,
get_kdist(neighbors[i].first));
372 if (sum_reachability == 0) {
static bool is_removed(const lof_entry &entry)
float get_kdist(const std::string &row) const
void update_row(const std::string &row, const common::sfv_t &diff)
void mix(const lof_table_t &lhs, lof_table_t &rhs) const
float get_lrd(const std::string &row) const
static const uint32_t DEFAULT_NEIGHBOR_NUM
void update_kdist(const std::string &row)
static const uint32_t DEFAULT_REVERSE_NN_NUM
#define JUBATUS_EXCEPTION(e)
jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine_
void update_lrd_with_neighbors(const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)
lof_table_t lof_table_diff_
void get_diff(lof_table_t &diff) const
void update_entries(const jubatus::util::data::unordered_set< std::string > &rows)
bool put_diff(const lof_table_t &mixed_diff)
void swap(weighted_point &p1, weighted_point &p2)
void get_all_row_ids(std::vector< std::string > &ids) const
void set_nn_engine(jubatus::util::lang::shared_ptr< core::recommender::recommender_base > nn_engine)
void collect_neighbors(const std::string &row, jubatus::util::data::unordered_set< std::string > &nn) const
static void mark_removed(lof_entry &entry)
float collect_lrds(const common::sfv_t &query, jubatus::util::data::unordered_map< std::string, float > &neighbor_lrd) const
std::vector< std::pair< std::string, float > > sfv_t
void remove_row(const std::string &row)
bool has_row(const std::string &row) const
void update_kdist_with_neighbors(const std::string &row, const std::vector< std::pair< std::string, float > > &neighbors)
void update_lrd(const std::string &row)
jubatus::util::data::unordered_map< std::string, lof_entry > lof_table_t
float collect_lrds_from_neighbors(const std::vector< std::pair< std::string, float > > &neighbors, jubatus::util::data::unordered_map< std::string, float > &neighbor_lrd) const