jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
revert.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 "revert.hpp"
18 
19 #include <string>
20 #include <utility>
21 #include "jubatus/util/data/string/utility.h"
22 #include "jubatus/util/lang/cast.h"
23 #include "datum.hpp"
24 
25 using jubatus::util::data::string::starts_with;
26 
27 namespace jubatus {
28 namespace core {
29 namespace fv_converter {
30 
32  for (size_t i = 0; i < fv.size(); ++i) {
33  std::pair<std::string, float> num_value;
34  std::pair<std::string, std::string> string_value;
35  if (revert_num_value(fv[i], num_value)) {
36  data.num_values_.push_back(num_value);
37  } else if (revert_string_value(fv[i], string_value)) {
38  data.string_values_.push_back(string_value);
39  }
40  }
41 }
42 
44  const std::pair<std::string, float>& feature,
45  std::pair<std::string, float>& num_value) {
46  // Only 'num' features and 'str' features can be reverted.
47  // Formats of two features are below:
48  // ("<KEY_NAME>@num", value)
49  // ("<KEY_NAME>@str$<VALUE>", 1)
50  const std::string& key = feature.first;
51  float value = feature.second;
52  size_t at = key.rfind('@');
53  if (at == std::string::npos) {
54  return false;
55  }
56  std::string num_value_key(key.substr(0, at));
57  std::string feature_value(key.substr(at + 1));
58  const std::string str_prefix = "str$";
59  if (feature_value == "num") {
60  num_value.first.swap(num_value_key);
61  num_value.second = value;
62  return true;
63  } else if (starts_with(feature_value, str_prefix)) {
64  std::string val_string(feature_value.substr(str_prefix.size()));
65  try {
66  float val = jubatus::util::lang::lexical_cast<float>(val_string);
67  num_value.first.swap(num_value_key);
68  num_value.second = val;
69  return true;
70  } catch (const std::bad_cast&) {
71  return false;
72  }
73  } else {
74  return false;
75  }
76 }
77 
79  const std::pair<std::string, float>& feature,
80  std::pair<std::string, std::string>& string_value) {
81  // Format of string feature is
82  // "<KEY_NAME>$<VALUE>@<FEATURE_TYPE>#<SAMPLE_WEIGHT>/<GLOBAL_WEIGHT>"
83  const std::string& f = feature.first;
84  size_t sharp = f.rfind('#');
85  if (sharp == std::string::npos) {
86  return false;
87  }
88  size_t at = f.rfind('@', sharp);
89  if (at == std::string::npos) {
90  return false;
91  }
92  size_t dollar = f.find('$');
93  if (dollar == std::string::npos) {
94  return false;
95  }
96  if (f.substr(at + 1, sharp - at - 1) != "str") {
97  return false;
98  }
99 
100  std::string key(f.substr(0, dollar));
101  std::string value(f.substr(dollar + 1, at - dollar - 1));
102 
103  string_value.first.swap(key);
104  string_value.second.swap(value);
105  return true;
106 }
107 
108 } // namespace fv_converter
109 } // namespace core
110 } // namespace jubatus
void revert_feature(const common::sfv_t &fv, fv_converter::datum &data)
Definition: revert.cpp:31
std::vector< std::pair< std::string, float > > sfv_t
Definition: type.hpp:29
bool revert_string_value(const std::pair< std::string, float > &feature, std::pair< std::string, std::string > &string_value)
Definition: revert.cpp:78
bool revert_num_value(const std::pair< std::string, float > &feature, std::pair< std::string, float > &num_value)
Definition: revert.cpp:43