| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2020-2026 Carbo Kuo and contributors |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include "Config.hpp" |
| 20 | #include "ResourceProvider.hpp" |
| 21 | #include "opencc.h" |
| 22 | #include <memory> |
| 23 | #include <pybind11/pybind11.h> |
| 24 | |
| 25 | namespace py = pybind11; |
| 26 | |
| 27 | PYBIND11_MODULE(opencc_clib, m) { |
| 28 | py::class_<opencc::SimpleConverter>(m, "_OpenCC") |
| 29 | .def(py::init([](const std::string& configFileName, |
| 30 | bool includeTofuRiskDictionaries, |
| 31 | py::object resourceZip) { |
| 32 | opencc::ConfigLoadOptions options; |
| 33 | options.includeTofuRiskDictionaries = includeTofuRiskDictionaries; |
| 34 | if (!resourceZip.is_none()) { |
| 35 | std::shared_ptr<opencc::ResourceProvider> provider( |
| 36 | new opencc::ZipResourceProvider( |
| 37 | resourceZip.cast<std::string>())); |
| 38 | return new opencc::SimpleConverter(configFileName, provider, |
| 39 | options); |
| 40 | } |
| 41 | return new opencc::SimpleConverter(configFileName, options); |
| 42 | }), |
| 43 | py::arg("config"), |
| 44 | py::arg("include_tofu_risk_dictionaries") = true, |
| 45 | py::arg("resource_zip") = py::none()) |
| 46 | .def("convert", py::overload_cast<const char*, size_t>( |
| 47 | &opencc::SimpleConverter::Convert, py::const_)); |
| 48 | |
| 49 | #ifdef OPENCC_VERSION |
| 50 | m.attr("__version__") = OPENCC_VERSION; |
| 51 | #else |
| 52 | m.attr("__version__") = "dev"; |
| 53 | #endif |
| 54 | } |