Skip to content

Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.

fix: detect chapters numbered with Kangxi-radical numerals (#175)

jsz119-create8a2cae63f0b1
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index f0ac2b2..5beadfb 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -70,8 +70,12 @@ _EPUB_IMAGE_NOTICE_THRESHOLD = 5
# those characters fell through to the whitespace-word branch, where a
# space-less run of them counts as a single "word": the same ~1000x undercount
# #103 fixed for the BMP, one plane up.
+# The Kangxi-radical range (U+2F00-U+2FDF) is included because some Chinese
+# ebooks render ordinary Han characters — 网 as ⽹ (U+2F79), 大 as ⼤
+# (U+2F24), 一 as ⼀ (U+2F00) — as radical forms throughout the whole text;
+# without it such a book still falls through to the whitespace-word branch.
_CJK_RE = re.compile(
- r"[ -〿぀-ヿ㐀-䶿一-鿿"
+ r"[⼀-⿟ -〿぀-ヿ㐀-䶿一-鿿"
r"가-힣豈-﫿＀-￯"
r"\U00020000-\U0003FFFF]"
)
@@ -140,6 +144,19 @@ _CN_NUM_VALUES = {
}
_CN_NUM_UNITS = {"十": 10, "百": 100, "千": 1000}
_CN_NUM_CLASS = "〇零一二两三四五六七八九十百千"
+
+# Kangxi-radical numerals → CJK ideograph numerals. Some Chinese ebooks
+# (e.g. certain e-reader platforms) encode numerals as Kangxi radicals from
+# the U+2F00 block instead of CJK unified ideographs — "第⼀章" with
+# U+2F00 (⼀) rather than U+4E00 (一). NFKC does not map these, so normalize
+# them explicitly before chapter detection. Only numerals that exist as
+# Kangxi radicals are listed (三/四/五/六/七/九 have no radical form).
+_KANGXI_NUMERAL_TRANS = {
+ 0x2F00: ord("一"), # ⼀ KANGXI RADICAL ONE
+ 0x2F06: ord("二"), # ⼆ KANGXI RADICAL TWO
+ 0x2F0B: ord("八"), # ⼋ KANGXI RADICAL EIGHT
+ 0x2F17: ord("十"), # ⼗ KANGXI RADICAL TEN
+}
# Full-width Arabic digits (U+FF10–U+FF19) are common in Japanese typesetting,
# e.g. "第1章". int() already parses them (str.isdigit() is True), so only the
# regex character classes need to accept them.
@@ -492,7 +509,9 @@ def _roman_to_int(s: str) -> int | None:
def _match_chapter_number(line: str) -> int | None:
"""Return the chapter number if the line is a genuine chapter heading,
with no Markdown/AsciiDoc heading prefix (the caller strips it first)."""
- s = line.strip()
+ # Normalize Kangxi-radical numerals (⼀⼆⼋⼗) to ideographs so Chinese
+ # ebooks that encode chapter numbers in the U+2F00 block are detected.
+ s = line.strip().translate(_KANGXI_NUMERAL_TRANS)
if len(s) > 80:
return None
m = _EXPLICIT_CHAPTER.match(s)
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index 42dcce7..35d3455 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -1031,6 +1031,33 @@ class TestDetectStructure:
assert _cn_numeral_to_int("不是数字") is None
assert _cn_numeral_to_int("9999") is None # out of 1..999 chapter range
+ # ── Kangxi-radical numerals (U+2F00 block) ──────────────────────────────
+ # Some Chinese ebooks (e.g. certain e-reader platforms) encode numerals as
+ # Kangxi radicals instead of CJK ideographs: 第⼀章 with U+2F00, not U+4E00.
+ # NFKC does not map these, so detection must normalize them explicitly.
+
+ def test_kangxi_radical_chapter_headings(self):
+ text = (
+ "第⼀章\n正文\n" # U+2F00 KANGXI RADICAL ONE
+ "第⼆章\n正文\n" # U+2F06 KANGXI RADICAL TWO
+ "第⼋章\n正文\n" # U+2F0B KANGXI RADICAL EIGHT
+ "第⼗章\n正文\n" # U+2F17 KANGXI RADICAL TEN
+ "第⼗⼀章\n正文\n" # ⼗⼀ = 11
+ "第⼗⼆章\n正文\n" # ⼗⼆ = 12
+ )
+ assert detect_structure(text)["chapters_detected"] == 6
+
+ def test_kangxi_mixed_with_ideograph_chapters(self):
+ # Real-world mix from an actual ebook: radicals for 一/二/八/十,
+ # ideographs for the rest — all 12 chapters must be found.
+ nums = ["⼀", "⼆", "三", "四", "五", "六", "七", "⼋", "九", "⼗", "⼗⼀", "⼗⼆"]
+ text = "".join(f"第{n}章\n正文。\n" for n in nums)
+ assert detect_structure(text)["chapters_detected"] == 12
+
+ def test_kangxi_radical_in_markdown_heading(self):
+ text = "## 第⼀讲\n正文\n## 第⼆讲\n正文\n"
+ assert detect_structure(text)["chapters_detected"] == 2
+
def test_french_chapitre(self):
assert detect_structure("Chapitre 1\nx\nChapitre 2\nx")["chapters_detected"] == 2
@@ -2023,6 +2050,12 @@ class TestCjkTokenEstimate:
def test_empty_is_zero(self):
assert estimate_tokens("") == 0
+ def test_kangxi_radicals_counted_as_cjk(self):
+ # Some Chinese ebooks render Han characters as Kangxi radicals
+ # throughout (网 as ⽹ U+2F79, 大 as ⼤ U+2F24, 一 as ⼀ U+2F00).
+ # A space-less run of them must not fall into the word branch.
+ assert estimate_tokens("⼀" * 1500) == 1000
+
class TestPdfLibsCleanup:
"""extract_with_pypdf / extract_with_pdfminer also clean their output."""