cpp-kana2 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright (C) 2023-2026 wolfgitpr
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,249 @@
1
+ Metadata-Version: 2.4
2
+ Name: cpp-kana2
3
+ Version: 2.0.0
4
+ Summary: Japanese kana <-> Hepburn romaji, over the cpp-kana2 C ABI
5
+ Home-page: https://github.com/wolfgitpr/cpp-kana
6
+ Author: wolfgitpr
7
+ License: Apache-2.0
8
+ Project-URL: Documentation, https://github.com/wolfgitpr/cpp-kana/blob/v2/docs/README.md
9
+ Project-URL: API reference, https://github.com/wolfgitpr/cpp-kana/blob/v2/docs/API.md
10
+ Project-URL: Source, https://github.com/wolfgitpr/cpp-kana/tree/v2
11
+ Project-URL: Changelog, https://github.com/wolfgitpr/cpp-kana/blob/v2/CHANGELOG.md
12
+ Project-URL: Issues, https://github.com/wolfgitpr/cpp-kana/issues
13
+ Keywords: japanese kana hiragana katakana romaji hepburn nlp
14
+ Classifier: Development Status :: 4 - Beta
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3 :: Only
17
+ Classifier: Programming Language :: C++
18
+ Classifier: License :: OSI Approved :: Apache Software License
19
+ Classifier: Natural Language :: Japanese
20
+ Classifier: Intended Audience :: Developers
21
+ Classifier: Topic :: Text Processing :: Linguistic
22
+ Classifier: Operating System :: Microsoft :: Windows
23
+ Classifier: Operating System :: POSIX :: Linux
24
+ Classifier: Operating System :: MacOS
25
+ Requires-Python: >=3.9
26
+ Description-Content-Type: text/markdown
27
+ License-File: LICENSE
28
+ Dynamic: author
29
+ Dynamic: classifier
30
+ Dynamic: description
31
+ Dynamic: description-content-type
32
+ Dynamic: home-page
33
+ Dynamic: keywords
34
+ Dynamic: license
35
+ Dynamic: license-file
36
+ Dynamic: project-url
37
+ Dynamic: requires-python
38
+ Dynamic: summary
39
+
40
+ # cpp-kana2
41
+
42
+ `cpp-kana2` converts Japanese kana to Hepburn romaji and back. Kana input may be hiragana, katakana,
43
+ half-width katakana, or a kana followed by a dakuten/handakuten mark, precomposed or combining; romaji input
44
+ accepts the Hepburn spellings plus the common input aliases. The library is C++17 and has no third-party
45
+ dependencies: the mora table is compiled in, and the tests use an assertion harness that ships with the tree.
46
+ It offers three calling surfaces — the C++ API, the C ABI, and the Python and C# bindings, which sit on top of
47
+ the C ABI.
48
+
49
+ ## Status
50
+
51
+ This is the `v2` branch. The package is `cpp-kana2`, the C++ namespace is `cpp_kana::v2`, the public headers
52
+ live under `<cpp-kana2/...>`, and the C surface carries its own tokens: `cppk2_` on symbols and typedefs,
53
+ `CPPK2_` on enum constants and macros. The version is `2.0.0`; this development line has not been released
54
+ yet, so that number is the contract for its first release rather than a bump of the v1 line.
55
+
56
+ It is a from-scratch rewrite that does not aim for behavioural compatibility with v1: v1's output is not a
57
+ contract here, and where v2 deliberately answers differently, the reason is recorded per case in
58
+ [docs/V1_DIFFERENCES.md](docs/V1_DIFFERENCES.md).
59
+
60
+ ## Quick start
61
+
62
+ ### C++
63
+
64
+ ```cpp
65
+ #include <cstdio>
66
+
67
+ #include <cpp-kana2/core/Convert.h>
68
+
69
+ int main() {
70
+ using namespace cpp_kana::v2;
71
+ const auto romanised = kanaToRomaji("きって");
72
+ if (!romanised.ok()) {
73
+ std::printf("%s\n", codeText(romanised.error().code));
74
+ return 1;
75
+ }
76
+ std::puts(romanised.value().rendered().c_str()); // kitte
77
+ return 0;
78
+ }
79
+ ```
80
+
81
+ A failure is a value (`Outcome<T>`), not an exception, so no `try`/`catch` is needed. `Conversion` also
82
+ exposes the per-unit breakdown through `moras()`. Linking is `target_link_libraries(app PRIVATE
83
+ cpp-kana2::cpp-kana2)`; the include directory is `include/`.
84
+
85
+ ### C
86
+
87
+ ```c
88
+ #include <stdio.h>
89
+
90
+ #include <cpp-kana2/ext/c_api.h>
91
+
92
+ int main(void) {
93
+ cppk2_conversion *conversion = NULL;
94
+ cppk2_error error = {0};
95
+ const int32_t status = cppk2_kana_to_romaji("きって", NULL, &conversion, &error);
96
+ if (status != CPPK2_OK) {
97
+ printf("%s\n", error.detail != NULL ? error.detail : "(no detail)");
98
+ cppk2_string_free(error.detail); /* ownership rule below */
99
+ return 1;
100
+ }
101
+ printf("%s\n", conversion->rendered);
102
+ cppk2_conversion_free(conversion);
103
+ return 0;
104
+ }
105
+ ```
106
+
107
+ Ownership rule: every allocation the library makes is freed by the library. A conversion (and every string
108
+ inside it) goes to `cppk2_conversion_free`; a detached string such as `cppk2_error.detail` goes to
109
+ `cppk2_string_free`. Calling the C runtime's `free()` on those pointers is heap corruption on Windows, not a
110
+ leak. Strings are NUL-terminated UTF-8 and failures are returned as status codes; no C++ exception crosses
111
+ the boundary.
112
+
113
+ ### Python
114
+
115
+ ```python
116
+ import cpp_kana2
117
+
118
+ cpp_kana2.kana_to_romaji("こんにちは").rendered
119
+ ```
120
+
121
+ ### C#
122
+
123
+ ```csharp
124
+ using CppKana2;
125
+
126
+ var romaji = Kana.KanaToRomaji("こんにちは").Rendered;
127
+ ```
128
+
129
+ ### Build and test from source
130
+
131
+ ```bash
132
+ cmake --preset default
133
+ cmake --build --preset default
134
+ ctest --preset default
135
+ ```
136
+
137
+ The `default` preset is a Release, shared-library build with tests and install enabled; `CMakePresets.json`
138
+ also defines `static` and `sanitize`. Using presets needs CMake 3.21 or newer, while the library itself only
139
+ needs 3.17. Build output goes under `.tmp/`.
140
+
141
+ Staging the Python package (builds the native library, writes `_version.py`, records the library's file
142
+ name):
143
+
144
+ ```bash
145
+ python python/tools/build_native.py
146
+ python -m pytest python/tests -q
147
+ ```
148
+
149
+ The C# suite (needs `dotnet`; it reuses the native library the Python step staged unless `--native-dir` is
150
+ given):
151
+
152
+ ```bash
153
+ python csharp/tools/run_tests.py
154
+ ```
155
+
156
+ ## Behaviour
157
+
158
+ The romanisation is Hepburn. There is no "scheme" switch: what varies between the ways people write the same
159
+ text down is captured by the policy enums in
160
+ [include/cpp-kana2/core/Types.h](include/cpp-kana2/core/Types.h).
161
+
162
+ Default options for `kanaToRomaji` (`RomajiOptions`):
163
+
164
+ | Option | Default | Meaning |
165
+ |---|---|---|
166
+ | `sokuon` | `SokuonPolicy::Geminate` | A sokuon doubles the next consonant: きって → `kitte`, まっちゃ → `matcha` |
167
+ | `hatsuon` | `HatsuonPolicy::Apostrophe` | A hatsuon before a vowel or `y` is written `n'`: しんよう → `shin'you` |
168
+ | `longVowel` | `LongVowelPolicy::Repeat` | A long vowel repeats the previous vowel: コーヒー → `koohii` |
169
+ | `letterCase` | `CasePolicy::Lower` | Lower-case output |
170
+ | `separator` | `SeparatorPolicy::None` | Compact output with no separator between morae |
171
+ | `unknown` | `UnknownPolicy::Keep` | A character the table does not know is kept and marked per mora |
172
+
173
+ `KanaOptions` (for `romajiToKana`) defaults to `Script::Hiragana` and `UnknownPolicy::Keep`.
174
+
175
+ The policy enums and their values:
176
+
177
+ - `Script`: `Hiragana` = 0, `Katakana` = 1, `AsIs` = 2 (`AsIs` applies to `convertScript` only).
178
+ - `SokuonPolicy`: `Geminate` = 0, `Drop` = 1 (renders nothing for the sokuon).
179
+ - `HatsuonPolicy`: `Apostrophe` = 0, `Plain` = 1 (always writes `n`).
180
+ - `LongVowelPolicy`: `Repeat` = 0, `Drop` = 1 (`kohi`), `KatakanaDash` = 2 (`ko-hi-`).
181
+ - `CasePolicy`: `Lower` = 0, `Upper` = 1, `Capitalize` = 2.
182
+ - `SeparatorPolicy`: `Space` = 0, `None` = 1.
183
+ - `UnknownPolicy`: `Keep` = 0, `Ignore` = 1, `Fail` = 2 (`Fail` makes the call fail instead of marking the
184
+ mora).
185
+
186
+ The C ABI mirrors these enums by number (`CPPK2_SOKUON_*`, `CPPK2_LONG_VOWEL_*`, ...), so a binding does not
187
+ need its own table. Per-unit results carry a `MoraKind` (`Syllable`, `Sokuon`, `Hatsuon`, `LongVowel`,
188
+ `Other`, `Archaic`, `Romaji`) and a per-unit `Code`, which is how a caller tells an ordinary syllable from a
189
+ kept unknown character without parsing the rendered string.
190
+
191
+ ## Gates
192
+
193
+ The repository-wide checks are Python scripts under `tools/` and are registered by name in
194
+ [tools/run_all_gates.py](tools/run_all_gates.py):
195
+
196
+ - `check_source_list` — the explicit source lists in `CMakeLists.txt` describe `src/` and `include/` in both
197
+ directions, so a file cannot join or leave the tree silently.
198
+ - `check_layers` — `src/` includes only point downwards through the layer table, and every `src/` file
199
+ belongs to a layer.
200
+ - `check_public_surface` — the public headers, the C ABI symbols and the namespace-level names match the
201
+ frozen lists in `tests/abi/`, in both directions.
202
+ - `check_bindings` — the Python and C# bindings transcribe the C ABI exactly: same entry points, same
203
+ structures, same field order.
204
+ - `check_version_tokens` — `project(cpp-kana2 VERSION ...)` is the single authority, and the version header,
205
+ the Python `_version.py`, the C# `version.props` and the CHANGELOG's top entry agree with it.
206
+ - `check_data_tables` — re-runs the table derivation and audits `data/moras.tsv` itself (duplicate kana,
207
+ ambiguous aliases, lying `secondary` flags, stray carriage returns).
208
+ - `check_conformance` — pins `conformance/cases.json` by its sha256 and checks the provenance field.
209
+ - `check_doc_links` — every relative link in every Markdown file resolves (a link to a generated, gitignored
210
+ file is a NOTE, not a failure).
211
+ - `check_doc_citations` — every `file:line` reference in the docs points at a line that still exists, so a
212
+ cleanup that deletes code cannot leave a citation pointing at nothing.
213
+ - `check_gate_inventory` — the gate registry, the scripts on disk and the `CPP_KANA2_GATES` list in
214
+ `tests/CMakeLists.txt` name the same gates.
215
+
216
+ Each gate is also a ctest test (`Gate_<name>`) when a Python 3 interpreter is found, and each one has a
217
+ `--self-test` that drives a synthetic failure through it. `tools/run_all_gates.py` runs the self-test before
218
+ the real check and stops the run if a self-test fails. When Python 3 is not found the gates are not
219
+ registered, and the C++ suite stays runnable on a compiler-only machine.
220
+
221
+ ctest also carries the non-Python assertions: `Smoke` and `CapiSmoke` (the C ABI smoke test is compiled as C),
222
+ `ConformanceFixture` (re-derives the fixture and compares it with the tracked bytes), `Gate_ExportGate` and its
223
+ self-test (the shared library's export table against the frozen symbol list), `Gate_SonameGate` on ELF, and
224
+ `Gate_InstallConsumer` (installs into a throwaway prefix and builds a consumer, including one that must be
225
+ refused for asking for a different major version).
226
+
227
+ ## Project layout
228
+
229
+ ```
230
+ include/cpp-kana2/core/ Convert.h Global.h Mora.h Types.h Utf8.h Version.h
231
+ include/cpp-kana2/ext/ c_api.h
232
+ src/infra/ UTF-8, Unicode and status-code text
233
+ src/data/ the compiled-in mora table (generated from data/moras.tsv)
234
+ src/mora/ normalisation, segmentation, kana -> romaji, romaji -> kana
235
+ src/core/ the public entry points
236
+ src/c_api/ the C ABI implementation
237
+ python/cpp_kana2/ the Python package (ctypes)
238
+ python/tests/ the pytest suite
239
+ csharp/CppKana2/ the C# binding (P/Invoke)
240
+ csharp/CppKana2.Tests/ the C# suite
241
+ tests/ cpp/ suites, abi/ frozen lists, install/ consumer check, data/corpus/
242
+ tools/ the gates, the table generator, the conformance emitter
243
+ data/ moras.tsv, the table's source
244
+ conformance/ cases.json + cases.sha256, the fixture all three suites read
245
+ ```
246
+
247
+ ## License
248
+
249
+ Apache License 2.0; see [LICENSE](LICENSE).
@@ -0,0 +1,237 @@
1
+ """cpp-kana2 for Python: a thin, honest shell over the C ABI.
2
+
3
+ Design rules, all of which exist because a binding that invents behaviour is worse than no binding:
4
+
5
+ * One-to-one with the C surface. ``kana_to_romaji``, ``romaji_to_kana``, ``convert_script``, ``version`` and
6
+ ``code_text`` are the whole API; the option objects mirror the option structs field for field.
7
+ * Failures raise, because that is what a Python caller expects -- and each status code maps to its own
8
+ exception class (see ``_errors``). The detail text always comes from the library, never from here.
9
+ * Every native allocation is released in a ``finally``. The conversion is copied into plain Python objects
10
+ before the native one is freed, so a returned object never points into freed memory.
11
+ * No hidden retries, no normalisation, no silent replacement characters: what the library says is what you
12
+ get.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import ctypes
18
+ from dataclasses import dataclass
19
+ from enum import IntEnum
20
+ from typing import Optional, Tuple
21
+
22
+ from . import _errors, _ffi, _native
23
+ from ._errors import ( # re-exported: callers should not have to import a private module to catch them
24
+ CppKanaError,
25
+ DataCorruptError,
26
+ InternalError,
27
+ InvalidArgumentError,
28
+ InvalidUtf8Error,
29
+ NotReadyError,
30
+ UnsupportedCharacterError,
31
+ )
32
+ from ._native import NativeLibraryError
33
+
34
+ __all__ = [
35
+ "Conversion",
36
+ "KanaOptions",
37
+ "Mora",
38
+ "MoraKind",
39
+ "RomajiOptions",
40
+ "Script",
41
+ "code_text",
42
+ "convert_script",
43
+ "kana_to_romaji",
44
+ "romaji_to_kana",
45
+ "version",
46
+ "CppKanaError",
47
+ "DataCorruptError",
48
+ "InternalError",
49
+ "InvalidArgumentError",
50
+ "InvalidUtf8Error",
51
+ "NativeLibraryError",
52
+ "NotReadyError",
53
+ "UnsupportedCharacterError",
54
+ ]
55
+
56
+ _library = _ffi.declare(_native.load())
57
+
58
+ try: # generated by tools/build_native.py; git-ignored
59
+ from ._version import __version__ # type: ignore
60
+ except ImportError: # pragma: no cover - a checkout nobody staged
61
+ __version__ = "0.0.0+unknown"
62
+
63
+
64
+ class MoraKind(IntEnum):
65
+ SYLLABLE = _ffi.MORA_SYLLABLE
66
+ SOKUON = _ffi.MORA_SOKUON
67
+ HATSUON = _ffi.MORA_HATSUON
68
+ LONG_VOWEL = _ffi.MORA_LONG_VOWEL
69
+ OTHER = _ffi.MORA_OTHER
70
+ ARCHAIC = _ffi.MORA_ARCHAIC
71
+ ROMAJI = _ffi.MORA_ROMAJI
72
+
73
+
74
+ class Script(IntEnum):
75
+ HIRAGANA = _ffi.SCRIPT_HIRAGANA
76
+ KATAKANA = _ffi.SCRIPT_KATAKANA
77
+ AS_IS = _ffi.SCRIPT_AS_IS
78
+
79
+
80
+ @dataclass(frozen=True)
81
+ class Mora:
82
+ """One unit of a conversion, copied out of the native structure."""
83
+
84
+ text: str
85
+ rendered: str
86
+ kind: MoraKind
87
+ code: int
88
+
89
+ @property
90
+ def ok(self) -> bool:
91
+ return self.code == _ffi.OK
92
+
93
+
94
+ @dataclass(frozen=True)
95
+ class Conversion:
96
+ """The whole result: the rendered string plus the per-unit breakdown."""
97
+
98
+ text: str
99
+ rendered: str
100
+ moras: Tuple[Mora, ...]
101
+ ok: bool
102
+
103
+ def __str__(self) -> str:
104
+ return self.rendered
105
+
106
+ def __len__(self) -> int:
107
+ return len(self.moras)
108
+
109
+
110
+ _SCRIPT_NAMES = {"hiragana": Script.HIRAGANA, "katakana": Script.KATAKANA, "as_is": Script.AS_IS}
111
+ _SOKUON_NAMES = {"geminate": _ffi.SOKUON_GEMINATE, "drop": _ffi.SOKUON_DROP}
112
+ _HATSUON_NAMES = {"apostrophe": _ffi.HATSUON_APOSTROPHE, "plain": _ffi.HATSUON_PLAIN}
113
+ _LONG_VOWEL_NAMES = {"repeat": _ffi.LONG_VOWEL_REPEAT, "drop": _ffi.LONG_VOWEL_DROP, "dash": _ffi.LONG_VOWEL_KATAKANA_DASH}
114
+ _CASE_NAMES = {"lower": _ffi.CASE_LOWER, "upper": _ffi.CASE_UPPER, "capitalize": _ffi.CASE_CAPITALIZE}
115
+ _SEPARATOR_NAMES = {"space": _ffi.SEPARATOR_SPACE, "none": _ffi.SEPARATOR_NONE}
116
+ _UNKNOWN_NAMES = {"keep": _ffi.UNKNOWN_KEEP, "ignore": _ffi.UNKNOWN_IGNORE, "fail": _ffi.UNKNOWN_FAIL}
117
+
118
+
119
+ def _lookup(table: dict, value, what: str) -> int:
120
+ if isinstance(value, int):
121
+ return int(value)
122
+ try:
123
+ return table[value]
124
+ except (KeyError, TypeError):
125
+ raise InvalidArgumentError(
126
+ _ffi.ERR_INVALID_ARGUMENT, f"{what}={value!r} is not one of {sorted(table)}"
127
+ ) from None
128
+
129
+
130
+ @dataclass(frozen=True)
131
+ class RomajiOptions:
132
+ """Mirrors ``cppk2_romaji_options``. Defaults are the library's own defaults."""
133
+
134
+ sokuon: str = "geminate"
135
+ hatsuon: str = "apostrophe"
136
+ long_vowel: str = "repeat"
137
+ case: str = "lower"
138
+ separator: str = "none"
139
+ unknown: str = "keep"
140
+
141
+ def _to_struct(self) -> _ffi.RomajiOptionsStruct:
142
+ struct = _ffi.RomajiOptionsStruct()
143
+ struct.struct_size = ctypes.sizeof(_ffi.RomajiOptionsStruct)
144
+ status = _library.cppk2_romaji_options_init(ctypes.byref(struct), struct.struct_size)
145
+ _errors.raise_for_status(status)
146
+ struct.sokuon = _lookup(_SOKUON_NAMES, self.sokuon, "sokuon")
147
+ struct.hatsuon = _lookup(_HATSUON_NAMES, self.hatsuon, "hatsuon")
148
+ struct.long_vowel = _lookup(_LONG_VOWEL_NAMES, self.long_vowel, "long_vowel")
149
+ struct.letter_case = _lookup(_CASE_NAMES, self.case, "case")
150
+ struct.separator = _lookup(_SEPARATOR_NAMES, self.separator, "separator")
151
+ struct.unknown = _lookup(_UNKNOWN_NAMES, self.unknown, "unknown")
152
+ return struct
153
+
154
+
155
+ @dataclass(frozen=True)
156
+ class KanaOptions:
157
+ """Mirrors ``cppk2_kana_options``."""
158
+
159
+ script: str = "hiragana"
160
+ unknown: str = "keep"
161
+
162
+ def _to_struct(self) -> _ffi.KanaOptionsStruct:
163
+ struct = _ffi.KanaOptionsStruct()
164
+ struct.struct_size = ctypes.sizeof(_ffi.KanaOptionsStruct)
165
+ status = _library.cppk2_kana_options_init(ctypes.byref(struct), struct.struct_size)
166
+ _errors.raise_for_status(status)
167
+ struct.script = int(_lookup(_SCRIPT_NAMES, self.script, "script"))
168
+ struct.unknown = _lookup(_UNKNOWN_NAMES, self.unknown, "unknown")
169
+ return struct
170
+
171
+
172
+ def _encode(text: str) -> bytes:
173
+ if not isinstance(text, str):
174
+ raise InvalidArgumentError(_ffi.ERR_INVALID_ARGUMENT, f"expected str, got {type(text).__name__}")
175
+ if "\x00" in text:
176
+ # The C ABI takes NUL-terminated strings; an embedded NUL would truncate silently.
177
+ raise InvalidArgumentError(_ffi.ERR_INVALID_ARGUMENT, "text must not contain U+0000")
178
+ return text.encode("utf-8")
179
+
180
+
181
+ def _convert(call) -> Conversion:
182
+ out = ctypes.POINTER(_ffi.ConversionStruct)()
183
+ error = _ffi.ErrorStruct()
184
+ status = call(out, error)
185
+ if status != _ffi.OK:
186
+ detail = _ffi.take_detail(_library, error)
187
+ _errors.raise_for_status(status, detail)
188
+ try:
189
+ native = out.contents
190
+ moras = tuple(
191
+ Mora(text=view.text, rendered=view.rendered, kind=MoraKind(view.kind), code=view.code)
192
+ for view in _ffi.moras_of(native)
193
+ )
194
+ return Conversion(
195
+ text=(native.text or b"").decode("utf-8", "replace"),
196
+ rendered=(native.rendered or b"").decode("utf-8", "replace"),
197
+ moras=moras,
198
+ ok=bool(native.ok),
199
+ )
200
+ finally:
201
+ _library.cppk2_conversion_free(out)
202
+
203
+
204
+ def kana_to_romaji(text: str, options: Optional[RomajiOptions] = None) -> Conversion:
205
+ """Kana (hiragana, katakana, half-width, combining dakuten) -> Hepburn romaji."""
206
+ data = _encode(text)
207
+ if options is None:
208
+ return _convert(lambda out, err: _library.cppk2_kana_to_romaji(data, None, out, err))
209
+ struct = options._to_struct()
210
+ return _convert(lambda out, err: _library.cppk2_kana_to_romaji(data, ctypes.byref(struct), out, err))
211
+
212
+
213
+ def romaji_to_kana(text: str, options: Optional[KanaOptions] = None) -> Conversion:
214
+ """Hepburn romaji (plus Kunrei/IME aliases) -> kana."""
215
+ data = _encode(text)
216
+ if options is None:
217
+ return _convert(lambda out, err: _library.cppk2_romaji_to_kana(data, None, out, err))
218
+ struct = options._to_struct()
219
+ return _convert(lambda out, err: _library.cppk2_romaji_to_kana(data, ctypes.byref(struct), out, err))
220
+
221
+
222
+ def convert_script(text: str, target: str = "hiragana", unknown: str = "keep") -> Conversion:
223
+ """Hiragana <-> katakana, with half-width input normalised first."""
224
+ data = _encode(text)
225
+ script = int(_lookup(_SCRIPT_NAMES, target, "target"))
226
+ unknown_code = _lookup(_UNKNOWN_NAMES, unknown, "unknown")
227
+ return _convert(lambda out, err: _library.cppk2_convert_script(data, script, unknown_code, out, err))
228
+
229
+
230
+ def version() -> str:
231
+ """The native library's package version (not this module's)."""
232
+ return _library.cppk2_version().decode("utf-8")
233
+
234
+
235
+ def code_text(code: int) -> str:
236
+ """The library's wording for a status code -- the only place that wording lives."""
237
+ return _library.cppk2_code_text(int(code)).decode("utf-8")