Last active
April 8, 2017 08:31
-
-
Save TkrUdagawa/44d93ccf3d605ccea14eec50ba3ddc1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <vector> | |
#include <utility> | |
#include <string> | |
#include <iostream> | |
#include <jubatus/util/lang/shared_ptr.h> | |
#include <jubatus/core/common/jsonconfig.hpp> | |
#include <jubatus/core/fv_converter/datum.hpp> | |
#include <jubatus/core/fv_converter/datum_to_fv_converter.hpp> | |
#include <jubatus/core/fv_converter/converter_config.hpp> | |
#include <jubatus/core/driver/recommender.hpp> | |
#include <jubatus/core/recommender/recommender_factory.hpp> | |
using jubatus::util::lang::shared_ptr; | |
using jubatus::util::text::json::json; | |
using jubatus::util::text::json::json_object; | |
using jubatus::util::text::json::to_json; | |
jubatus::util::lang::shared_ptr<jubatus::core::fv_converter::datum_to_fv_converter> | |
make_fv_converter() { | |
jubatus::util::lang::shared_ptr<jubatus::core::fv_converter::datum_to_fv_converter> | |
converter( | |
new jubatus::core::fv_converter::datum_to_fv_converter); | |
jubatus::core::fv_converter::string_rule str_rule; | |
str_rule.key = "*"; | |
str_rule.type = "str"; | |
str_rule.sample_weight = "bin"; | |
str_rule.global_weight = "bin"; | |
jubatus::core::fv_converter::num_rule num_rule; | |
num_rule.key = "*"; | |
num_rule.type = "num"; | |
jubatus::core::fv_converter::converter_config c; | |
c.string_rules = std::vector<jubatus::core::fv_converter::string_rule>(); | |
c.string_rules->push_back(str_rule); | |
c.num_rules = std::vector<jubatus::core::fv_converter::num_rule>(); | |
c.num_rules->push_back(num_rule); | |
jubatus::core::fv_converter::initialize_converter(c, *converter); | |
return converter; | |
} | |
int main() { | |
json jsconf(new json_object); | |
jubatus::core::common::jsonconfig::config conf(jsconf); | |
shared_ptr<jubatus::core::driver::recommender> p; | |
p.reset(new jubatus::core::driver::recommender( | |
jubatus::core::recommender::recommender_factory::create_recommender( | |
"inverted_index", conf, "test"), | |
make_fv_converter())); | |
std::vector<float> v; | |
v.push_back(1.1); | |
v.push_back(1.1); | |
v.push_back(0.0001); | |
for (int j = 0; j < v.size(); j++) { | |
jubatus::core::fv_converter::datum d; | |
for (int i = 0; i < 10; i++) { | |
std::stringstream os; | |
os << "x" << i; | |
std::pair<std::string, float> kv(os.str(), v[j]); | |
d.num_values_.push_back(kv); | |
} | |
std::stringstream id; | |
id << "id" << j; | |
p->update_row(id.str(), d); | |
} | |
std::vector<std::pair<std::string, float> > res; | |
res = p->similar_row_from_id("id0", 10); | |
for (int i = 0; i < res.size(); i++) { | |
std::cout << std::setprecision(8) << res[i].first << ":" << res[i].second << std::endl; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test: inverted_index_test.cpp | |
g++ -O2 -o test inverted_index_test.cpp `pkg-config jubatus-client --libs --cflags jubatus jubatus_core` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jubatus | |
from jubatus.common import Datum | |
client = jubatus.Recommender("127.0.0.1", 9199, "") | |
dim = 10 | |
dat = [1.1, 1.1, 0.0001] | |
for i, v in enumerate(dat): | |
d = Datum() | |
for j in range(10): | |
n = "x{}".format(j) | |
d.add_number(n, v) | |
client.update_row("id{}".format(i), d) | |
res = client.similar_row_from_id("id0", 10) | |
for r in res: | |
print(res) |
$ python recommender.py
[id_with_score{id: id2, score: 1.0000001192092896}, id_with_score{id: id1, score: 1.0}, id_with_score{id: id0, score: 1.0}]
[id_with_score{id: id2, score: 1.0000001192092896}, id_with_score{id: id1, score: 1.0}, id_with_score{id: id0, score: 1.0}]
[id_with_score{id: id2, score: 1.0000001192092896}, id_with_score{id: id1, score: 1.0}, id_with_score{id: id0, score: 1.0}]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ ./test
id2:1.0000001
id1:1
id0:1