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 Gujarati (પ્રકરણ) chapter headings (#252)

Shubham Srivastavabf16dcd0d245
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index 3b1718f..c6bcd28 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -265,6 +265,21 @@ _ML_CHAPTER = re.compile(
rf"^\s*(?:#{{1,6}}\s+)?അ(?:ധ്|ദ്ധ്)യായം\s*([0-9{_ML_DIGITS}]+)\b"
)
+# Gujarati chapter headings: "પ્રકરણ 1", "અધ્યાય ૧", "## પ્રકરણ 2".
+# Gujarati uses two distinct words for "chapter" — પ્રકરણ (common in modern
+# books) and અધ્યાય (classical/religious) — so both are matched. અધ્યાય shares
+# its transliteration with Devanagari अध्याय (the Hindi block above) but not its
+# codepoints (U+0A85.. vs U+0905..), so the two scripts cannot collide. Gujarati
+# digits (U+0AE6-U+0AEF) are positional like the other Indic blocks above, so
+# only a digit remap is needed. Unlike Malayalam, an inflected form keeps the
+# whole word ("પ્રકરણમાં" is the word + માં), so requiring a number right after
+# it is what keeps prose from matching.
+_GU_DIGITS = "૦-૯"
+_GU_DIGIT_MAP = str.maketrans("૦૧૨૩૪૫૬૭૮૯", "0123456789")
+_GU_CHAPTER = re.compile(
+ rf"^\s*(?:#{{1,6}}\s+)?(?:પ્રકરણ|અધ્યાય)\s*([0-9{_GU_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
@@ -693,6 +708,9 @@ def _match_chapter_number(
mlm = _ML_CHAPTER.match(s)
if mlm:
return int(mlm.group(1).translate(_ML_DIGIT_MAP))
+ gum = _GU_CHAPTER.match(s)
+ if gum:
+ return int(gum.group(1).translate(_GU_DIGIT_MAP))
rum = _RU_CHAPTER.match(s)
if rum:
return int(rum.group(1))
@@ -726,6 +744,7 @@ def _chapter_number(line: str, prev_line: str | None = None) -> int | None:
Telugu ("అధ్యాయము 1", "అధ్యాయం ౧", "## అధ్యాయం 2"),
Kannada ("ಅಧ್ಯಾಯ 1", "ಅಧ್ಯಾಯ ೧", "## ಅಧ್ಯಾಯ 2"),
Malayalam ("അധ്യായം 1", "അധ്യായം ൧", "## അദ്ധ്യായം 2"),
+ Gujarati ("પ્રકરણ 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 f1c0779..b885ed3 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -748,6 +748,35 @@ class TestDetectStructure:
assert _chapter_number("ഈ അധ്യായത്തിൽ നാം ചർച്ച ചെയ്യുന്നു") is None
assert _chapter_number("അധ്യായം") is None
+ def test_detects_gujarati_chapters(self):
+ """Gujarati headings: both words (પ્રકરણ/અધ્યાય), Gujarati or Arabic digits."""
+ text = (
+ "પ્રકરણ ૧ પરિચય\nસારાંશ\n"
+ "અધ્યાય ૨ પદ્ધતિઓ\nસારાંશ\n"
+ "પ્રકરણ 3 પરિણામો\nસારાંશ"
+ )
+ assert detect_structure(text)["chapters_detected"] == 3
+
+ def test_gujarati_markdown_prefix(self):
+ text = "## પ્રકરણ ૧ પહેલું\nસારાંશ\n## અધ્યાય ૨ બીજું\nસારાંશ"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_gujarati_prose_is_not_a_chapter_heading(self):
+ """Inflected forms (પ્રકરણમાં/અધ્યાયમાં) and bare words are not headings."""
+ 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
+
+ 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
+
+ assert _chapter_number("અધ્યાય ૫") == 5
+ assert _chapter_number("अध्याय ५") == 5
+
def test_detects_russian_chapters(self):
"""Russian headings: `Глава N`, case-insensitive, with Arabic digits."""
text = (