jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
num_filter_factory.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 <map>
18 #include <string>
19 #include "jubatus/util/lang/cast.h"
20 #include "exception.hpp"
21 #include "num_filter_factory.hpp"
22 #include "num_filter_impl.hpp"
23 #include "util.hpp"
24 
25 using jubatus::util::lang::shared_ptr;
26 
27 namespace jubatus {
28 namespace core {
29 namespace fv_converter {
30 
31 namespace {
32 shared_ptr<add_filter> create_add_filter(
33  const std::map<std::string, std::string>& params) {
34  const std::string& value = get_or_die(params, "value");
35  double float_val = jubatus::util::lang::lexical_cast<double>(value);
36  return shared_ptr<add_filter>(new add_filter(float_val));
37 }
38 
39 shared_ptr<linear_normalization_filter> create_linear_normalization_filter(
40  const std::map<std::string, std::string>& params) {
41  const std::string& min = get_or_die(params, "min");
42  const std::string& max = get_or_die(params, "max");
43  const std::string truncate = get_with_default(params, "truncate", "True");
44  const double float_min = jubatus::util::lang::lexical_cast<double>(min);
45  const double float_max = jubatus::util::lang::lexical_cast<double>(max);
46  const bool truncate_flag = truncate == "True";
47  return shared_ptr<linear_normalization_filter>(
48  new linear_normalization_filter(float_min, float_max, truncate_flag));
49 }
50 
51 shared_ptr<gaussian_normalization_filter> create_gaussian_normalization_filter(
52  const std::map<std::string, std::string>& params) {
53  const std::string& avg = get_or_die(params, "average");
54  const std::string& stddev = get_or_die(params, "standard_deviation");
55  const double float_avg = jubatus::util::lang::lexical_cast<double>(avg);
56  const double float_stddev = jubatus::util::lang::lexical_cast<double>(stddev);
57  return shared_ptr<gaussian_normalization_filter>(
58  new gaussian_normalization_filter(float_avg, float_stddev));
59 }
60 
61 shared_ptr<sigmoid_normalization_filter> create_sigmoid_normalization_filter(
62  const std::map<std::string, std::string>& params) {
63  const std::string gain = get_with_default(params, "gain", "1");
64  const std::string bias = get_with_default(params, "bias", "0");
65  const double float_gain = jubatus::util::lang::lexical_cast<double>(gain);
66  const double float_bias = jubatus::util::lang::lexical_cast<double>(bias);
67  return shared_ptr<sigmoid_normalization_filter>(
68  new sigmoid_normalization_filter(float_gain, float_bias));
69 }
70 } // namespace
71 
72 shared_ptr<num_filter> num_filter_factory::create(
73  const std::string& name,
74  const param_t& params) const {
75  num_filter* p;
76  if (name == "add") {
77  return create_add_filter(params);
78  } else if (name == "linear_normalization") {
79  return create_linear_normalization_filter(params);
80  } else if (name == "gaussian_normalization") {
81  return create_gaussian_normalization_filter(params);
82  } else if (name == "sigmoid_normalization") {
83  return create_sigmoid_normalization_filter(params);
84  } else if (ext_ && (p = ext_(name, params))) {
85  return shared_ptr<num_filter>(p);
86  } else {
87  throw JUBATUS_EXCEPTION(
88  converter_exception("unknonw num filter name: " + name));
89  }
90 }
91 
92 } // namespace fv_converter
93 } // namespace core
94 } // namespace jubatus
jubatus::util::lang::shared_ptr< num_filter > create(const std::string &name, const param_t &params) const
std::map< std::string, std::string > param_t
Definition: type.hpp:28
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
const std::string & get_or_die(const std::map< std::string, std::string > &params, const std::string &key)
Definition: util.cpp:28
std::string get_with_default(const std::map< std::string, std::string > &params, const std::string &key, const std::string &default_value)
Definition: util.cpp:39