|
@@ -143,7 +143,15 @@ PYBIND11_MODULE(fasttext_pybind, m) {
|
|
|
|
|
|
|
|
py::enum_<fasttext::metric_name>(m, "metric_name")
|
|
py::enum_<fasttext::metric_name>(m, "metric_name")
|
|
|
.value("f1score", fasttext::metric_name::f1score)
|
|
.value("f1score", fasttext::metric_name::f1score)
|
|
|
- .value("labelf1score", fasttext::metric_name::labelf1score)
|
|
|
|
|
|
|
+ .value("f1scoreLabel", fasttext::metric_name::f1scoreLabel)
|
|
|
|
|
+ .value("precisionAtRecall", fasttext::metric_name::precisionAtRecall)
|
|
|
|
|
+ .value(
|
|
|
|
|
+ "precisionAtRecallLabel",
|
|
|
|
|
+ fasttext::metric_name::precisionAtRecallLabel)
|
|
|
|
|
+ .value("recallAtPrecision", fasttext::metric_name::recallAtPrecision)
|
|
|
|
|
+ .value(
|
|
|
|
|
+ "recallAtPrecisionLabel",
|
|
|
|
|
+ fasttext::metric_name::recallAtPrecisionLabel)
|
|
|
.export_values();
|
|
.export_values();
|
|
|
|
|
|
|
|
m.def(
|
|
m.def(
|
|
@@ -186,6 +194,34 @@ PYBIND11_MODULE(fasttext_pybind, m) {
|
|
|
sizeof(fasttext::real) * (int64_t)1});
|
|
sizeof(fasttext::real) * (int64_t)1});
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ py::class_<fasttext::Meter>(m, "Meter")
|
|
|
|
|
+ .def(py::init<bool>())
|
|
|
|
|
+ .def("scoreVsTrue", &fasttext::Meter::scoreVsTrue)
|
|
|
|
|
+ .def(
|
|
|
|
|
+ "precisionRecallCurveLabel",
|
|
|
|
|
+ py::overload_cast<int32_t>(
|
|
|
|
|
+ &fasttext::Meter::precisionRecallCurve, py::const_))
|
|
|
|
|
+ .def(
|
|
|
|
|
+ "precisionRecallCurve",
|
|
|
|
|
+ py::overload_cast<>(
|
|
|
|
|
+ &fasttext::Meter::precisionRecallCurve, py::const_))
|
|
|
|
|
+ .def(
|
|
|
|
|
+ "precisionAtRecallLabel",
|
|
|
|
|
+ py::overload_cast<int32_t, double>(
|
|
|
|
|
+ &fasttext::Meter::precisionAtRecall, py::const_))
|
|
|
|
|
+ .def(
|
|
|
|
|
+ "precisionAtRecall",
|
|
|
|
|
+ py::overload_cast<double>(
|
|
|
|
|
+ &fasttext::Meter::precisionAtRecall, py::const_))
|
|
|
|
|
+ .def(
|
|
|
|
|
+ "recallAtPrecisionLabel",
|
|
|
|
|
+ py::overload_cast<int32_t, double>(
|
|
|
|
|
+ &fasttext::Meter::recallAtPrecision, py::const_))
|
|
|
|
|
+ .def(
|
|
|
|
|
+ "recallAtPrecision",
|
|
|
|
|
+ py::overload_cast<double>(
|
|
|
|
|
+ &fasttext::Meter::recallAtPrecision, py::const_));
|
|
|
|
|
+
|
|
|
py::class_<fasttext::FastText>(m, "fasttext")
|
|
py::class_<fasttext::FastText>(m, "fasttext")
|
|
|
.def(py::init<>())
|
|
.def(py::init<>())
|
|
|
.def("getArgs", &fasttext::FastText::getArgs)
|
|
.def("getArgs", &fasttext::FastText::getArgs)
|
|
@@ -231,20 +267,33 @@ PYBIND11_MODULE(fasttext_pybind, m) {
|
|
|
[](fasttext::FastText& m, std::string s) { m.saveModel(s); })
|
|
[](fasttext::FastText& m, std::string s) { m.saveModel(s); })
|
|
|
.def(
|
|
.def(
|
|
|
"test",
|
|
"test",
|
|
|
- [](fasttext::FastText& m,
|
|
|
|
|
- const std::string filename,
|
|
|
|
|
- int32_t k,
|
|
|
|
|
- fasttext::real threshold) {
|
|
|
|
|
|
|
+ [](fasttext::FastText& m,
|
|
|
|
|
+ const std::string& filename,
|
|
|
|
|
+ int32_t k,
|
|
|
|
|
+ fasttext::real threshold) {
|
|
|
std::ifstream ifs(filename);
|
|
std::ifstream ifs(filename);
|
|
|
if (!ifs.is_open()) {
|
|
if (!ifs.is_open()) {
|
|
|
throw std::invalid_argument("Test file cannot be opened!");
|
|
throw std::invalid_argument("Test file cannot be opened!");
|
|
|
}
|
|
}
|
|
|
- fasttext::Meter meter;
|
|
|
|
|
|
|
+ fasttext::Meter meter(false);
|
|
|
m.test(ifs, k, threshold, meter);
|
|
m.test(ifs, k, threshold, meter);
|
|
|
ifs.close();
|
|
ifs.close();
|
|
|
return std::tuple<int64_t, double, double>(
|
|
return std::tuple<int64_t, double, double>(
|
|
|
meter.nexamples(), meter.precision(), meter.recall());
|
|
meter.nexamples(), meter.precision(), meter.recall());
|
|
|
})
|
|
})
|
|
|
|
|
+ .def(
|
|
|
|
|
+ "getMeter",
|
|
|
|
|
+ [](fasttext::FastText& m, const std::string& filename, int32_t k) {
|
|
|
|
|
+ std::ifstream ifs(filename);
|
|
|
|
|
+ if (!ifs.is_open()) {
|
|
|
|
|
+ throw std::invalid_argument("Test file cannot be opened!");
|
|
|
|
|
+ }
|
|
|
|
|
+ fasttext::Meter meter(true);
|
|
|
|
|
+ m.test(ifs, k, 0.0, meter);
|
|
|
|
|
+ ifs.close();
|
|
|
|
|
+
|
|
|
|
|
+ return meter;
|
|
|
|
|
+ })
|
|
|
.def(
|
|
.def(
|
|
|
"getSentenceVector",
|
|
"getSentenceVector",
|
|
|
[](fasttext::FastText& m,
|
|
[](fasttext::FastText& m,
|
|
@@ -397,7 +446,7 @@ PYBIND11_MODULE(fasttext_pybind, m) {
|
|
|
if (!ifs.is_open()) {
|
|
if (!ifs.is_open()) {
|
|
|
throw std::invalid_argument("Test file cannot be opened!");
|
|
throw std::invalid_argument("Test file cannot be opened!");
|
|
|
}
|
|
}
|
|
|
- fasttext::Meter meter;
|
|
|
|
|
|
|
+ fasttext::Meter meter(false);
|
|
|
m.test(ifs, k, threshold, meter);
|
|
m.test(ifs, k, threshold, meter);
|
|
|
std::shared_ptr<const fasttext::Dictionary> d = m.getDictionary();
|
|
std::shared_ptr<const fasttext::Dictionary> d = m.getDictionary();
|
|
|
std::unordered_map<std::string, py::dict> returnedValue;
|
|
std::unordered_map<std::string, py::dict> returnedValue;
|
|
@@ -412,7 +461,7 @@ PYBIND11_MODULE(fasttext_pybind, m) {
|
|
|
})
|
|
})
|
|
|
.def(
|
|
.def(
|
|
|
"getWordId",
|
|
"getWordId",
|
|
|
- [](fasttext::FastText& m, const std::string word) {
|
|
|
|
|
|
|
+ [](fasttext::FastText& m, const std::string& word) {
|
|
|
return m.getWordId(word);
|
|
return m.getWordId(word);
|
|
|
})
|
|
})
|
|
|
.def(
|
|
.def(
|
|
@@ -420,6 +469,11 @@ PYBIND11_MODULE(fasttext_pybind, m) {
|
|
|
[](fasttext::FastText& m, const std::string word) {
|
|
[](fasttext::FastText& m, const std::string word) {
|
|
|
return m.getSubwordId(word);
|
|
return m.getSubwordId(word);
|
|
|
})
|
|
})
|
|
|
|
|
+ .def(
|
|
|
|
|
+ "getLabelId",
|
|
|
|
|
+ [](fasttext::FastText& m, const std::string& label) {
|
|
|
|
|
+ return m.getLabelId(label);
|
|
|
|
|
+ })
|
|
|
.def(
|
|
.def(
|
|
|
"getInputVector",
|
|
"getInputVector",
|
|
|
[](fasttext::FastText& m, fasttext::Vector& vec, int32_t ind) {
|
|
[](fasttext::FastText& m, fasttext::Vector& vec, int32_t ind) {
|