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 Punjabi (ਕਾਂਡ) chapter headings (#280)

Shubham Srivastavafa6940143ddf
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index f375d34..a0d8c7f 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -294,6 +294,21 @@ _GU_CHAPTER = re.compile(
rf"^\s*(?:#{{1,6}}\s+)?(?:પ્રકરણ|અધ્યાય)\s*([0-9{_GU_DIGITS}]+)\b"
)
+# Punjabi (Gurmukhi) chapter headings: "ਕਾਂਡ 1", "ਅਧਿਆਇ ੧", "## ਕਾਂਡ 2".
+# Like Gujarati above, Punjabi uses two distinct words for "chapter" — ਕਾਂਡ
+# (usual in books) and ਅਧਿਆਇ — so both are matched. ਅਧਿਆਇ resembles Gujarati
+# અધ્યાય and Devanagari अध्याय but the scripts are disjoint (Gurmukhi
+# U+0A00-U+0A7F, Gujarati U+0A80-U+0AFF, Devanagari U+0900-U+097F), so none of
+# the three patterns can see another's text. Gurmukhi digits (U+0A66-U+0A6F) are
+# positional, so only a digit remap is needed. Requiring a number right after
+# the word keeps prose from matching, whether it follows the word with a
+# postposition ("ਕਾਂਡ ਵਿੱਚ") or inflects it ("ਕਾਂਡਾਂ").
+_PA_DIGITS = "੦-੯"
+_PA_DIGIT_MAP = str.maketrans("੦੧੨੩੪੫੬੭੮੯", "0123456789")
+_PA_CHAPTER = re.compile(
+ rf"^\s*(?:#{{1,6}}\s+)?(?:ਕਾਂਡ|ਅਧਿਆਇ)\s*([0-9{_PA_DIGITS}]+)\b"
+)
+
# Russian (Cyrillic) chapter headings: "Глава 1", "ГЛАВА 12", "## Глава 2".
# "Глава" ("chapter") + a number. Cyrillic uses ordinary Arabic digits, so —
# unlike the Devanagari/Bengali blocks above — no digit remap is needed. A
@@ -787,6 +802,9 @@ def _match_chapter_number(
gum = _GU_CHAPTER.match(s)
if gum:
return int(gum.group(1).translate(_GU_DIGIT_MAP))
+ pam = _PA_CHAPTER.match(s)
+ if pam:
+ return int(pam.group(1).translate(_PA_DIGIT_MAP))
rum = _RU_CHAPTER.match(s)
if rum:
return int(rum.group(1))
@@ -822,6 +840,7 @@ def _chapter_number(line: str, prev_line: str | None = None) -> int | None:
Kannada ("ಅಧ್ಯಾಯ 1", "ಅಧ್ಯಾಯ ೧", "## ಅಧ್ಯಾಯ 2"),
Malayalam ("അധ്യായം 1", "അധ്യായം ൧", "## അദ്ധ്യായം 2"),
Gujarati ("પ્રકરણ 1", "અધ્યાય ૧", "## પ્રકરણ 2"),
+ Punjabi ("ਕਾਂਡ 1", "ਅਧਿਆਇ ੧", "## ਕਾਂਡ 2"),
Russian ("Глава 1", "ГЛАВА 12", "## Глава 2"),
Greek ("Κεφάλαιο 1", "ΚΕΦΑΛΑΙΟ 12", "## Κεφάλαιο 2"),
Korean ("제1장 총칙", "## 제4장 근로시간과 휴식"), and
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index f7a9eca..d93eda7 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -797,6 +797,37 @@ class TestDetectStructure:
assert _chapter_number("પ્રકરણ") is None
assert _chapter_number("અધ્યાય") is None
+ def test_detects_punjabi_chapters(self):
+ """Punjabi headings: both words (ਕਾਂਡ/ਅਧਿਆਇ), Gurmukhi or Arabic digits."""
+ text = (
+ "ਕਾਂਡ ੧ ਜਾਣ-ਪਛਾਣ\nਸਾਰ\n"
+ "ਅਧਿਆਇ ੨ ਢੰਗ\nਸਾਰ\n"
+ "ਕਾਂਡ 3 ਨਤੀਜੇ\nਸਾਰ"
+ )
+ assert detect_structure(text)["chapters_detected"] == 3
+
+ def test_punjabi_markdown_prefix(self):
+ text = "## ਕਾਂਡ ੧ ਪਹਿਲਾ\nਸਾਰ\n## ਅਧਿਆਇ ੨ ਦੂਜਾ\nਸਾਰ"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_punjabi_prose_is_not_a_chapter_heading(self):
+ """A postposition phrase, an oblique form, or a bare word is not a heading."""
+ from book_to_skill.utils import _chapter_number
+
+ assert _chapter_number("ਇਸ ਕਾਂਡ ਵਿੱਚ ਅਸੀਂ ਚਰਚਾ ਕਰਾਂਗੇ") is None
+ assert _chapter_number("ਇਸ ਅਧਿਆਇ ਵਿੱਚ ਅਸੀਂ ਚਰਚਾ ਕਰਾਂਗੇ") is None
+ assert _chapter_number("ਸਾਰੇ ਕਾਂਡਾਂ ਦੀ ਸੂਚੀ") is None
+ assert _chapter_number("ਕਾਂਡ") is None
+ assert _chapter_number("ਅਧਿਆਇ") is None
+
+ def test_punjabi_does_not_collide_with_neighbouring_scripts(self):
+ """Gurmukhi (U+0A00–0A7F), Gujarati (U+0A80–0AFF) and Devanagari stay separate."""
+ from book_to_skill.utils import _chapter_number
+
+ assert _chapter_number("ਅਧਿਆਇ ੫") == 5
+ assert _chapter_number("અધ્યાય ૫") == 5
+ assert _chapter_number("अध्याय ५") == 5
+
def test_gujarati_adhyay_does_not_collide_with_hindi(self):
"""Gujarati અધ્યાય (U+0A85…) and Devanagari अध्याय (U+0905…) are distinct."""
from book_to_skill.utils import _chapter_number