jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
keyword_weights.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 <cmath>
18 #include <string>
19 #include <sstream>
20 #include <utility>
21 #include "../common/type.hpp"
23 #include "keyword_weights.hpp"
24 
25 using std::string;
26 using std::pair;
27 using std::stringstream;
29 
30 namespace jubatus {
31 namespace core {
32 namespace fv_converter {
33 
34 namespace {
35 
36 struct is_zero {
37  bool operator()(const pair<string, float>& p) {
38  return p.second == 0;
39  }
40 };
41 
42 } // namespace
43 
45  : document_count_(),
46  document_frequencies_(),
47  weights_() {
48 }
49 
52  for (sfv_t::const_iterator it = fv.begin(); it != fv.end(); ++it) {
53  ++document_frequencies_[it->first];
54  }
55 }
56 
57 void keyword_weights::add_weight(const string& key, float weight) {
58  weights_[key] = weight;
59 }
60 
61 float keyword_weights::get_user_weight(const string& key) const {
62  weight_t::const_iterator wit = weights_.find(key);
63  if (wit != weights_.end()) {
64  return wit->second;
65  } else {
66  return 0;
67  }
68 }
69 
73  weight_t weights(w.weights_);
74  weights.insert(weights_.begin(), weights_.end());
75  weights_.swap(weights);
76 }
77 
79  document_count_ = 0;
81  weight_t().swap(weights_);
82 }
83 
85  stringstream ss;
86  ss << "document_count: " << document_count_
87  << " document_frequencies: " << document_frequencies_
88  << " weights: {";
89  for (weight_t::const_iterator it = weights_.begin();
90  it != weights_.end();
91  ++it) {
92  ss << it->first << " => " << it->second << std::endl;
93  }
94  ss << " }";
95  return ss.str();
96 }
97 
98 } // namespace fv_converter
99 } // namespace core
100 } // namespace jubatus
jubatus::util::data::unordered_map< std::string, float > weight_t
float get_user_weight(const std::string &key) const
void update_document_frequency(const common::sfv_t &fv)
void add_weight(const std::string &key, float weight)
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
void add(const counter< T > &counts)
Definition: counter.hpp:79