Skip to content

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

fix: add Korean chapter-heading detection (제N장) to _chapter_number() (#84)

Varun Sahnieeab39d8ed8e
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index ca2fdc4..7d7084f 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -104,6 +104,22 @@ _TH_CHAPTER = re.compile(
rf"^\s*(?:#{{1,6}}\s+)?(?:บทที่|ตอนที่|ภาคที่|บท|ตอน|ภาค)\s*([0-9{_TH_DIGITS}]+)\b"
)
+# Korean chapter headings: "제1장 총칙", "## 제4장 근로시간과 휴식", "제6장의2 …".
+# 제 + Arabic numeral + a classifier (장 chapter / 편 part / 절 section / 관
+# subsection), with an optional "의N" branch suffix that Korean statutes use for
+# inserted chapters (제6장의2). Modern Korean numbers chapters with Arabic digits,
+# so unlike the Chinese branch no numeral composition is needed. Optional Markdown
+# "#" prefix so "## 제1장" is recognized in converted ebooks.
+#
+# The trailing group is the Korean analogue of _HEADING_TAIL: Korean has no letter
+# case, so the existing "capitalized title word" test does not transfer.
+# Requiring end-of-line, punctuation, or whitespace-then-content is what separates
+# a heading from a prose cross-reference, because Korean particles attach directly
+# to the noun ("제5장에서", "제2장의") with no intervening space.
+_KO_CHAPTER = re.compile(
+ r"^\s*(?:#{1,6}\s+)?제\s*([0-9]+)\s*[장편절관](?:\s*의\s*[0-9]+)?(?:\s*$|[.:\-]|\s+\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.
_TOC_HEADERS = (
@@ -242,8 +258,9 @@ def _chapter_number(line: str) -> int | None:
"""Return the chapter number if the line is a genuine chapter heading.
Handles Arabic ("Chapter 5", "Capítulo 5: ..."), Roman-numeral
- ("I: Loomings", "II. The Carpet-Bag") and Chinese ("第三章 …", "## 一 · …",
- "## 第一讲") heading styles.
+ ("I: Loomings", "II. The Carpet-Bag"), Chinese ("第三章 …", "## 一 · …",
+ "## 第一讲"), Thai ("บทที่ 3", "## บทที่ ๑"), and Korean ("제1장 총칙",
+ "## 제4장 근로시간과 휴식") heading styles.
"""
s = line.strip()
if len(s) > 80:
@@ -262,6 +279,9 @@ def _chapter_number(line: str) -> int | None:
tm = _TH_CHAPTER.match(s)
if tm:
return int(tm.group(1).translate(_TH_DIGIT_MAP))
+ km = _KO_CHAPTER.match(s)
+ if km:
+ return int(km.group(1))
return None
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index 0e158e7..5f27e07 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -627,6 +627,52 @@ class TestDetectStructure:
assert _chapter_number("บทความนี้ยาวมากและมีรายละเอียดเยอะ") is None
assert _chapter_number("ตอนนี้เรามาดูกันว่าเกิดอะไรขึ้น") is None
+ # ── Korean chapter headings ────────────────────────────────────────────
+
+ def test_korean_je_n_jang(self):
+ """Korean headings: `제N장` with Arabic digits."""
+ text = (
+ "제1장 총칙\n내용\n"
+ "제2장 근로시간\n내용\n"
+ "제3장 휴식\n내용"
+ )
+ assert detect_structure(text)["chapters_detected"] == 3
+
+ def test_korean_markdown_prefix(self):
+ """`## 제N장` with Markdown heading prefix."""
+ text = "## 제1장 서론\n내용\n## 제2장 본론\n내용"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_korean_inserted_chapter_suffix(self):
+ """`제6장의2` — inserted-chapter suffix used in Korean statutes."""
+ text = "제6장의2 직장 내 괴롭힘의 금지\n내용\n제7장 보칙\n내용"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_korean_article_is_not_chapter(self):
+ """`제N조` (article) is not a chapter classifier — deliberately excluded."""
+ from book_to_skill.utils import _chapter_number
+
+ assert _chapter_number("제56조 (연장·야간 및 휴일 근로)") is None
+
+ def test_korean_prose_cross_reference_not_chapter(self):
+ """Prose cross-references with particles are not headings."""
+ from book_to_skill.utils import _chapter_number
+
+ assert _chapter_number("이 장과 제5장에서 정한 근로시간…") is None
+ assert _chapter_number("제5장에서 정한 근로시간에 관한 규정은…") is None
+ assert _chapter_number("제2장의 규정에도 불구하고…") is None
+
+ def test_korean_dedups_toc_and_body(self):
+ """ToC entry and body heading with same number count once."""
+ text = "제1장 총칙\n제2장 근로시간\n## 제1장\n내용\n## 제2장\n내용"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_korean_other_classifiers(self):
+ """`제N편` (part), `제N절` (section), `제N관` (subsection) are also detected."""
+ text = "제1편 총칙\n내용\n제2장 정의\n내용\n제3절 통칙\n내용"
+ assert detect_structure(text)["chapters_detected"] == 3
+
+
def test_roman_footnote_reference_is_not_a_chapter(self):
"""Scholarly cross-references must stay rejected after the Roman change."""
from book_to_skill.utils import _chapter_number