scriptconv 0.0.3a4__tar.gz → 0.0.3a6__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.
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/PKG-INFO +1 -1
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/pyproject.toml +1 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/__init__.py +4 -0
- scriptconv-0.0.3a6/scriptconv/data/cangjie5_tc.tsv.gz +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/readings.py +41 -12
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/version.py +1 -1
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv.egg-info/PKG-INFO +1 -1
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv.egg-info/SOURCES.txt +1 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/tests/test_cangjie.py +7 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/tests/test_readings.py +42 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/LICENSE +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/README.md +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/requirements.txt +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/__main__.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/cangjie.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/data/__init__.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/notation.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/py.typed +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/scripts.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv/translit.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv.egg-info/dependency_links.txt +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv.egg-info/requires.txt +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/scriptconv.egg-info/top_level.txt +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/setup.cfg +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/tests/test_cli.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/tests/test_examples.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/tests/test_notation.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/tests/test_readings_zh.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/tests/test_scripts.py +0 -0
- {scriptconv-0.0.3a4 → scriptconv-0.0.3a6}/tests/test_translit.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: scriptconv
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.3a6
|
|
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
|
|
@@ -68,6 +68,8 @@ from scriptconv.translit import (
|
|
|
68
68
|
kana_to_hira,
|
|
69
69
|
)
|
|
70
70
|
from scriptconv.readings import (
|
|
71
|
+
ReadingToken,
|
|
72
|
+
tokens,
|
|
71
73
|
to_hiragana,
|
|
72
74
|
to_katakana,
|
|
73
75
|
to_pinyin,
|
|
@@ -117,6 +119,8 @@ __all__ = [
|
|
|
117
119
|
"hira_to_kana",
|
|
118
120
|
"kana_to_hira",
|
|
119
121
|
# readings (dictionary-backed, needs scriptconv[ja])
|
|
122
|
+
"ReadingToken",
|
|
123
|
+
"tokens",
|
|
120
124
|
"to_hiragana",
|
|
121
125
|
"to_katakana",
|
|
122
126
|
# cangjie (vendored Cangjie5 table)
|
|
Binary file
|
|
@@ -22,7 +22,17 @@ unconverted text.
|
|
|
22
22
|
"""
|
|
23
23
|
from __future__ import annotations
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
from typing import Iterator, NamedTuple
|
|
26
|
+
|
|
27
|
+
__all__ = ["ReadingToken", "tokens", "to_hiragana", "to_katakana",
|
|
28
|
+
"to_pinyin", "to_bopomofo"]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ReadingToken(NamedTuple):
|
|
32
|
+
"""One dictionary-segmented token of Japanese text and its kana readings."""
|
|
33
|
+
orig: str
|
|
34
|
+
hira: str
|
|
35
|
+
kana: str
|
|
26
36
|
|
|
27
37
|
_kakasi = None
|
|
28
38
|
|
|
@@ -44,32 +54,51 @@ def _converter():
|
|
|
44
54
|
return _kakasi
|
|
45
55
|
|
|
46
56
|
|
|
47
|
-
def
|
|
57
|
+
def tokens(text: str) -> Iterator[ReadingToken]:
|
|
58
|
+
"""Iterate the dictionary's segmentation of *text* with per-token readings.
|
|
59
|
+
|
|
60
|
+
Each token is a :class:`ReadingToken` with the original surface form and
|
|
61
|
+
its hiragana and katakana readings. This is the primitive under
|
|
62
|
+
:func:`to_hiragana`/:func:`to_katakana`; use it directly when the consumer
|
|
63
|
+
needs token boundaries — e.g. word segmentation (wakachigaki) or
|
|
64
|
+
reading-dependent post-processing — rather than a joined string.
|
|
65
|
+
"""
|
|
66
|
+
for t in _converter().convert(text):
|
|
67
|
+
if t["orig"]:
|
|
68
|
+
yield ReadingToken(t["orig"], t["hira"], t["kana"])
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def to_hiragana(text: str, keep_katakana: bool = False,
|
|
72
|
+
segment: bool = False) -> str:
|
|
48
73
|
"""Respell Japanese text in hiragana.
|
|
49
74
|
|
|
50
75
|
Kanji are resolved to their dictionary reading; existing kana and any
|
|
51
76
|
non-Japanese characters pass through. With ``keep_katakana=True``,
|
|
52
77
|
katakana tokens keep their original script (useful when katakana marks
|
|
53
|
-
loanwords deliberately) instead of being folded into hiragana.
|
|
78
|
+
loanwords deliberately) instead of being folded into hiragana. With
|
|
79
|
+
``segment=True``, dictionary tokens are joined with spaces (wakachigaki,
|
|
80
|
+
the spaced orthographic mode) instead of concatenated.
|
|
54
81
|
"""
|
|
55
82
|
out = []
|
|
56
|
-
for
|
|
57
|
-
orig
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
out.append(orig)
|
|
83
|
+
for tok in tokens(text):
|
|
84
|
+
if keep_katakana and tok.orig and all(
|
|
85
|
+
ord(c) in _KANA or not _is_japanese(c) for c in tok.orig):
|
|
86
|
+
out.append(tok.orig)
|
|
61
87
|
else:
|
|
62
|
-
out.append(
|
|
63
|
-
return "".join(out)
|
|
88
|
+
out.append(tok.hira)
|
|
89
|
+
return " ".join(" ".join(out).split()) if segment else "".join(out)
|
|
64
90
|
|
|
65
91
|
|
|
66
|
-
def to_katakana(text: str) -> str:
|
|
92
|
+
def to_katakana(text: str, segment: bool = False) -> str:
|
|
67
93
|
"""Respell Japanese text in katakana.
|
|
68
94
|
|
|
69
95
|
Kanji are resolved to their dictionary reading; existing kana are
|
|
70
96
|
transposed to katakana and any non-Japanese characters pass through.
|
|
97
|
+
With ``segment=True``, dictionary tokens are joined with spaces
|
|
98
|
+
(wakachigaki) instead of concatenated.
|
|
71
99
|
"""
|
|
72
|
-
|
|
100
|
+
kana = [tok.kana for tok in tokens(text)]
|
|
101
|
+
return " ".join(" ".join(kana).split()) if segment else "".join(kana)
|
|
73
102
|
|
|
74
103
|
|
|
75
104
|
def _is_japanese(ch: str) -> bool:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: scriptconv
|
|
3
|
-
Version: 0.0.
|
|
3
|
+
Version: 0.0.3a6
|
|
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
|
|
@@ -24,6 +24,13 @@ class TestCangjieCode(unittest.TestCase):
|
|
|
24
24
|
self.assertTrue(all(c.isalpha() and c.islower() for c in sample))
|
|
25
25
|
|
|
26
26
|
|
|
27
|
+
class TestTableIsPackaged(unittest.TestCase):
|
|
28
|
+
def test_data_file_resolvable_via_importlib_resources(self):
|
|
29
|
+
from importlib.resources import files
|
|
30
|
+
raw = files("scriptconv.data").joinpath("cangjie5_tc.tsv.gz").read_bytes()
|
|
31
|
+
self.assertGreater(len(raw), 100000)
|
|
32
|
+
|
|
33
|
+
|
|
27
34
|
class TestToCangjie(unittest.TestCase):
|
|
28
35
|
def test_all_hanzi_sentence(self):
|
|
29
36
|
self.assertEqual(to_cangjie("倉頡"), "oiar grmbc")
|
|
@@ -71,3 +71,45 @@ class TestDependencyBoundary(unittest.TestCase):
|
|
|
71
71
|
|
|
72
72
|
if __name__ == "__main__":
|
|
73
73
|
unittest.main()
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
class TestTokens(unittest.TestCase):
|
|
77
|
+
def test_yields_reading_tokens_with_all_forms(self):
|
|
78
|
+
from scriptconv import ReadingToken, tokens
|
|
79
|
+
toks = list(tokens("東京タワー"))
|
|
80
|
+
self.assertTrue(all(isinstance(t, ReadingToken) for t in toks))
|
|
81
|
+
origs = [t.orig for t in toks]
|
|
82
|
+
self.assertIn("東京", origs)
|
|
83
|
+
tokyo = toks[origs.index("東京")]
|
|
84
|
+
self.assertEqual(tokyo.hira, "とうきょう")
|
|
85
|
+
self.assertEqual(tokyo.kana, "トウキョウ")
|
|
86
|
+
|
|
87
|
+
def test_tokens_cover_input_exactly(self):
|
|
88
|
+
from scriptconv import tokens
|
|
89
|
+
text = "私はcoffeeが好き"
|
|
90
|
+
self.assertEqual("".join(t.orig for t in tokens(text)), text)
|
|
91
|
+
|
|
92
|
+
def test_empty_string_yields_nothing(self):
|
|
93
|
+
from scriptconv import tokens
|
|
94
|
+
self.assertEqual(list(tokens("")), [])
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class TestSegmentFlag(unittest.TestCase):
|
|
98
|
+
def test_hiragana_wakachigaki(self):
|
|
99
|
+
from scriptconv import to_hiragana
|
|
100
|
+
self.assertEqual(to_hiragana("私は学生です", segment=True),
|
|
101
|
+
"わたし は がくせい です")
|
|
102
|
+
|
|
103
|
+
def test_katakana_wakachigaki(self):
|
|
104
|
+
from scriptconv import to_katakana
|
|
105
|
+
self.assertEqual(to_katakana("私は学生です", segment=True),
|
|
106
|
+
"ワタシ ハ ガクセイ デス")
|
|
107
|
+
|
|
108
|
+
def test_segment_false_is_default_concatenation(self):
|
|
109
|
+
from scriptconv import to_hiragana
|
|
110
|
+
self.assertEqual(to_hiragana("私は学生です"), "わたしはがくせいです")
|
|
111
|
+
|
|
112
|
+
def test_segment_collapses_original_whitespace(self):
|
|
113
|
+
from scriptconv import to_hiragana
|
|
114
|
+
out = to_hiragana("東京 大阪", segment=True)
|
|
115
|
+
self.assertNotIn(" ", out)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|