PyHakka 0.1.0__py3-none-any.whl
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.
- pyhakka/__init__.py +72 -0
- pyhakka/_db.py +80 -0
- pyhakka/cli.py +293 -0
- pyhakka/data/hakka.sqlite3 +4 -0
- pyhakka/data/manifest.json +106 -0
- pyhakka/dialects.py +163 -0
- pyhakka/lexicon/__init__.py +22 -0
- pyhakka/lexicon/db.py +25 -0
- pyhakka/lexicon/query.py +385 -0
- pyhakka/phon/__init__.py +26 -0
- pyhakka/phon/converter.py +256 -0
- pyhakka/phon/parser.py +748 -0
- pyhakka/phon/schemes.py +319 -0
- pyhakka/phon/syllable.py +164 -0
- pyhakka/phon/validate.py +79 -0
- pyhakka/py.typed +0 -0
- pyhakka/sandhi/__init__.py +17 -0
- pyhakka/sandhi/rules.py +369 -0
- pyhakka-0.1.0.dist-info/METADATA +40 -0
- pyhakka-0.1.0.dist-info/RECORD +24 -0
- pyhakka-0.1.0.dist-info/WHEEL +5 -0
- pyhakka-0.1.0.dist-info/entry_points.txt +2 -0
- pyhakka-0.1.0.dist-info/licenses/LICENSE +29 -0
- pyhakka-0.1.0.dist-info/top_level.txt +1 -0
pyhakka/dialects.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
"""Dialect registry, metadata models, and alias resolution for PyHakka."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from functools import lru_cache
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from pyhakka._db import get_connection
|
|
11
|
+
|
|
12
|
+
DEFAULT_DIALECT = "huiyang"
|
|
13
|
+
DEFAULT_SCHEME = "hps_ext"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class ToneSpec:
|
|
18
|
+
"""Description of one tone class in a Hakka variety."""
|
|
19
|
+
|
|
20
|
+
tone_class: str
|
|
21
|
+
name: str
|
|
22
|
+
value: str = ""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
@dataclass(frozen=True)
|
|
26
|
+
class DialectInfo:
|
|
27
|
+
"""Metadata record for a curated core dialect or an MCPDict Hakka variety."""
|
|
28
|
+
|
|
29
|
+
id: int
|
|
30
|
+
slug: str
|
|
31
|
+
name_trad: str
|
|
32
|
+
name_simp: str
|
|
33
|
+
tier: str # "core" | "mcpdict"
|
|
34
|
+
subgroup: str | None
|
|
35
|
+
location: str | None
|
|
36
|
+
lat: float | None
|
|
37
|
+
lon: float | None
|
|
38
|
+
is_island: bool
|
|
39
|
+
is_historical: bool
|
|
40
|
+
tones: dict[str, ToneSpec]
|
|
41
|
+
use_tone_value: bool
|
|
42
|
+
has_words: bool
|
|
43
|
+
char_count: int
|
|
44
|
+
word_count: int
|
|
45
|
+
source_license: str
|
|
46
|
+
reference: str | None
|
|
47
|
+
|
|
48
|
+
def tone_value_for(self, tone_class: str) -> str:
|
|
49
|
+
spec = self.tones.get(tone_class)
|
|
50
|
+
return spec.value if spec else ""
|
|
51
|
+
|
|
52
|
+
def tone_class_for_value(self, tone_value: str) -> str:
|
|
53
|
+
"""Inverse lookup from 5-level tone value (e.g. '44') to tone class ('1')."""
|
|
54
|
+
if not tone_value:
|
|
55
|
+
return ""
|
|
56
|
+
for cls, spec in self.tones.items():
|
|
57
|
+
if spec.value == tone_value:
|
|
58
|
+
return cls
|
|
59
|
+
return ""
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _row_to_dialect(row: Any) -> DialectInfo:
|
|
63
|
+
raw_tones = json.loads(row["tones_json"]) if row["tones_json"] else {}
|
|
64
|
+
tones = {
|
|
65
|
+
str(k): ToneSpec(
|
|
66
|
+
tone_class=str(k),
|
|
67
|
+
name=str(v.get("name", "")),
|
|
68
|
+
value=str(v.get("value", "")),
|
|
69
|
+
)
|
|
70
|
+
for k, v in raw_tones.items()
|
|
71
|
+
}
|
|
72
|
+
return DialectInfo(
|
|
73
|
+
id=int(row["id"]),
|
|
74
|
+
slug=str(row["slug"]),
|
|
75
|
+
name_trad=str(row["name_trad"]),
|
|
76
|
+
name_simp=str(row["name_simp"]),
|
|
77
|
+
tier=str(row["tier"]),
|
|
78
|
+
subgroup=row["subgroup"],
|
|
79
|
+
location=row["location"],
|
|
80
|
+
lat=float(row["lat"]) if row["lat"] is not None else None,
|
|
81
|
+
lon=float(row["lon"]) if row["lon"] is not None else None,
|
|
82
|
+
is_island=bool(row["is_island"]),
|
|
83
|
+
is_historical=bool(row["is_historical"]),
|
|
84
|
+
tones=tones,
|
|
85
|
+
use_tone_value=bool(row["use_tone_value"]),
|
|
86
|
+
has_words=bool(row["has_words"]),
|
|
87
|
+
char_count=int(row["char_count"]),
|
|
88
|
+
word_count=int(row["word_count"]),
|
|
89
|
+
source_license=str(row["source_license"]),
|
|
90
|
+
reference=row["reference"],
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
@lru_cache(maxsize=512)
|
|
95
|
+
def get_dialect(name: str = DEFAULT_DIALECT) -> DialectInfo:
|
|
96
|
+
"""Resolve a dialect slug, Chinese name (Trad/Simp), or alias to `DialectInfo`."""
|
|
97
|
+
key = name.strip().lower()
|
|
98
|
+
if not key:
|
|
99
|
+
key = DEFAULT_DIALECT
|
|
100
|
+
conn = get_connection()
|
|
101
|
+
row = conn.execute(
|
|
102
|
+
"""
|
|
103
|
+
SELECT d.*
|
|
104
|
+
FROM dialect_aliases a
|
|
105
|
+
JOIN dialects d ON d.id = a.dialect_id
|
|
106
|
+
WHERE a.alias = ?
|
|
107
|
+
""",
|
|
108
|
+
(key,),
|
|
109
|
+
).fetchone()
|
|
110
|
+
if row is None:
|
|
111
|
+
# Try direct slug match just in case
|
|
112
|
+
row = conn.execute("SELECT * FROM dialects WHERE lower(slug) = ?", (key,)).fetchone()
|
|
113
|
+
if row is None:
|
|
114
|
+
suggestions = [
|
|
115
|
+
r["slug"]
|
|
116
|
+
for r in conn.execute(
|
|
117
|
+
"""
|
|
118
|
+
SELECT DISTINCT d.slug
|
|
119
|
+
FROM dialects d
|
|
120
|
+
LEFT JOIN dialect_aliases a ON a.dialect_id = d.id
|
|
121
|
+
WHERE lower(d.slug) LIKE ? OR lower(d.name_trad) LIKE ? OR lower(d.name_simp) LIKE ?
|
|
122
|
+
LIMIT 5
|
|
123
|
+
""",
|
|
124
|
+
(f"%{key}%", f"%{key}%", f"%{key}%"),
|
|
125
|
+
).fetchall()
|
|
126
|
+
]
|
|
127
|
+
hint = f" Did you mean: {', '.join(suggestions)}?" if suggestions else ""
|
|
128
|
+
raise KeyError(f"Unknown Hakka dialect: {name!r}.{hint}")
|
|
129
|
+
return _row_to_dialect(row)
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def list_dialects(
|
|
133
|
+
*,
|
|
134
|
+
tier: str | None = None,
|
|
135
|
+
subgroup: str | None = None,
|
|
136
|
+
has_words: bool | None = None,
|
|
137
|
+
historical_only: bool = False,
|
|
138
|
+
islands_only: bool = False,
|
|
139
|
+
) -> list[DialectInfo]:
|
|
140
|
+
"""List available Hakka varieties with optional filtering."""
|
|
141
|
+
clauses: list[str] = []
|
|
142
|
+
params: list[Any] = []
|
|
143
|
+
if tier is not None:
|
|
144
|
+
clauses.append("tier = ?")
|
|
145
|
+
params.append(tier)
|
|
146
|
+
if subgroup is not None:
|
|
147
|
+
clauses.append("subgroup LIKE ?")
|
|
148
|
+
params.append(f"%{subgroup}%")
|
|
149
|
+
if has_words is not None:
|
|
150
|
+
clauses.append("has_words = ?")
|
|
151
|
+
params.append(1 if has_words else 0)
|
|
152
|
+
if historical_only:
|
|
153
|
+
clauses.append("is_historical = 1")
|
|
154
|
+
if islands_only:
|
|
155
|
+
clauses.append("is_island = 1")
|
|
156
|
+
|
|
157
|
+
sql = "SELECT * FROM dialects"
|
|
158
|
+
if clauses:
|
|
159
|
+
sql += " WHERE " + " AND ".join(clauses)
|
|
160
|
+
sql += " ORDER BY CASE tier WHEN 'core' THEN 0 ELSE 1 END, id"
|
|
161
|
+
|
|
162
|
+
conn = get_connection()
|
|
163
|
+
return [_row_to_dialect(r) for r in conn.execute(sql, params).fetchall()]
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""Lexicon query module for Hakka characters, words, and cross-dialect comparison."""
|
|
2
|
+
|
|
3
|
+
from pyhakka._db import get_db_path, get_manifest
|
|
4
|
+
from pyhakka.lexicon.query import (
|
|
5
|
+
CharReading,
|
|
6
|
+
WordReading,
|
|
7
|
+
compare_dialects,
|
|
8
|
+
lookup_char,
|
|
9
|
+
lookup_word,
|
|
10
|
+
search_by_pron,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"CharReading",
|
|
15
|
+
"WordReading",
|
|
16
|
+
"compare_dialects",
|
|
17
|
+
"get_db_path",
|
|
18
|
+
"get_manifest",
|
|
19
|
+
"lookup_char",
|
|
20
|
+
"lookup_word",
|
|
21
|
+
"search_by_pron",
|
|
22
|
+
]
|
pyhakka/lexicon/db.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""Re-exports for PyHakka's read-only SQLite database access layer."""
|
|
2
|
+
|
|
3
|
+
from pyhakka._db import (
|
|
4
|
+
DATA_DIR,
|
|
5
|
+
DEFAULT_DB_PATH,
|
|
6
|
+
MANIFEST_PATH,
|
|
7
|
+
expand_char_variants,
|
|
8
|
+
get_connection,
|
|
9
|
+
get_db_path,
|
|
10
|
+
get_manifest,
|
|
11
|
+
to_simplified_char,
|
|
12
|
+
to_simplified_text,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"DATA_DIR",
|
|
17
|
+
"DEFAULT_DB_PATH",
|
|
18
|
+
"MANIFEST_PATH",
|
|
19
|
+
"expand_char_variants",
|
|
20
|
+
"get_connection",
|
|
21
|
+
"get_db_path",
|
|
22
|
+
"get_manifest",
|
|
23
|
+
"to_simplified_char",
|
|
24
|
+
"to_simplified_text",
|
|
25
|
+
]
|
pyhakka/lexicon/query.py
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
"""Lexicon lookup and cross-dialect comparison APIs."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
from pyhakka._db import expand_char_variants, get_connection, to_simplified_text
|
|
10
|
+
from pyhakka.dialects import DEFAULT_DIALECT, DialectInfo, get_dialect, list_dialects
|
|
11
|
+
from pyhakka.phon.converter import format_syllable
|
|
12
|
+
from pyhakka.phon.parser import parse_syllable, parse_syllables
|
|
13
|
+
from pyhakka.phon.schemes import normalize_scheme
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
@dataclass(frozen=True)
|
|
17
|
+
class CharReading:
|
|
18
|
+
"""One character reading in a Hakka variety."""
|
|
19
|
+
|
|
20
|
+
char: str
|
|
21
|
+
dialect: str
|
|
22
|
+
pron: str
|
|
23
|
+
hps_ext: str
|
|
24
|
+
ipa: str
|
|
25
|
+
tone: str
|
|
26
|
+
tone_value: str
|
|
27
|
+
form: str | None = None
|
|
28
|
+
mark: str | None = None
|
|
29
|
+
source: str | None = None
|
|
30
|
+
note: str | None = None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@dataclass(frozen=True)
|
|
34
|
+
class WordReading:
|
|
35
|
+
"""One multi-character lexical reading in a Hakka variety."""
|
|
36
|
+
|
|
37
|
+
word: str
|
|
38
|
+
dialect: str
|
|
39
|
+
pron: str
|
|
40
|
+
hps_ext: str
|
|
41
|
+
ipa: str
|
|
42
|
+
char_len: int
|
|
43
|
+
entry_type: str
|
|
44
|
+
source: str | None = None
|
|
45
|
+
note: str | None = None
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _build_char_reading(
|
|
49
|
+
row: Any,
|
|
50
|
+
dinfo: DialectInfo,
|
|
51
|
+
canon_scheme: str,
|
|
52
|
+
) -> CharReading:
|
|
53
|
+
raw_hps = row["hps_ext"]
|
|
54
|
+
raw_ipa = row["ipa"]
|
|
55
|
+
mark = row["mark"]
|
|
56
|
+
|
|
57
|
+
syl = None
|
|
58
|
+
if raw_hps:
|
|
59
|
+
try:
|
|
60
|
+
syl = parse_syllable(raw_hps, scheme="hps_ext", dialect=dinfo.slug)
|
|
61
|
+
except ValueError:
|
|
62
|
+
syl = None
|
|
63
|
+
if syl is None:
|
|
64
|
+
try:
|
|
65
|
+
syl = parse_syllable(raw_ipa, scheme="ipa", dialect=dinfo.slug)
|
|
66
|
+
except ValueError:
|
|
67
|
+
syl = None
|
|
68
|
+
|
|
69
|
+
hps_ext = raw_hps or (syl.hps_ext if syl is not None else raw_ipa)
|
|
70
|
+
tone = syl.tone if syl is not None else ""
|
|
71
|
+
tone_val = (syl.tone_value if syl is not None else "") or (
|
|
72
|
+
dinfo.tone_value_for(tone) if tone else ""
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
if canon_scheme == "hps_ext":
|
|
76
|
+
pron = hps_ext
|
|
77
|
+
elif canon_scheme == "ipa":
|
|
78
|
+
pron = raw_ipa
|
|
79
|
+
elif syl is not None:
|
|
80
|
+
pron = format_syllable(syl, scheme=canon_scheme, dialect=dinfo.slug)
|
|
81
|
+
else:
|
|
82
|
+
pron = raw_ipa
|
|
83
|
+
|
|
84
|
+
return CharReading(
|
|
85
|
+
char=str(row["char"]),
|
|
86
|
+
dialect=dinfo.slug,
|
|
87
|
+
pron=pron,
|
|
88
|
+
hps_ext=hps_ext,
|
|
89
|
+
ipa=raw_ipa,
|
|
90
|
+
tone=tone,
|
|
91
|
+
tone_value=tone_val,
|
|
92
|
+
form=row["form"],
|
|
93
|
+
mark=mark,
|
|
94
|
+
source=row["source"],
|
|
95
|
+
note=row["note"],
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def _format_word_pron(hps_ext: str, ipa: str, dinfo: DialectInfo, canon_scheme: str) -> str:
|
|
100
|
+
if canon_scheme == "hps_ext":
|
|
101
|
+
return hps_ext
|
|
102
|
+
if canon_scheme == "ipa":
|
|
103
|
+
return ipa
|
|
104
|
+
try:
|
|
105
|
+
syls = parse_syllables(hps_ext, scheme="hps_ext", dialect=dinfo.slug)
|
|
106
|
+
return " ".join(format_syllable(s, scheme=canon_scheme, dialect=dinfo.slug) for s in syls)
|
|
107
|
+
except ValueError:
|
|
108
|
+
return hps_ext
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def lookup_char(
|
|
112
|
+
char: str,
|
|
113
|
+
dialect: str = DEFAULT_DIALECT,
|
|
114
|
+
scheme: str = "hps_ext",
|
|
115
|
+
) -> list[CharReading]:
|
|
116
|
+
"""Look up all readings of a single Chinese character in `dialect`.
|
|
117
|
+
|
|
118
|
+
Supports both Traditional and Simplified characters seamlessly across all
|
|
119
|
+
10 curated core varieties and all 218 MCPDict varieties.
|
|
120
|
+
"""
|
|
121
|
+
if not char:
|
|
122
|
+
return []
|
|
123
|
+
dinfo = get_dialect(dialect)
|
|
124
|
+
canon_scheme = normalize_scheme(scheme)
|
|
125
|
+
conn = get_connection()
|
|
126
|
+
|
|
127
|
+
rows = conn.execute(
|
|
128
|
+
"""
|
|
129
|
+
SELECT char, hps_ext, ipa, form, mark, source, note
|
|
130
|
+
FROM char_readings
|
|
131
|
+
WHERE char = ? AND dialect_id = ?
|
|
132
|
+
ORDER BY seq
|
|
133
|
+
""",
|
|
134
|
+
(char, dinfo.id),
|
|
135
|
+
).fetchall()
|
|
136
|
+
|
|
137
|
+
# For MCPDict (Traditional-only) or missing simplified entries, bridge via simp_trad
|
|
138
|
+
if not rows and len(char) == 1:
|
|
139
|
+
variants = [v for v in expand_char_variants(char) if v != char]
|
|
140
|
+
if variants:
|
|
141
|
+
placeholders = ",".join("?" for _ in variants)
|
|
142
|
+
rows = conn.execute(
|
|
143
|
+
f"""
|
|
144
|
+
SELECT char, hps_ext, ipa, form, mark, source, note
|
|
145
|
+
FROM char_readings
|
|
146
|
+
WHERE char IN ({placeholders}) AND dialect_id = ?
|
|
147
|
+
ORDER BY char, seq
|
|
148
|
+
""",
|
|
149
|
+
(*variants, dinfo.id),
|
|
150
|
+
).fetchall()
|
|
151
|
+
|
|
152
|
+
return [_build_char_reading(r, dinfo, canon_scheme) for r in rows]
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def lookup_word(
|
|
156
|
+
word: str,
|
|
157
|
+
dialect: str = DEFAULT_DIALECT,
|
|
158
|
+
scheme: str = "hps_ext",
|
|
159
|
+
) -> list[WordReading]:
|
|
160
|
+
"""Look up a multi-character word/idiom in `dialect`'s curated word table.
|
|
161
|
+
|
|
162
|
+
Accepts both Traditional and Simplified input without requiring external libraries.
|
|
163
|
+
"""
|
|
164
|
+
if not word:
|
|
165
|
+
return []
|
|
166
|
+
dinfo = get_dialect(dialect)
|
|
167
|
+
if not dinfo.has_words:
|
|
168
|
+
return []
|
|
169
|
+
canon_scheme = normalize_scheme(scheme)
|
|
170
|
+
conn = get_connection()
|
|
171
|
+
|
|
172
|
+
rows = conn.execute(
|
|
173
|
+
"""
|
|
174
|
+
SELECT word, hps_ext, ipa, char_len, entry_type, source, note
|
|
175
|
+
FROM word_readings
|
|
176
|
+
WHERE dialect_id = ? AND word = ?
|
|
177
|
+
ORDER BY seq
|
|
178
|
+
""",
|
|
179
|
+
(dinfo.id, word),
|
|
180
|
+
).fetchall()
|
|
181
|
+
|
|
182
|
+
if not rows:
|
|
183
|
+
rows = conn.execute(
|
|
184
|
+
"""
|
|
185
|
+
SELECT word, hps_ext, ipa, char_len, entry_type, source, note
|
|
186
|
+
FROM word_readings
|
|
187
|
+
WHERE dialect_id = ? AND word_simp = ?
|
|
188
|
+
ORDER BY word, seq
|
|
189
|
+
""",
|
|
190
|
+
(dinfo.id, word),
|
|
191
|
+
).fetchall()
|
|
192
|
+
|
|
193
|
+
if not rows:
|
|
194
|
+
simp_query = to_simplified_text(word)
|
|
195
|
+
if simp_query != word:
|
|
196
|
+
rows = conn.execute(
|
|
197
|
+
"""
|
|
198
|
+
SELECT word, hps_ext, ipa, char_len, entry_type, source, note
|
|
199
|
+
FROM word_readings
|
|
200
|
+
WHERE dialect_id = ? AND (word_simp = ? OR word = ?)
|
|
201
|
+
ORDER BY word, seq
|
|
202
|
+
""",
|
|
203
|
+
(dinfo.id, simp_query, simp_query),
|
|
204
|
+
).fetchall()
|
|
205
|
+
|
|
206
|
+
return [
|
|
207
|
+
WordReading(
|
|
208
|
+
word=str(r["word"]),
|
|
209
|
+
dialect=dinfo.slug,
|
|
210
|
+
pron=_format_word_pron(r["hps_ext"], r["ipa"], dinfo, canon_scheme),
|
|
211
|
+
hps_ext=str(r["hps_ext"]),
|
|
212
|
+
ipa=str(r["ipa"]),
|
|
213
|
+
char_len=int(r["char_len"]),
|
|
214
|
+
entry_type=str(r["entry_type"]),
|
|
215
|
+
source=r["source"],
|
|
216
|
+
note=r["note"],
|
|
217
|
+
)
|
|
218
|
+
for r in rows
|
|
219
|
+
]
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def search_by_pron(
|
|
223
|
+
pron: str,
|
|
224
|
+
dialect: str = DEFAULT_DIALECT,
|
|
225
|
+
scheme: str = "hps_ext",
|
|
226
|
+
scope: str = "char",
|
|
227
|
+
limit: int = 100,
|
|
228
|
+
) -> list[CharReading] | list[WordReading]:
|
|
229
|
+
"""Reverse lookup: find characters or words matching a pronunciation.
|
|
230
|
+
|
|
231
|
+
Args:
|
|
232
|
+
pron: Pronunciation query (e.g. ``"ngin2"`` or ``"id7 ha5"``).
|
|
233
|
+
dialect: Dialect slug or alias (default: ``"meixian"``).
|
|
234
|
+
scheme: Scheme of `pron` AND the returned `.pron` field.
|
|
235
|
+
scope: ``"char"`` or ``"word"``.
|
|
236
|
+
limit: Maximum number of results.
|
|
237
|
+
"""
|
|
238
|
+
dinfo = get_dialect(dialect)
|
|
239
|
+
canon_scheme = normalize_scheme(scheme)
|
|
240
|
+
conn = get_connection()
|
|
241
|
+
|
|
242
|
+
if scope == "word":
|
|
243
|
+
if not dinfo.has_words:
|
|
244
|
+
return []
|
|
245
|
+
syls = parse_syllables(pron, scheme=canon_scheme, dialect=dinfo.slug)
|
|
246
|
+
hps_query = " ".join(s.hps_ext for s in syls)
|
|
247
|
+
rows = conn.execute(
|
|
248
|
+
"""
|
|
249
|
+
SELECT word, hps_ext, ipa, char_len, entry_type, source, note
|
|
250
|
+
FROM word_readings
|
|
251
|
+
WHERE dialect_id = ? AND hps_ext = ?
|
|
252
|
+
ORDER BY word, seq
|
|
253
|
+
LIMIT ?
|
|
254
|
+
""",
|
|
255
|
+
(dinfo.id, hps_query, limit),
|
|
256
|
+
).fetchall()
|
|
257
|
+
return [
|
|
258
|
+
WordReading(
|
|
259
|
+
word=str(r["word"]),
|
|
260
|
+
dialect=dinfo.slug,
|
|
261
|
+
pron=_format_word_pron(r["hps_ext"], r["ipa"], dinfo, canon_scheme),
|
|
262
|
+
hps_ext=str(r["hps_ext"]),
|
|
263
|
+
ipa=str(r["ipa"]),
|
|
264
|
+
char_len=int(r["char_len"]),
|
|
265
|
+
entry_type=str(r["entry_type"]),
|
|
266
|
+
source=r["source"],
|
|
267
|
+
note=r["note"],
|
|
268
|
+
)
|
|
269
|
+
for r in rows
|
|
270
|
+
]
|
|
271
|
+
|
|
272
|
+
if scope != "char":
|
|
273
|
+
raise ValueError(f"scope must be 'char' or 'word', got {scope!r}")
|
|
274
|
+
|
|
275
|
+
syl = parse_syllable(pron, scheme=canon_scheme, dialect=dinfo.slug)
|
|
276
|
+
hps_query = syl.hps_ext
|
|
277
|
+
|
|
278
|
+
if dinfo.tier == "core":
|
|
279
|
+
rows = conn.execute(
|
|
280
|
+
"""
|
|
281
|
+
SELECT char, hps_ext, ipa, form, mark, source, note
|
|
282
|
+
FROM char_readings
|
|
283
|
+
WHERE dialect_id = ? AND hps_ext = ?
|
|
284
|
+
ORDER BY char, seq
|
|
285
|
+
LIMIT ?
|
|
286
|
+
""",
|
|
287
|
+
(dinfo.id, hps_query, limit),
|
|
288
|
+
).fetchall()
|
|
289
|
+
return [_build_char_reading(r, dinfo, canon_scheme) for r in rows]
|
|
290
|
+
|
|
291
|
+
# MCPDict dialect: match normalized HPS-Ext or IPA
|
|
292
|
+
target_ipa = format_syllable(syl, scheme="ipa", dialect=dinfo.slug)
|
|
293
|
+
rows = conn.execute(
|
|
294
|
+
"""
|
|
295
|
+
SELECT char, hps_ext, ipa, form, mark, source, note
|
|
296
|
+
FROM char_readings
|
|
297
|
+
WHERE dialect_id = ? AND ipa = ?
|
|
298
|
+
ORDER BY char, seq
|
|
299
|
+
LIMIT ?
|
|
300
|
+
""",
|
|
301
|
+
(dinfo.id, target_ipa, limit),
|
|
302
|
+
).fetchall()
|
|
303
|
+
if rows:
|
|
304
|
+
return [_build_char_reading(r, dinfo, canon_scheme) for r in rows]
|
|
305
|
+
|
|
306
|
+
# Fallback: scan dialect's rows by parsed HPS-Ext match
|
|
307
|
+
matched: list[CharReading] = []
|
|
308
|
+
for r in conn.execute(
|
|
309
|
+
"""
|
|
310
|
+
SELECT char, hps_ext, ipa, form, mark, source, note
|
|
311
|
+
FROM char_readings
|
|
312
|
+
WHERE dialect_id = ?
|
|
313
|
+
""",
|
|
314
|
+
(dinfo.id,),
|
|
315
|
+
):
|
|
316
|
+
cr = _build_char_reading(r, dinfo, canon_scheme)
|
|
317
|
+
if cr.hps_ext == hps_query:
|
|
318
|
+
matched.append(cr)
|
|
319
|
+
if len(matched) >= limit:
|
|
320
|
+
break
|
|
321
|
+
return matched
|
|
322
|
+
|
|
323
|
+
|
|
324
|
+
def compare_dialects(
|
|
325
|
+
char: str,
|
|
326
|
+
dialects: Sequence[str] | None = None,
|
|
327
|
+
*,
|
|
328
|
+
tier: str = "core",
|
|
329
|
+
scheme: str = "hps_ext",
|
|
330
|
+
) -> dict[str, list[CharReading]]:
|
|
331
|
+
"""Compare readings of `char` across multiple Hakka varieties.
|
|
332
|
+
|
|
333
|
+
Args:
|
|
334
|
+
char: Single Chinese character (Traditional or Simplified).
|
|
335
|
+
dialects: Explicit list of dialect names/aliases. If omitted, queries all
|
|
336
|
+
varieties matching `tier` (``"core"`` = 10 curated, ``"mcpdict"`` = 218,
|
|
337
|
+
``"all"`` = all 228).
|
|
338
|
+
tier: Filter when `dialects` is None.
|
|
339
|
+
scheme: Output transcription scheme.
|
|
340
|
+
"""
|
|
341
|
+
if not char:
|
|
342
|
+
return {}
|
|
343
|
+
canon_scheme = normalize_scheme(scheme)
|
|
344
|
+
|
|
345
|
+
if dialects is not None:
|
|
346
|
+
target_infos = [get_dialect(d) for d in dialects]
|
|
347
|
+
elif tier == "all":
|
|
348
|
+
target_infos = list_dialects()
|
|
349
|
+
else:
|
|
350
|
+
target_infos = list_dialects(tier=tier)
|
|
351
|
+
|
|
352
|
+
id_to_info = {d.id: d for d in target_infos}
|
|
353
|
+
if not id_to_info:
|
|
354
|
+
return {}
|
|
355
|
+
|
|
356
|
+
variants = expand_char_variants(char) if len(char) == 1 else (char,)
|
|
357
|
+
conn = get_connection()
|
|
358
|
+
ph_chars = ",".join("?" for _ in variants)
|
|
359
|
+
rows = conn.execute(
|
|
360
|
+
f"""
|
|
361
|
+
SELECT dialect_id, char, hps_ext, ipa, form, mark, source, note
|
|
362
|
+
FROM char_readings
|
|
363
|
+
WHERE char IN ({ph_chars})
|
|
364
|
+
ORDER BY dialect_id, CASE WHEN char = ? THEN 0 ELSE 1 END, seq
|
|
365
|
+
""",
|
|
366
|
+
(*variants, char),
|
|
367
|
+
).fetchall()
|
|
368
|
+
|
|
369
|
+
grouped: dict[int, list[Any]] = {}
|
|
370
|
+
for r in rows:
|
|
371
|
+
did = int(r["dialect_id"])
|
|
372
|
+
if did in id_to_info:
|
|
373
|
+
grouped.setdefault(did, []).append(r)
|
|
374
|
+
|
|
375
|
+
result: dict[str, list[CharReading]] = {}
|
|
376
|
+
for dinfo in target_infos:
|
|
377
|
+
drows = grouped.get(dinfo.id, [])
|
|
378
|
+
# Prefer exact char match if present (important for core tables which already store both simp & trad)
|
|
379
|
+
exact = [r for r in drows if r["char"] == char]
|
|
380
|
+
chosen = exact if exact else drows
|
|
381
|
+
if chosen:
|
|
382
|
+
result[dinfo.slug] = [_build_char_reading(r, dinfo, canon_scheme) for r in chosen]
|
|
383
|
+
elif dialects is not None:
|
|
384
|
+
result[dinfo.slug] = []
|
|
385
|
+
return result
|
pyhakka/phon/__init__.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""Phonology module: canonical Syllable IR, multi-scheme parsing, conversion, and validation."""
|
|
2
|
+
|
|
3
|
+
from pyhakka.phon.converter import convert, format_syllable
|
|
4
|
+
from pyhakka.phon.parser import parse_syllable, parse_syllables
|
|
5
|
+
from pyhakka.phon.schemes import (
|
|
6
|
+
CANONICAL_SCHEMES,
|
|
7
|
+
DialectPhonProfile,
|
|
8
|
+
get_phon_profile,
|
|
9
|
+
normalize_scheme,
|
|
10
|
+
)
|
|
11
|
+
from pyhakka.phon.syllable import Syllable
|
|
12
|
+
from pyhakka.phon.validate import ValidationResult, validate_syllable
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"CANONICAL_SCHEMES",
|
|
16
|
+
"DialectPhonProfile",
|
|
17
|
+
"Syllable",
|
|
18
|
+
"ValidationResult",
|
|
19
|
+
"convert",
|
|
20
|
+
"format_syllable",
|
|
21
|
+
"get_phon_profile",
|
|
22
|
+
"normalize_scheme",
|
|
23
|
+
"parse_syllable",
|
|
24
|
+
"parse_syllables",
|
|
25
|
+
"validate_syllable",
|
|
26
|
+
]
|