Skip to content

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

fix(extractor): detect full-width Arabic digits in CJK chapter headings (#46)

yukaina6e444f831595
diff --git a/scripts/extractor/utils.py b/scripts/extractor/utils.py
index e6d140e..3c08a6e 100644
--- a/scripts/extractor/utils.py
+++ b/scripts/extractor/utils.py
@@ -82,8 +82,12 @@ _CN_NUM_VALUES = {
}
_CN_NUM_UNITS = {"十": 10, "百": 100, "千": 1000}
_CN_NUM_CLASS = "〇零一二两三四五六七八九十百千"
-_CN_CHAPTER = re.compile(rf"^\s*第\s*([0-9{_CN_NUM_CLASS}]+)\s*[章回卷节篇讲]")
-_MD_CN_HEADING = re.compile(rf"^#{{1,6}}\s+第?\s*([{_CN_NUM_CLASS}]+)\s*[·、.::章回卷节篇讲]")
+# 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.
+_FW_DIGITS = "0-9"
+_CN_CHAPTER = re.compile(rf"^\s*第\s*([0-9{_FW_DIGITS}{_CN_NUM_CLASS}]+)\s*[章回卷节篇讲]")
+_MD_CN_HEADING = re.compile(rf"^#{{1,6}}\s+第?\s*([{_FW_DIGITS}{_CN_NUM_CLASS}]+)\s*[·、.::章回卷节篇讲]")
# Table-of-contents header lines across common languages. Anchored to a whole
# line (^\s*X\s*$) so an inline "the contents of this chapter" never matches.
diff --git a/tests/test_extractor.py b/tests/test_extractor.py
index d748314..bf5dca0 100644
--- a/tests/test_extractor.py
+++ b/tests/test_extractor.py
@@ -641,6 +641,17 @@ class TestDetectStructure:
text = "第一章 绪论\n正文。\n第二章 方法\n更多正文。\n"
assert detect_structure(text)["chapters_detected"] == 2
+ def test_japanese_fullwidth_digit_chapters(self):
+ # Full-width Arabic digits (U+FF10–U+FF19) in "第N章" are common in
+ # Japanese typesetting and must be detected like half-width "第1章".
+ text = "第1章 はじめに\n本文。\n第2章 つぎ\n本文。\n"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_fullwidth_multi_digit_chapter(self):
+ # Multi-digit full-width numbers ("第10章") resolve to the right int.
+ text = "第1章 序\n第10章 終\n"
+ assert detect_structure(text)["chapters_detected"] == 2
+
def test_chinese_di_n_jiang_lecture(self):
# lecture transcripts numbered 第N讲
text = "第一讲\n正文\n第二讲\n正文\n第三讲\n正文\n"
@@ -710,6 +721,7 @@ class TestDetectStructure:
assert _cn_numeral_to_int("二十一") == 21
assert _cn_numeral_to_int("一百零八") == 108
assert _cn_numeral_to_int("15") == 15
+ assert _cn_numeral_to_int("12") == 12 # full-width Arabic digits
assert _cn_numeral_to_int("不是数字") is None
assert _cn_numeral_to_int("9999") is None # out of 1..999 chapter range