scriptconv 0.0.4a5__tar.gz → 0.0.4a6__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.
Files changed (97) hide show
  1. {scriptconv-0.0.4a5/scriptconv.egg-info → scriptconv-0.0.4a6}/PKG-INFO +1 -1
  2. scriptconv-0.0.4a6/scriptconv/diacritics.py +319 -0
  3. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/base.py +4 -177
  4. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/registry.py +2 -5
  5. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/version.py +1 -1
  6. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6/scriptconv.egg-info}/PKG-INFO +1 -1
  7. scriptconv-0.0.4a6/tests/test_diacritics.py +201 -0
  8. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_diacritics_graph.py +1 -2
  9. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_phonemizers_base.py +0 -100
  10. scriptconv-0.0.4a5/scriptconv/diacritics.py +0 -162
  11. scriptconv-0.0.4a5/tests/test_diacritics.py +0 -90
  12. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/LICENSE +0 -0
  13. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/README.md +0 -0
  14. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/pyproject.toml +0 -0
  15. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/requirements.txt +0 -0
  16. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/__init__.py +0 -0
  17. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/__main__.py +0 -0
  18. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/cangjie.py +0 -0
  19. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/conventions.py +0 -0
  20. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/data/__init__.py +0 -0
  21. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/data/cangjie5_tc.tsv.gz +0 -0
  22. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/graph.py +0 -0
  23. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/notation.py +0 -0
  24. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/__init__.py +0 -0
  25. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/__init__.py +0 -0
  26. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/bw2ipa.py +0 -0
  27. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/hangul2ipa.py +0 -0
  28. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/ko_tables/aspiration.csv +0 -0
  29. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/ko_tables/assimilation.csv +0 -0
  30. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/ko_tables/double_coda.csv +0 -0
  31. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/ko_tables/hanja.tsv +0 -0
  32. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/ko_tables/ipa.csv +0 -0
  33. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/ko_tables/neutralization.csv +0 -0
  34. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/ko_tables/tensification.csv +0 -0
  35. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/ko_tables/yale.csv +0 -0
  36. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/__init__.py +0 -0
  37. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/codeswitch.py +0 -0
  38. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/diacritize.py +0 -0
  39. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/dialectal.py +0 -0
  40. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/english_g2p.py +0 -0
  41. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/espeak.py +0 -0
  42. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/frontend.py +0 -0
  43. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/levantine_g2p.py +0 -0
  44. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/normalize.py +0 -0
  45. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/shami/phoneme_inventory.py +0 -0
  46. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_thirdparty/zh_num.py +0 -0
  47. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/__init__.py +0 -0
  48. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/kog2p/LICENSE.md +0 -0
  49. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/kog2p/__init__.py +0 -0
  50. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/kog2p/rulebook.txt +0 -0
  51. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/mantoq/LICENSE.md +0 -0
  52. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/mantoq/__init__.py +0 -0
  53. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/mantoq/buck/__init__.py +0 -0
  54. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/mantoq/buck/phonetise_buckwalter.py +0 -0
  55. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/mantoq/buck/symbols.py +0 -0
  56. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/mantoq/buck/tokenization.py +0 -0
  57. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/mantoq/num2words.py +0 -0
  58. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/_vendored/mantoq/unicode_symbol2label.py +0 -0
  59. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/ar.py +0 -0
  60. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/en.py +0 -0
  61. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/enums.py +0 -0
  62. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/eu.py +0 -0
  63. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/fa.py +0 -0
  64. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/gl.py +0 -0
  65. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/he.py +0 -0
  66. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/ja.py +0 -0
  67. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/ko.py +0 -0
  68. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/mul.py +0 -0
  69. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/mwl.py +0 -0
  70. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/o2ipa.py +0 -0
  71. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/pt.py +0 -0
  72. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/shami.py +0 -0
  73. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/vi.py +0 -0
  74. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/phonemizers/zh.py +0 -0
  75. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/py.typed +0 -0
  76. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/readings.py +0 -0
  77. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/scripts.py +0 -0
  78. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv/translit.py +0 -0
  79. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv.egg-info/SOURCES.txt +0 -0
  80. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv.egg-info/dependency_links.txt +0 -0
  81. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv.egg-info/requires.txt +0 -0
  82. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/scriptconv.egg-info/top_level.txt +0 -0
  83. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/setup.cfg +0 -0
  84. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_arpa_stress.py +0 -0
  85. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_cangjie.py +0 -0
  86. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_cli.py +0 -0
  87. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_conventions.py +0 -0
  88. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_errors_policy.py +0 -0
  89. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_examples.py +0 -0
  90. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_graph.py +0 -0
  91. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_notation.py +0 -0
  92. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_phonemizers_cjk_ar.py +0 -0
  93. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_readings.py +0 -0
  94. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_readings_zh.py +0 -0
  95. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_scripts.py +0 -0
  96. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_scripts_stressonnx_compat.py +0 -0
  97. {scriptconv-0.0.4a5 → scriptconv-0.0.4a6}/tests/test_translit.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scriptconv
3
- Version: 0.0.4a5
3
+ Version: 0.0.4a6
4
4
  Summary: Zero-dependency script & phoneme-notation core — ISO-15924 detection & metadata, IPA↔ARPABET/X-SAMPA/Lexique/Kirshenbaum/Cotovía/RFE, Buckwalter↔Arabic, Hangul→jamo, kana
5
5
  License: Apache-2.0
6
6
  Project-URL: Homepage, https://github.com/TigreGotico/scriptconv
@@ -0,0 +1,319 @@
1
+ """Diacritization: mechanism, dispatch, and graph extension.
2
+
3
+ This module OWNS diacritization — lang→backend routing, lazy/cached backend
4
+ loading (phonikud, text2tashkeel, stressonnx, bifonia), and the overlay-strip
5
+ helpers. :mod:`scriptconv.phonemizers` (``BasePhonemizer`` and subclasses)
6
+ knows nothing about diacritics: phonemization (orthography → sound) and
7
+ diacritization (a text → text graph transform disambiguating pronunciation
8
+ before G2P) are separate concerns.
9
+
10
+ Diacritization (Arabic tashkeel, Hebrew niqqud, East-Slavic/Turkic/Caucasian
11
+ word stress, European-Portuguese homograph sense marks) is, architecturally,
12
+ just another transform between text representations: it maps the ``"text"``
13
+ node to a lang-contextual ``"text-diacritized"`` node. Like
14
+ :func:`scriptconv.phonemizers.register` it is opt-in — ``DEFAULT_GRAPH`` stays
15
+ free of it until a caller extends a graph::
16
+
17
+ from scriptconv.graph import DEFAULT_GRAPH
18
+ from scriptconv import diacritics, phonemizers
19
+ g = DEFAULT_GRAPH.extend(diacritics.register).extend(phonemizers.register)
20
+
21
+ g.convert("замок стоит на горе", "text", "text-diacritized", lang="ru")
22
+ # 'за́мок сто́ит на горе́' — just the diacritized text
23
+
24
+ g.convert("Tenho muita sede hoje.", "text", "text-diacritized", lang="pt")
25
+ # 'Tenho muita sêde hoje.'
26
+
27
+ Routing ``"text" -> "ipa"`` still takes the direct phonemization edge by
28
+ default: the diacritization edge is model-based (``lossless=False``), so it
29
+ never out-prices a direct phonemization, and enabling this extension does not
30
+ silently change phonemization output. The diacritized route is taken only
31
+ when a caller asks for ``"text-diacritized"`` explicitly.
32
+
33
+ A future per-engine *stance* would push this further into topology rather than
34
+ a flag: an engine that *requires* vocalized input would carry only a
35
+ ``"text-diacritized" -> "ipa"`` edge (forcing the detour), one that *forbids*
36
+ diacritics would carry only ``"text" -> "ipa"`` (making the detour
37
+ unroutable), and a tolerant engine (e.g. arbtok, which self-vocalizes) carries
38
+ both. The strip direction (``"text-diacritized" -> "text"``) is registered too, but
39
+ gated to languages whose marks are removable *overlay* diacritics —
40
+ Arabic/Hebrew vocalization and East-Slavic/Turkic/Caucasian stress. Strip
41
+ removes only the specific overlay codepoints each backend adds (combining
42
+ acute/grave for stress, tashkeel for Arabic, niqqud for Hebrew) — never a
43
+ blanket combining-mark filter — so precomposed native letters survive
44
+ (Cyrillic й/ё, Latvian macrons, Azerbaijani ç/ö, Arabic hamza carriers أ إ آ ؤ
45
+ ئ). The gate uses exact primary-subtag matching, so Aragonese (``arg``),
46
+ Herero (``her``), Mapudungun (``arn``) etc. are never misread as Arabic/Hebrew.
47
+ Languages whose diacritics are native orthography (European Portuguese, via
48
+ bifonia) refuse the strip with :class:`ValueError` instead of silently
49
+ corrupting the spelling (``café`` must never become ``cafe``). This split
50
+ tracks which backend/model produced the marks: overlay backends (phonikud,
51
+ tashkeel, stressonnx) are strippable; spelling-integral backends (bifonia) are
52
+ not.
53
+ """
54
+ from typing import Optional
55
+
56
+ from scriptconv.graph import Edge
57
+ from scriptconv.phonemizers.base import _primary_subtag
58
+
59
+ #: The lang-contextual node produced by diacritization. Like ``"text"`` it is
60
+ #: meaningful only with ``lang=`` context and exists only in opted-in graphs.
61
+ DIACRITIZED = "text-diacritized"
62
+
63
+ # Across East Slavic, Bulgarian/Macedonian/Slovene, Latvian, Armenian,
64
+ # Georgian, and several Turkic/Caucasian languages, lexical word stress is
65
+ # free (not fixed to a syllable) and ordinary orthography leaves it unwritten
66
+ # or under-marked. The clearest case is East Slavic: stress is also mobile
67
+ # (it shifts between forms of the same word) and unstressed vowels *reduce*
68
+ # — Russian unstressed "о" surfaces as [ɐ] or [ə] depending on distance from
69
+ # the stress, not [o] — so a wrong or missing mark there corrupts the vowel
70
+ # quality of the whole word, not just its prosody. Other families in this set
71
+ # don't necessarily reduce vowels, but still need the mark for correct stress
72
+ # placement and prosody. stressonnx restores it as a combining acute (U+0301)
73
+ # after the stressed vowel, covering 26 BCP-47 tags across these families
74
+ # (24 primary subtags; Azerbaijani and Uzbek each have Cyrillic/Latin script
75
+ # variants routed by the full tag).
76
+ STRESS_LANGS = {
77
+ "az", "ba", "be", "bg", "cv", "hy", "ka", "kbd", "kjh", "kk", "ky", "lv",
78
+ "mdf", "mk", "myv", "ru", "sah", "sl", "tg", "tt", "udm", "uk", "uz", "xal",
79
+ }
80
+
81
+
82
+ def _is_european_portuguese(lang: str) -> bool:
83
+ """True for European Portuguese (``pt`` or a ``pt-PT`` region tag).
84
+
85
+ False for Brazilian Portuguese (``pt-BR``) and everything else — the two
86
+ varieties' vowel systems differ, and bifonia's open/closed diacritics are
87
+ only valid for European Portuguese phonology.
88
+ """
89
+ norm = lang.lower().replace("_", "-")
90
+ return norm == "pt" or norm == "pt-pt"
91
+
92
+
93
+ def _diacritizer_family(lang: str) -> Optional[str]:
94
+ """Which diacritization backend family handles *lang*, or ``None``.
95
+
96
+ Single source of truth for lang→backend routing: both the forward
97
+ dispatch (:func:`diacritize`) and the strip direction (:func:`_overlay_marks`)
98
+ resolve through this, so the two can never disagree about which language
99
+ uses which backend. Returns one of ``"he"`` (niqqud), ``"ar"`` (tashkeel),
100
+ ``"stress"`` (stressonnx), ``"pt"`` (bifonia sense diacritics), or
101
+ ``None``. Uses exact primary-subtag matching (never ``startswith``), so
102
+ Aragonese (``arg``), Herero (``her``) and Mapudungun (``arn``) are never
103
+ misread as Arabic/Hebrew.
104
+ """
105
+ p = _primary_subtag(lang)
106
+ if p == "he":
107
+ return "he"
108
+ if p == "ar":
109
+ return "ar"
110
+ if p in STRESS_LANGS:
111
+ return "stress"
112
+ if _is_european_portuguese(lang):
113
+ return "pt"
114
+ return None
115
+
116
+
117
+ _DEFAULT_DIACRITIZER_MODEL = "rawi-ensemble"
118
+
119
+ _PHONIKUD_CACHE: dict = {} # resolved model path/string -> Phonikud instance
120
+ _TASHKEEL_CACHE: dict = {} # model name -> text2tashkeel Diacritizer
121
+
122
+
123
+ def _phonikud(phonikud_model):
124
+ """Lazily build (and cache) the phonikud Phonikud instance used for Hebrew.
125
+
126
+ ``phonikud_model`` is a local path to a phonikud ONNX model, or a
127
+ zero-arg callable resolving one lazily; scriptconv never downloads —
128
+ the consumer resolves the file. Install with ``pip install
129
+ scriptconv[he]`` (or ``pip install phonikud-onnx``)."""
130
+ model = phonikud_model() if callable(phonikud_model) else phonikud_model
131
+ if not model:
132
+ raise ValueError(
133
+ "Hebrew diacritization needs a local phonikud ONNX model: "
134
+ "pass phonikud_model=<path> (scriptconv never downloads "
135
+ "models; obtain one from the phonikud-onnx release)")
136
+ if model not in _PHONIKUD_CACHE:
137
+ try:
138
+ from phonikud_onnx import Phonikud
139
+ except ImportError:
140
+ raise ImportError(
141
+ "Hebrew diacritization needs phonikud-onnx — install "
142
+ "with `pip install scriptconv[he]`") from None
143
+ _PHONIKUD_CACHE[model] = Phonikud(model)
144
+ return _PHONIKUD_CACHE[model]
145
+
146
+
147
+ def _tashkeel(model: Optional[str] = None):
148
+ """Lazily build (and cache) the text2tashkeel Diacritizer used for Arabic.
149
+
150
+ text2tashkeel is a dependency of the ``[ar]`` extra; it restores hamza and the
151
+ dagger alef in addition to the standard marks. Install with
152
+ ``pip install scriptconv[tashkeel]`` (or ``pip install text2tashkeel``)."""
153
+ model = model or _DEFAULT_DIACRITIZER_MODEL
154
+ if model not in _TASHKEEL_CACHE:
155
+ try:
156
+ from text2tashkeel import Diacritizer
157
+ except ImportError as e:
158
+ raise ImportError(
159
+ "Arabic diacritization requires the text2tashkeel package: "
160
+ "pip install scriptconv[tashkeel] (or pip install text2tashkeel)"
161
+ ) from e
162
+ _TASHKEEL_CACHE[model] = Diacritizer(model)
163
+ return _TASHKEEL_CACHE[model]
164
+
165
+
166
+ def _stress(text: str, lang: str, model: Optional[str] = None) -> str:
167
+ """Word-stress restoration via stressonnx, for the 26 language tags
168
+ it covers (see ``STRESS_LANGS``) — East Slavic, Bulgarian/Macedonian/
169
+ Slovene, Latvian, Armenian, Georgian, and Turkic/Caucasian languages.
170
+
171
+ stressonnx is not on PyPI yet; install straight from source. Install
172
+ with ``pip install scriptconv[stress]`` (or ``pip install
173
+ stressonnx``)."""
174
+ try:
175
+ from stressonnx import stress
176
+ except ImportError as e:
177
+ raise ImportError(
178
+ "stress restoration requires the stressonnx package: "
179
+ "pip install scriptconv[stress] (or pip install stressonnx)"
180
+ ) from e
181
+ return stress(text, lang, model=model)
182
+
183
+
184
+ def _sense_diacritics_pt(text: str) -> str:
185
+ """European-Portuguese heterophonic-homograph sense diacritics via bifonia.
186
+
187
+ Rewrites homographs whose pronunciation depends on meaning (e.g.
188
+ "sede" thirst/closed vs. seat/open) with an explicit open/closed
189
+ vowel diacritic. These are ordinary Portuguese orthographic marks,
190
+ chosen so any downstream G2P — rule-based, neural, or espeak —
191
+ reads them correctly. Install with ``pip install scriptconv[pt]``
192
+ (or ``pip install bifonia``)."""
193
+ try:
194
+ from bifonia import add_extra_diacritics
195
+ except ImportError as e:
196
+ raise ImportError(
197
+ "European-Portuguese sense diacritics require the bifonia package: "
198
+ "pip install scriptconv[pt] (or pip install bifonia)"
199
+ ) from e
200
+ return add_extra_diacritics(text)
201
+
202
+
203
+ def diacritize(text: str, lang: str = "und", model=None,
204
+ phonikud_model=None, diacritizer_model=None, **_) -> str:
205
+ """Add pronunciation-disambiguating diacritics to *text* for *lang*.
206
+
207
+ Four backends, each restoring information ordinary orthography omits but
208
+ downstream G2P needs:
209
+
210
+ - Hebrew (``he``) — niqqud via phonikud (``phonikud_model=``).
211
+ - Arabic (``ar``) — tashkeel via text2tashkeel (``[tashkeel]``,
212
+ ``model=``/``diacritizer_model=``).
213
+ - East Slavic, Bulgarian/Macedonian/Slovene, Latvian, Armenian, Georgian,
214
+ and Turkic/Caucasian languages (``STRESS_LANGS``, 26 stressonnx tags) —
215
+ word stress via stressonnx (``[stress]``); stress is unwritten or
216
+ under-marked in these languages, and in East Slavic unstressed vowels
217
+ also reduce, so a missing mark can corrupt more than prosody.
218
+ - European Portuguese (``pt``/``pt-PT``, never ``pt-BR``) —
219
+ heterophonic-homograph sense diacritics via bifonia (``[pt]``);
220
+ ordinary Portuguese orthographic marks that any downstream G2P reads
221
+ correctly.
222
+
223
+ Unrecognized languages are returned unchanged. Each backend raises
224
+ ``ImportError`` naming its extra when the optional dependency is missing
225
+ — scriptconv never installs anything on the caller's behalf.
226
+ """
227
+ family = _diacritizer_family(lang)
228
+ if family == "he":
229
+ return _phonikud(phonikud_model).add_diacritics(text)
230
+ if family == "ar":
231
+ return _tashkeel(model or diacritizer_model).diacritize(text)
232
+ if family == "stress":
233
+ return _stress(text, lang, model)
234
+ if family == "pt":
235
+ return _sense_diacritics_pt(text)
236
+ return text
237
+
238
+
239
+ # Combining marks each diacritization backend overlays onto the bare text.
240
+ # Strip removes ONLY these codepoints — never a blanket combining-mark filter,
241
+ # which (via NFD) would decompose and destroy precomposed native letters:
242
+ # Cyrillic й/ё, Latvian ī, Azerbaijani ç/ö, and Arabic hamza carriers أ إ آ ؤ ئ.
243
+ _STRESS_MARKS = frozenset({0x0300, 0x0301}) # combining grave / acute
244
+ # U+0300 is included defensively alongside the U+0301 that stressonnx
245
+ # actually emits, in case any backend/locale marks secondary stress with a
246
+ # grave instead of an acute; harmless to strip since native precomposed
247
+ # letters (e.g. Cyrillic й/ё) are unaffected either way.
248
+ _ARABIC_MARKS = frozenset(range(0x064B, 0x0660)) | frozenset({0x0670}) # tashkeel + dagger alef
249
+ _HEBREW_MARKS = (frozenset(range(0x05B0, 0x05BE))
250
+ | frozenset({0x05BF, 0x05C1, 0x05C2, 0x05C4, 0x05C5, 0x05C7})) # niqqud
251
+
252
+
253
+ def _overlay_marks(lang: str) -> "frozenset | None":
254
+ """The overlay codepoints for *lang*'s diacritization backend, or None.
255
+
256
+ Resolves the backend family through :func:`_diacritizer_family` (the shared
257
+ lang→backend routing), so this can never disagree with
258
+ :func:`diacritize` about which language a backend owns.
259
+ Only *overlay* families are strippable: ``"pt"`` (bifonia sense marks are
260
+ native orthography) and ``None`` both return None.
261
+ """
262
+ family = _diacritizer_family(lang)
263
+ if family == "stress":
264
+ return _STRESS_MARKS
265
+ if family == "ar":
266
+ return _ARABIC_MARKS
267
+ if family == "he":
268
+ return _HEBREW_MARKS
269
+ return None
270
+
271
+
272
+ def _supports_strip(lang: str) -> bool:
273
+ """True for languages whose diacritics are removable overlays (stress marks,
274
+ Arabic tashkeel, Hebrew niqqud) rather than native orthography."""
275
+ return _overlay_marks(lang) is not None
276
+
277
+
278
+ def strip_diacritics(text: str, lang: str = "und", **_) -> str:
279
+ """Remove the overlay diacritics *lang*'s backend adds, recovering the bare
280
+ text WITHOUT touching native letters.
281
+
282
+ Removes only the specific overlay codepoints (combining acute/grave for
283
+ stress, tashkeel for Arabic, niqqud for Hebrew) — precomposed native
284
+ letters (Cyrillic й/ё, Latvian ī, Azerbaijani ç, Arabic hamza carriers) are
285
+ left intact. Raises :class:`ValueError` for languages whose diacritics are
286
+ part of the native orthography (e.g. European Portuguese), where any removal
287
+ would corrupt the spelling.
288
+ """
289
+ marks = _overlay_marks(lang)
290
+ if marks is None:
291
+ raise ValueError(
292
+ f"cannot strip diacritics for lang={lang!r}: its diacritics are "
293
+ "part of the native orthography, so removal would corrupt the "
294
+ "spelling. strip is defined only for overlay diacritics "
295
+ "(Arabic/Hebrew vocalization, East-Slavic/Turkic stress).")
296
+ return "".join(c for c in text if ord(c) not in marks)
297
+
298
+
299
+ def register(graph) -> None:
300
+ """Opt-in graph integration: add the diacritize/strip edge pair.
301
+
302
+ ``text -> text-diacritized`` is model-based (``lossless=False``), so a
303
+ direct ``text -> ipa`` phonemization always out-prices the detour —
304
+ enabling this extension is safe and non-invasive. Pair with
305
+ :func:`scriptconv.phonemizers.register` to make
306
+ ``"text-diacritized" -> "ipa"`` reachable.
307
+
308
+ ``text-diacritized -> text`` (:func:`strip_diacritics`) is lossless and
309
+ cheap, but gated: it raises for languages whose diacritics are native
310
+ orthography rather than removable overlays.
311
+ """
312
+ graph.register(
313
+ Edge("text", DIACRITIZED,
314
+ lambda text, lang="und", model=None, **c: diacritize(text, lang, model, **c),
315
+ lossless=False))
316
+ graph.register(
317
+ Edge(DIACRITIZED, "text",
318
+ lambda text, lang="und", **_: strip_diacritics(text, lang),
319
+ lossless=True))
@@ -34,163 +34,25 @@ RawPhonemizedChunks = List[Tuple[str, str, bool]]
34
34
 
35
35
  PhonemizedChunks = list[list[str]]
36
36
 
37
- # Across East Slavic, Bulgarian/Macedonian/Slovene, Latvian, Armenian,
38
- # Georgian, and several Turkic/Caucasian languages, lexical word stress is
39
- # free (not fixed to a syllable) and ordinary orthography leaves it unwritten
40
- # or under-marked. The clearest case is East Slavic: stress is also mobile
41
- # (it shifts between forms of the same word) and unstressed vowels *reduce*
42
- # — Russian unstressed "о" surfaces as [ɐ] or [ə] depending on distance from
43
- # the stress, not [o] — so a wrong or missing mark there corrupts the vowel
44
- # quality of the whole word, not just its prosody. Other families in this set
45
- # don't necessarily reduce vowels, but still need the mark for correct stress
46
- # placement and prosody. stressonnx restores it as a combining acute (U+0301)
47
- # after the stressed vowel, covering 26 BCP-47 tags across these families
48
- # (24 primary subtags; Azerbaijani and Uzbek each have Cyrillic/Latin script
49
- # variants routed by the full tag).
50
- STRESS_LANGS = {
51
- "az", "ba", "be", "bg", "cv", "hy", "ka", "kbd", "kjh", "kk", "ky", "lv",
52
- "mdf", "mk", "myv", "ru", "sah", "sl", "tg", "tt", "udm", "uk", "uz", "xal",
53
- }
54
-
55
37
 
56
38
  def _primary_subtag(lang: str) -> str:
57
39
  """Lowercase, ``_``→``-`` normalized primary language subtag.
58
40
 
59
- Used for exact-match routing (``STRESS_LANGS`` membership) rather than
60
- ``str.startswith``, so e.g. Berber (``ber``) never false-matches
61
- Belarusian (``be``).
41
+ Used for exact-match language routing (e.g. registry lang defaults,
42
+ diacritization backend selection) rather than ``str.startswith``, so
43
+ e.g. Berber (``ber``) never false-matches Belarusian (``be``).
62
44
  """
63
45
  return lang.lower().replace("_", "-").split("-")[0]
64
46
 
65
47
 
66
- def _is_european_portuguese(lang: str) -> bool:
67
- """True for European Portuguese (``pt`` or a ``pt-PT`` region tag).
68
-
69
- False for Brazilian Portuguese (``pt-BR``) and everything else — the two
70
- varieties' vowel systems differ, and bifonia's open/closed diacritics are
71
- only valid for European Portuguese phonology.
72
- """
73
- norm = lang.lower().replace("_", "-")
74
- return norm == "pt" or norm == "pt-pt"
75
-
76
-
77
- def _diacritizer_family(lang: str) -> Optional[str]:
78
- """Which diacritization backend family handles *lang*, or ``None``.
79
-
80
- Single source of truth for lang→backend routing: both the forward
81
- dispatch (:meth:`BasePhonemizer.add_diacritics`) and the strip direction
82
- (:func:`scriptconv.diacritics._overlay_marks`) resolve through this, so the
83
- two can never disagree about which language uses which backend. Returns one
84
- of ``"he"`` (niqqud), ``"ar"`` (tashkeel), ``"stress"`` (stressonnx), ``"pt"``
85
- (bifonia sense diacritics), or ``None``. Uses exact primary-subtag matching
86
- (never ``startswith``), so Aragonese (``arg``), Herero (``her``) and
87
- Mapudungun (``arn``) are never misread as Arabic/Hebrew.
88
- """
89
- p = _primary_subtag(lang)
90
- if p == "he":
91
- return "he"
92
- if p == "ar":
93
- return "ar"
94
- if p in STRESS_LANGS:
95
- return "stress"
96
- if _is_european_portuguese(lang):
97
- return "pt"
98
- return None
99
-
100
-
101
48
  class BasePhonemizer(metaclass=abc.ABCMeta):
102
49
  def __init__(self, alphabet: Alphabet = Alphabet.UNICODE,
103
- diacritizer_model: str = "rawi-ensemble",
104
- normalizer: Optional[Callable[[str, str], str]] = None,
105
- phonikud_model: Optional[str] = None):
50
+ normalizer: Optional[Callable[[str, str], str]] = None):
106
51
  super().__init__()
107
52
  self.alphabet = alphabet
108
53
  # optional (text, lang) -> str hook run before chunking; see module
109
54
  # docstring — scriptconv performs no normalization of its own
110
55
  self.normalizer = normalizer
111
- # local path to a phonikud ONNX model (Hebrew diacritization), or a
112
- # zero-arg callable resolving one lazily; scriptconv never downloads
113
- # — the consumer resolves the file
114
- self.phonikud_model = phonikud_model
115
-
116
- # diacritizer model name, for languages that need one. Arabic uses
117
- # text2tashkeel; the default "rawi-ensemble" restores hamza and the dagger
118
- # alef in addition to the standard marks.
119
- self.diacritizer_model = diacritizer_model
120
- self._phonikud = None # hebrew only
121
- self._tashkeel: dict = {} # model name -> text2tashkeel Diacritizer
122
-
123
- @property
124
- def phonikud(self):
125
- if self._phonikud is None:
126
- model = self.phonikud_model() if callable(self.phonikud_model) \
127
- else self.phonikud_model
128
- if not model:
129
- raise ValueError(
130
- "Hebrew diacritization needs a local phonikud ONNX model: "
131
- "pass phonikud_model=<path> (scriptconv never downloads "
132
- "models; obtain one from the phonikud-onnx release)")
133
- try:
134
- from phonikud_onnx import Phonikud
135
- except ImportError:
136
- raise ImportError(
137
- "Hebrew diacritization needs phonikud-onnx — install "
138
- "with `pip install scriptconv[he]`") from None
139
- self._phonikud = Phonikud(model)
140
- return self._phonikud
141
-
142
- def tashkeel(self, model: Optional[str] = None):
143
- """Lazily build (and cache) the text2tashkeel Diacritizer used for Arabic.
144
-
145
- text2tashkeel is a dependency of the ``[ar]`` extra; it restores hamza and the
146
- dagger alef in addition to the standard marks. Install with
147
- ``pip install scriptconv[tashkeel]`` (or ``pip install text2tashkeel``)."""
148
- model = model or self.diacritizer_model
149
- if model not in self._tashkeel:
150
- try:
151
- from text2tashkeel import Diacritizer
152
- except ImportError as e:
153
- raise ImportError(
154
- "Arabic diacritization requires the text2tashkeel package: "
155
- "pip install scriptconv[tashkeel] (or pip install text2tashkeel)"
156
- ) from e
157
- self._tashkeel[model] = Diacritizer(model)
158
- return self._tashkeel[model]
159
-
160
- def _stress(self, text: str, lang: str, model: Optional[str] = None) -> str:
161
- """Word-stress restoration via stressonnx, for the 26 language tags
162
- it covers (see ``STRESS_LANGS``) — East Slavic, Bulgarian/Macedonian/
163
- Slovene, Latvian, Armenian, Georgian, and Turkic/Caucasian languages.
164
-
165
- stressonnx is not on PyPI yet; install straight from source. Install
166
- with ``pip install scriptconv[stress]`` (or ``pip install
167
- stressonnx``)."""
168
- try:
169
- from stressonnx import stress
170
- except ImportError as e:
171
- raise ImportError(
172
- "stress restoration requires the stressonnx package: "
173
- "pip install scriptconv[stress] (or pip install stressonnx)"
174
- ) from e
175
- return stress(text, lang, model=model)
176
-
177
- def _sense_diacritics_pt(self, text: str) -> str:
178
- """European-Portuguese heterophonic-homograph sense diacritics via bifonia.
179
-
180
- Rewrites homographs whose pronunciation depends on meaning (e.g.
181
- "sede" thirst/closed vs. seat/open) with an explicit open/closed
182
- vowel diacritic. These are ordinary Portuguese orthographic marks,
183
- chosen so any downstream G2P — rule-based, neural, or espeak —
184
- reads them correctly. Install with ``pip install scriptconv[pt]``
185
- (or ``pip install bifonia``)."""
186
- try:
187
- from bifonia import add_extra_diacritics
188
- except ImportError as e:
189
- raise ImportError(
190
- "European-Portuguese sense diacritics require the bifonia package: "
191
- "pip install scriptconv[pt] (or pip install bifonia)"
192
- ) from e
193
- return add_extra_diacritics(text)
194
56
 
195
57
  @abc.abstractmethod
196
58
  def phonemize_string(self, text: str, lang: str) -> str:
@@ -199,41 +61,6 @@ class BasePhonemizer(metaclass=abc.ABCMeta):
199
61
  def phonemize_to_list(self, text: str, lang: str) -> List[str]:
200
62
  return list(self.phonemize_string(text, lang))
201
63
 
202
- def add_diacritics(self, text: str, lang: str,
203
- model: Optional[str] = None) -> str:
204
- """Disambiguate pronunciation before G2P by adding diacritics.
205
-
206
- Four backends, each restoring information ordinary orthography
207
- omits but downstream G2P needs:
208
-
209
- - Hebrew (``he``) — niqqud via phonikud (``phonikud_model=``).
210
- - Arabic (``ar``) — tashkeel via text2tashkeel (``[tashkeel]``).
211
- - East Slavic, Bulgarian/Macedonian/Slovene, Latvian, Armenian,
212
- Georgian, and Turkic/Caucasian languages (``STRESS_LANGS``, 26
213
- stressonnx tags) — word stress via stressonnx (``[stress]``);
214
- stress is unwritten or under-marked in these languages, and in
215
- East Slavic unstressed vowels also reduce, so a missing mark can
216
- corrupt more than prosody.
217
- - European Portuguese (``pt``/``pt-PT``, never ``pt-BR``) —
218
- heterophonic-homograph sense diacritics via bifonia (``[pt]``);
219
- ordinary Portuguese orthographic marks that any downstream G2P
220
- reads correctly.
221
-
222
- Unrecognized languages are returned unchanged. Each backend raises
223
- ``ImportError`` naming its extra when the optional dependency is
224
- missing — scriptconv never installs anything on the caller's behalf.
225
- """
226
- family = _diacritizer_family(lang)
227
- if family == "he":
228
- return self.phonikud.add_diacritics(text)
229
- if family == "ar":
230
- return self.tashkeel(model).diacritize(text)
231
- if family == "stress":
232
- return self._stress(text, lang, model)
233
- if family == "pt":
234
- return self._sense_diacritics_pt(text)
235
- return text
236
-
237
64
  def phonemize(self, text: str, lang: str) -> PhonemizedChunks:
238
65
  # PhonemizedChunks is list[list[str]]; empty text yields no
239
66
  # sentences. (Returning the raw (str, str, bool) tuple form here
@@ -102,10 +102,9 @@ def get_phonemizer(phonemizer: Phonemizer,
102
102
  """
103
103
  phonemizer = Phonemizer(phonemizer)
104
104
  cls = get_phonemizer_class(phonemizer)
105
- # normalizer/phonikud_model are plain BasePhonemizer attributes; set them
106
- # after construction so wrapper __init__ signatures stay untouched
105
+ # normalizer is a plain BasePhonemizer attribute; set it after
106
+ # construction so wrapper __init__ signatures stay untouched
107
107
  normalizer = kwargs.pop("normalizer", None)
108
- phonikud_model = kwargs.pop("phonikud_model", None)
109
108
  if phonemizer in (_P.BYT5, _P.CHARSIU, _P.DEEPPHONEMIZER):
110
109
  inst = cls(model, **kwargs)
111
110
  else:
@@ -124,8 +123,6 @@ def get_phonemizer(phonemizer: Phonemizer,
124
123
  inst = cls(**kwargs)
125
124
  if normalizer is not None:
126
125
  inst.normalizer = normalizer
127
- if phonikud_model is not None:
128
- inst.phonikud_model = phonikud_model
129
126
  return inst
130
127
 
131
128
 
@@ -2,7 +2,7 @@
2
2
  VERSION_MAJOR = 0
3
3
  VERSION_MINOR = 0
4
4
  VERSION_BUILD = 4
5
- VERSION_ALPHA = 5
5
+ VERSION_ALPHA = 6
6
6
  # END_VERSION_BLOCK
7
7
 
8
8
  VERSION_STR = f"{VERSION_MAJOR}.{VERSION_MINOR}.{VERSION_BUILD}"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: scriptconv
3
- Version: 0.0.4a5
3
+ Version: 0.0.4a6
4
4
  Summary: Zero-dependency script & phoneme-notation core — ISO-15924 detection & metadata, IPA↔ARPABET/X-SAMPA/Lexique/Kirshenbaum/Cotovía/RFE, Buckwalter↔Arabic, Hangul→jamo, kana
5
5
  License: Apache-2.0
6
6
  Project-URL: Homepage, https://github.com/TigreGotico/scriptconv