text_pybind.cc 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "ultra_infer/pybind/main.h"
  15. namespace py = pybind11;
  16. using namespace py::literals;
  17. namespace ultra_infer {
  18. void BindUIE(py::module &m);
  19. py::dict ConvertUIEResultToDict(const text::UIEResult &self) {
  20. py::dict d;
  21. d["start"] = self.start_;
  22. d["end"] = self.end_;
  23. d["probability"] = self.probability_;
  24. d["text"] = self.text_;
  25. if (!self.relation_.empty()) {
  26. d["relation"] = py::dict();
  27. for (auto iter = self.relation_.begin(); iter != self.relation_.end();
  28. ++iter) {
  29. py::list l;
  30. for (auto result_iter = iter->second.begin();
  31. result_iter != iter->second.end(); ++result_iter) {
  32. l.append(ConvertUIEResultToDict(*result_iter));
  33. }
  34. d["relation"][iter->first.c_str()] = l;
  35. }
  36. }
  37. return d;
  38. }
  39. void BindText(py::module &m) {
  40. py::class_<text::UIEResult>(m, "UIEResult", py::dynamic_attr())
  41. .def(py::init())
  42. .def_readwrite("start", &text::UIEResult::start_)
  43. .def_readwrite("end", &text::UIEResult::end_)
  44. .def_readwrite("probability", &text::UIEResult::probability_)
  45. .def_readwrite("text", &text::UIEResult::text_)
  46. .def_readwrite("relation", &text::UIEResult::relation_)
  47. .def("get_dict",
  48. [](const text::UIEResult &self) {
  49. return ConvertUIEResultToDict(self);
  50. })
  51. .def("__repr__", &text::UIEResult::Str)
  52. .def("__str__", &text::UIEResult::Str);
  53. BindUIE(m);
  54. }
  55. } // namespace ultra_infer