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 Malayalam (അധ്യായം) chapter headings (#243)

Shubham Srivastava6dc94f178644
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index 758b10d..7b0cff9 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -234,6 +234,19 @@ _KN_CHAPTER = re.compile(
rf"^\s*(?:#{{1,6}}\s+)?ಅಧ್ಯಾಯ\s*([0-9{_KN_DIGITS}]+)\b"
)
+# Malayalam chapter headings: "അധ്യായം 1", "അധ്യായം ൧", "## അദ്ധ്യായം 2".
+# The chapter word has two common spellings — അധ്യായം (modern) and അദ്ധ്യായം
+# (older ദ്ധ orthography), differing only in that conjunct — so both stems are
+# matched. Malayalam digits (U+0D66-U+0D6F) are positional like the other Indic
+# blocks above, so only a digit remap is needed. Requiring the full word
+# (including the final ം) then a number keeps prose that merely uses an
+# inflected form like "അധ്യായത്തിൽ" from matching.
+_ML_DIGITS = "൦-൯"
+_ML_DIGIT_MAP = str.maketrans("൦൧൨൩൪൫൬൭൮൯", "0123456789")
+_ML_CHAPTER = re.compile(
+ rf"^\s*(?:#{{1,6}}\s+)?അ(?:ധ്|ദ്ധ്)യായം\s*([0-9{_ML_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
@@ -626,6 +639,9 @@ def _match_chapter_number(line: str) -> int | None:
knm = _KN_CHAPTER.match(s)
if knm:
return int(knm.group(1).translate(_KN_DIGIT_MAP))
+ mlm = _ML_CHAPTER.match(s)
+ if mlm:
+ return int(mlm.group(1).translate(_ML_DIGIT_MAP))
rum = _RU_CHAPTER.match(s)
if rum:
return int(rum.group(1))
@@ -655,6 +671,7 @@ def _chapter_number(line: str) -> int | None:
Tamil ("அத்தியாயம் 1", "அத்தியாயம் ௧", "## அத்தியாயம் 2"),
Telugu ("అధ్యాయము 1", "అధ్యాయం ౧", "## అధ్యాయం 2"),
Kannada ("ಅಧ್ಯಾಯ 1", "ಅಧ್ಯಾಯ ೧", "## ಅಧ್ಯಾಯ 2"),
+ Malayalam ("അധ്യായം 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 933114e..02131ec 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -728,6 +728,26 @@ class TestDetectStructure:
assert _chapter_number("ಈ ಅಧ್ಯಾಯದಲ್ಲಿ ನಾವು ಚರ್ಚಿಸುತ್ತೇವೆ") is None
assert _chapter_number("ಅಧ್ಯಾಯ") is None
+ def test_detects_malayalam_chapters(self):
+ """Malayalam headings: both spellings (അധ്യായം/അദ്ധ്യായം), Malayalam or Arabic digits."""
+ text = (
+ "അധ്യായം ൧ ആമുഖം\nസംഗ്രഹം\n"
+ "അദ്ധ്യായം ൨ രീതികൾ\nസംഗ്രഹം\n"
+ "അധ്യായം 3 ഫലങ്ങൾ\nസംഗ്രഹം"
+ )
+ assert detect_structure(text)["chapters_detected"] == 3
+
+ def test_malayalam_markdown_prefix(self):
+ text = "## അധ്യായം ൧ ഒന്നാമത്തേത്\nസംഗ്രഹം\n## അദ്ധ്യായം ൨ രണ്ടാമത്തേത്\nസംഗ്രഹം"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_malayalam_prose_is_not_a_chapter_heading(self):
+ """An inflected 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
+
def test_detects_russian_chapters(self):
"""Russian headings: `Глава N`, case-insensitive, with Arabic digits."""
text = (