jubatus_core  0.1.2
Jubatus: Online machine learning framework for distributed environment
libsvm_converter.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 <sstream>
18 #include <string>
19 #include "jubatus/util/lang/cast.h"
20 #include "datum.hpp"
21 #include "exception.hpp"
22 #include "libsvm_converter.hpp"
23 
24 namespace jubatus {
25 namespace core {
26 namespace fv_converter {
27 
29  const std::string& line,
30  datum& ret_datum,
31  std::string& ret_label) {
32  std::string label;
33  std::istringstream in(line);
34  in >> label;
35  datum::nv_t num_values;
36 
37  std::string s;
38  while (in) {
39  in >> s;
40  if (!in) {
41  break;
42  }
43  size_t p = s.find(':');
44  if (p == std::string::npos) {
45  throw JUBATUS_EXCEPTION(
46  converter_exception("invalid libsvm format: " + s));
47  }
48  std::string id = s.substr(0, p);
49  float val = jubatus::util::lang::lexical_cast<float>(s.substr(p + 1));
50  num_values.push_back(make_pair(id, val));
51  }
52 
53  ret_label.swap(label);
54  ret_datum.string_values_.clear();
55  ret_datum.num_values_.swap(num_values);
56 }
57 
58 } // namespace fv_converter
59 } // namespace core
60 } // namespace jubatus
static void convert(const std::string &line, datum &ret_datum, std::string &ret_label)
#define JUBATUS_EXCEPTION(e)
Definition: exception.hpp:79
std::vector< std::pair< std::string, double > > nv_t
Definition: datum.hpp:33