Skip to content

Instantly share code, notes, and snippets.

@t-abe
Last active December 18, 2015 07:28
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-abe/5746333 to your computer and use it in GitHub Desktop.
Save t-abe/5746333 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <fstream>
#include "../third_party/cmdline/cmdline.h"
#include "../common/exception.hpp"
#include "../storage/local_storage.hpp"
using std::vector;
using std::pair;
using jubatus::storage::local_storage;
using jubatus::storage::id_features3_t;
using jubatus::storage::id_feature_val3_t;
class mylocal_storage : public local_storage {
public:
id_features3_t& get_tbl() {
return tbl_;
}
jubatus::key_manager& get_class2id() {
return class2id_;
}
};
int main(int argc, char* argv[]){
cmdline::parser a;
a.add<std::string>("input", 'i', "model", true);
a.add<std::string>("label", 'l', "class", true);
a.parse_check(argc, argv);
std::ifstream ifs(a.get<std::string>("input").c_str());
if(ifs.fail()) throw std::runtime_error("Cannot open file : " + a.get<std::string>("input"));
mylocal_storage storage;
storage.load(ifs);
ifs.close();
vector<pair<double, std::string> > weights;
uint64_t label = storage.get_class2id().get_id(a.get<std::string>("label"));
id_features3_t& tbl = storage.get_tbl();
for(id_features3_t::const_iterator cit = tbl.begin(); cit != tbl.end(); ++cit){
const id_feature_val3_t& m = cit->second;
for (id_feature_val3_t::const_iterator it = m.begin(); it != m.end(); ++it) {
if(label == it->first){
weights.push_back(std::make_pair(it->second.v1, cit->first));
break;
}
}
}
std::partial_sort(weights.begin(), weights.begin() + 10, weights.end(), std::greater<pair<double, std::string> >());
for(vector<pair<double, std::string> >::const_iterator it=weights.begin();
it != weights.begin()+10; ++it){
std::cerr << it->first << "\t" << it->second << std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment