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 Sinhala (පරිච්ඡේදය) chapter headings (#285)

Shubham Srivastava55d4014a6b81
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index a0d8c7f..18b71b0 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -243,6 +243,21 @@ _TA_CHAPTER = re.compile(
rf"^\s*(?:#{{1,6}}\s+)?அத்தியாயம்\s*([0-9{_TA_DIGITS}]+)\b"
)
+# Sinhala chapter headings: "පරිච්ඡේදය 1", "පරිච්ඡේදය ෨", "## පරිච්ඡේදය 2".
+# පරිච්ඡේදය ("chapter") + a number. Grouped next to Tamil as the other Sri
+# Lankan script. Modern Sinhala prints numbers with ASCII digits; the Lith
+# digits (U+0DE6-U+0DEF) are included for historical texts, remapped like the
+# other Indic blocks. Only පරිච්ඡේදය is matched, not the Sanskrit loan අධ්‍යාය:
+# that word carries a ZERO WIDTH JOINER inside its conjunct, and
+# sanitize_extracted_text() strips U+200D before detect_structure() ever sees
+# the text, so a pattern containing one could never match. Requiring a number
+# keeps prose that merely inflects the word ("පරිච්ඡේදයේ") from matching.
+_SI_DIGITS = "෦-෯"
+_SI_DIGIT_MAP = str.maketrans("෦෧෨෩෪෫෬෭෮෯", "0123456789")
+_SI_CHAPTER = re.compile(
+ rf"^\s*(?:#{{1,6}}\s+)?පරිච්ඡේදය\s*([0-9{_SI_DIGITS}]+)\b"
+)
+
# Telugu chapter headings: "అధ్యాయము 1", "అధ్యాయం ౧", "## అధ్యాయం 2".
# The chapter word has two common spellings — అధ్యాయము (formal) and అధ్యాయం
# (modern), sharing the stem అధ్యాయ — so both endings are matched. Telugu digits
@@ -790,6 +805,9 @@ def _match_chapter_number(
tam = _TA_CHAPTER.match(s)
if tam:
return int(tam.group(1).translate(_TA_DIGIT_MAP))
+ sim = _SI_CHAPTER.match(s)
+ if sim:
+ return int(sim.group(1).translate(_SI_DIGIT_MAP))
tem = _TE_CHAPTER.match(s)
if tem:
return int(tem.group(1).translate(_TE_DIGIT_MAP))
@@ -836,6 +854,7 @@ def _chapter_number(line: str, prev_line: str | None = None) -> int | None:
Bengali ("অধ্যায় 1", "অধ্যায় ১", "## অধ্যায় 2"),
Odia ("ଅଧ୍ୟାୟ 1", "ଅଧ୍ୟାୟ ୧", "## ଅଧ୍ୟାୟ 2"),
Tamil ("அத்தியாயம் 1", "அத்தியாயம் ௧", "## அத்தியாயம் 2"),
+ Sinhala ("පරිච්ඡේදය 1", "පරිච්ඡේදය ෨", "## පරිච්ඡේදය 2"),
Telugu ("అధ్యాయము 1", "అధ్యాయం ౧", "## అధ్యాయం 2"),
Kannada ("ಅಧ್ಯಾಯ 1", "ಅಧ್ಯಾಯ ೧", "## ಅಧ್ಯಾಯ 2"),
Malayalam ("അധ്യായം 1", "അധ്യായം ൧", "## അദ്ധ്യായം 2"),
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index d93eda7..b37fb1a 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -715,6 +715,27 @@ class TestDetectStructure:
assert _chapter_number("இந்த அத்தியாயத்தில் நாம் பார்ப்போம்") is None
assert _chapter_number("அத்தியாயம்") is None
+ def test_detects_sinhala_chapters(self):
+ """Sinhala headings: `පරිච්ඡේදය N`, Arabic or Sinhala Lith digits."""
+ text = (
+ "පරිච්ඡේදය 1 හැඳින්වීම\nසාරාංශය\n"
+ "පරිච්ඡේදය ෨ ක්‍රම\nසාරාංශය\n"
+ "පරිච්ඡේදය 3 ප්‍රතිඵල\nසාරාංශය"
+ )
+ assert detect_structure(text)["chapters_detected"] == 3
+
+ def test_sinhala_markdown_prefix(self):
+ text = "## පරිච්ඡේදය 1 පළමු\nසාරාංශය\n## පරිච්ඡේදය 2 දෙවන\nසාරාංශය"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_sinhala_prose_is_not_a_chapter_heading(self):
+ """Inflected forms (පරිච්ඡේදයේ / පරිච්ඡේදයක්) and the bare word 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
+
def test_detects_telugu_chapters(self):
"""Telugu headings: both spellings (అధ్యాయము/అధ్యాయం), Telugu or Arabic digits."""
text = (