Skip to content

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

feat(extractor): detect Vietnamese (Chương) chapter headings (#194)

Shubham Srivastavaae28e78a4665
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index 0513e6b..08658f3 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -101,12 +101,13 @@ def estimate_tokens(text: str) -> int:
# Explicit chapter heading: "Chapter 5", "Capítulo 5: ...", "Chapter 1. Intro".
-# Also French/German/Italian/Dutch chapter words (chapitre/kapitel/capitolo/
-# hoofdstuk), matching the ToC languages added alongside. "ch.?" stays last so
-# the longer words match in full. Captures the number (bounded to 1..99 — drops
-# years like "2025.") and whatever follows it on the line, so we can reject prose.
+# Also French/German/Italian/Dutch/Vietnamese chapter words (chapitre/kapitel/
+# capitolo/hoofdstuk/chương), matching the ToC languages added alongside. "ch.?"
+# stays last so the longer words match in full. Captures the number (bounded to
+# 1..99 — drops years like "2025.") and whatever follows it on the line, so we
+# can reject prose.
_EXPLICIT_CHAPTER = re.compile(
- r"^\s*(?:chapter|chapitre|kapitel|cap[ií]tulo|capitolo|hoofdstuk|ch\.?)\s*(?:(\d{1,2})|(?P<roman>[IVXLCDMivxlcdm]{1,7}))\b(?P<rest>.*)$",
+ r"^\s*(?:chapter|chapitre|kapitel|cap[ií]tulo|capitolo|hoofdstuk|chương|ch\.?)\s*(?:(\d{1,2})|(?P<roman>[IVXLCDMivxlcdm]{1,7}))\b(?P<rest>.*)$",
re.IGNORECASE,
)
# A heading's number is followed by end-of-line, punctuation (“. : - —“), or a
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index ca656f9..d40302d 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -1088,6 +1088,16 @@ class TestDetectStructure:
def test_dutch_hoofdstuk(self):
assert detect_structure("Hoofdstuk 1\nx\nHoofdstuk 2\nx")["chapters_detected"] == 2
+ def test_vietnamese_chuong(self):
+ assert detect_structure("Chương 1\nx\nChương 2\nx")["chapters_detected"] == 2
+
+ def test_vietnamese_chuong_not_program(self):
+ # "Chương trình" (program) starts with the chapter word but is not a
+ # heading — no number follows "Chương", so it must not match.
+ from book_to_skill.utils import _chapter_number
+
+ assert _chapter_number("Chương trình 1 của khóa học") is None
+
def test_german_kapitel_with_title(self):
text = "Kapitel 1: Einführung\nx\nKapitel 2: Methoden\nx"
assert detect_structure(text)["chapters_detected"] == 2