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 Hebrew (פרק) chapter headings (#293)

Shubham Srivastava6236146654bf
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index 4ed93c1..b6e528f 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -342,6 +342,18 @@ _RU_CHAPTER = re.compile(r"^\s*(?:#{1,6}\s+)?глава\s+([0-9]+)\b", re.IGNORE
# matching.
_EL_CHAPTER = re.compile(r"^\s*(?:#{1,6}\s+)?κεφ[άα]λαιο\s+([0-9]+)\b", re.IGNORECASE)
+# Hebrew chapter headings: "פרק 1", "פרק 12", "## פרק 2".
+# פרק ("chapter") + a number. Hebrew is unicameral and uses ordinary Arabic
+# digits, so neither case folding nor a digit remap is needed. Being right-to-left
+# does not affect the pattern: the regex reads the logical order stored in the
+# file, which is פ-ר-ק then the number, and sanitize_extracted_text() removes any
+# bidi wrapper (RLM/RLE/PDF) before detect_structure() sees the line. Requiring
+# whitespace then a number keeps prose from matching — the word also means
+# "joint", and the prefixed and plural forms ("בפרק", "פרקים") are not followed by
+# a number. Gematria numbering ("פרק א"), common in religious texts, is a separate
+# letter-to-number mapping and is deliberately not handled here.
+_HE_CHAPTER = re.compile(r"^\s*(?:#{1,6}\s+)?פרק\s+([0-9]+)\b")
+
# Korean chapter headings: "제1장 총칙", "## 제4장 근로시간과 휴식", "제6장의2 …".
# 제 + Arabic numeral + a classifier (장 chapter / 편 part / 절 section / 관
# subsection), with an optional "의N" branch suffix that Korean statutes use for
@@ -842,6 +854,9 @@ def _match_chapter_number(
elm = _EL_CHAPTER.match(s)
if elm:
return int(elm.group(1))
+ hem = _HE_CHAPTER.match(s)
+ if hem:
+ return int(hem.group(1))
km = _KO_CHAPTER.match(s)
if km:
@@ -875,6 +890,7 @@ def _chapter_number(line: str, prev_line: str | None = None) -> int | None:
Punjabi ("ਕਾਂਡ 1", "ਅਧਿਆਇ ੧", "## ਕਾਂਡ 2"),
Russian ("Глава 1", "ГЛАВА 12", "## Глава 2"),
Greek ("Κεφάλαιο 1", "ΚΕΦΑΛΑΙΟ 12", "## Κεφάλαιο 2"),
+ Hebrew ("פרק 1", "פרק 12", "## פרק 2"),
Korean ("제1장 총칙", "## 제4장 근로시간과 휴식"), and
Persian ("فصل ۱", "فصل اول", "فصل بیست و یکم", "بخش ۲: مفاهیم",
"## فصل ۱: مقدمه", PDF-glued "فصل سی و چهارمخداحافظ…") heading styles — each
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index d731b68..f3e4229 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -897,6 +897,36 @@ class TestDetectStructure:
assert _chapter_number("Σε αυτό το κεφάλαιο θα δούμε") is None
assert _chapter_number("Κεφάλαιο") is None
+ def test_detects_hebrew_chapters(self):
+ """Hebrew headings: `פרק N`, with Arabic digits."""
+ text = (
+ "פרק 1 מבוא\nתקציר\n"
+ "פרק 2 שיטות\nתקציר\n"
+ "פרק 3 תוצאות\nתקציר"
+ )
+ assert detect_structure(text)["chapters_detected"] == 3
+
+ def test_hebrew_markdown_prefix(self):
+ text = "## פרק 1 ראשון\nתקציר\n## פרק 2 שני\nתקציר"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_hebrew_prose_is_not_a_chapter_heading(self):
+ """Plural/prefixed 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_hebrew_heading_survives_bidi_controls(self):
+ """Bidi wrappers are stripped before detection, so the heading still counts."""
+ from book_to_skill.sanitize import sanitize_extracted_text
+
+ text = "\u202bפרק 1 מבוא\u202c\nתקציר\n\u202bפרק 2 שיטות\u202c\nתקציר"
+ sanitized, removed = sanitize_extracted_text(text)
+ assert removed == 4
+ assert detect_structure(sanitized)["chapters_detected"] == 2
+
# ── Korean chapter headings ────────────────────────────────────────────
def test_korean_je_n_jang(self):