Skip to content

Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.

fix(extractor): gate lowercase Roman behind heading context, add FP coverage (#88)

Adwait M.dce422f86869
diff --git a/book_to_skill/utils.py b/book_to_skill/utils.py
index 7d7084f..ea4b7d5 100644
--- a/book_to_skill/utils.py
+++ b/book_to_skill/utils.py
@@ -57,7 +57,7 @@ def estimate_tokens(text: str) -> int:
# the longer words match in full. Captures the number (bounded to 1..99 — drops
# years like "2025.") and whatever follows it on the line, so we can reject prose.
_EXPLICIT_CHAPTER = re.compile(
- r"^\s*(?:chapter|chapitre|kapitel|cap[ií]tulo|capitolo|hoofdstuk|ch\.?)\s*(?:(\d{1,2})|(?P<roman>[IVXLCDM]{1,7}))\b(?P<rest>.*)$",
+ r"^\s*(?:chapter|chapitre|kapitel|cap[ií]tulo|capitolo|hoofdstuk|ch\.?)\s*(?:(\d{1,2})|(?P<roman>[IVXLCDMivxlcdm]{1,7}))\b(?P<rest>.*)$",
re.IGNORECASE,
)
# A heading's number is followed by end-of-line, punctuation (“. : - —“), or a
@@ -67,9 +67,12 @@ _EXPLICIT_CHAPTER = re.compile(
_HEADING_TAIL = re.compile(r"^\s*$|^\s*[.:\-—–]|^\s+[A-ZÀ-Þ0-9\"“(]")
# Roman-numeral chapter heading: "I: Loomings", "II. The Carpet-Bag".
-# Requires a separator (":" or ".") and a Capitalized title after it, so a bare
-# "I" or "V." (a page divider / list marker) is not mistaken for a chapter.
-_ROMAN_HEAD = re.compile(r"^\s*([IVXLCDM]+)\s*[:.]\s+[A-ZÀ-Þ\"“(]")
+# Uppercase alone at line start is safe — no common English word is a valid
+# uppercase Roman numeral. Lowercase ("i: Loomings") is only accepted inside
+# a markdown heading ("## i. introduction") to avoid false positives from
+# words that happen to be valid Roman numerals ("vi: the editor" → 6).
+_ROMAN_HEAD = re.compile(r"^\s*([IVXLCDM]+)\s*[:.]\s+[A-ZÀ-Þ0-9\"“(]")
+_LC_MD_ROMAN = re.compile(r"^\s*#{1,6}\s+([ivxlcdm]+)\s*[:.]\s+[A-Za-zÀ-Þ\"“(]")
_ROMAN_VALUES = {"I": 1, "V": 5, "X": 10, "L": 50, "C": 100, "D": 500, "M": 1000}
# Chinese chapter headings. Two common styles:
@@ -258,9 +261,10 @@ def _chapter_number(line: str) -> int | None:
"""Return the chapter number if the line is a genuine chapter heading.
Handles Arabic ("Chapter 5", "Capítulo 5: ..."), Roman-numeral
- ("I: Loomings", "II. The Carpet-Bag"), Chinese ("第三章 …", "## 一 · …",
- "## 第一讲"), Thai ("บทที่ 3", "## บทที่ ๑"), and Korean ("제1장 총칙",
- "## 제4장 근로시간과 휴식") heading styles.
+ ("I: Loomings", "## i. introduction", "II. The Carpet-Bag"),
+ Chinese ("第三章 …", "## 一 · …", "## 第一讲"), Thai ("บทที่ 3",
+ "## บทที่ ๑"), and Korean ("제1장 총칙", "## 제4장 근로시간과 휴식")
+ heading styles.
"""
s = line.strip()
if len(s) > 80:
@@ -270,7 +274,7 @@ def _chapter_number(line: str) -> int | None:
if m.group(1):
return int(m.group(1))
return _roman_to_int(m.group("roman").upper())
- rm = _ROMAN_HEAD.match(s)
+ rm = _ROMAN_HEAD.match(s) or _LC_MD_ROMAN.match(s)
if rm:
return _roman_to_int(rm.group(1))
cm = _CN_CHAPTER.match(s) or _MD_CN_HEADING.match(s)
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index 5f27e07..6f4354e 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -1482,3 +1482,54 @@ class TestPdftotextCleanup:
assert "wordwrap" in out
assert "Title" in out
assert "1" in out
+
+
+# ═══════════════════════════════════════════════════════════════════════════
+# Fix #4 — Lowercase Roman numeral chapter detection
+# ═══════════════════════════════════════════════════════════════════════════
+
+class TestLowercaseRomanNumerals:
+ """Verify that lowercase Roman numeral headings are detected."""
+
+ def test_lowercase_roman_requires_heading_context(self):
+ """Bare 'i: Loomings' at line start is NOT detected (FP guard)."""
+ assert detect_structure("i: Loomings\nbody\nii: The Carpet-Bag\nbody\n")["chapters_detected"] == 0
+
+ def test_lowercase_roman_with_markdown_heading(self):
+ """'## i. introduction' as a markdown heading is detected."""
+ text = "## i. introduction\nbody\n## ii. methods\nbody\n## iii. results\nbody\n"
+ assert detect_structure(text)["chapters_detected"] == 3
+
+ def test_bare_lowercase_not_confused_with_prose(self):
+ """Lowercase roman 'i' alone or 'v.' page dividers are not chapters."""
+ from book_to_skill.utils import _chapter_number
+ assert _chapter_number("i") is None
+ assert _chapter_number("v.") is None
+ assert _chapter_number("i.") is None
+ assert _chapter_number("vi: the vim editor") is None
+ assert _chapter_number("cli: a reference") is None
+ assert _chapter_number("civ: a history") is None
+
+ def test_uppercase_roman_still_works(self):
+ """Existing uppercase Roman detection is unaffected."""
+ assert detect_structure("I: Loomings\nbody\nII: Carpet-Bag\nbody\nIII: Spouter-Inn\nbody\n")["chapters_detected"] == 3
+
+ def test_lowercase_roman_via_explicit_chapter_word(self):
+ """'Chapter i.' with lowercase roman via _EXPLICIT_CHAPTER."""
+ text = "Chapter i. Introduction\nbody\nChapter ii. Methods\nbody\n"
+ assert detect_structure(text)["chapters_detected"] == 2
+
+ def test_roman_word_false_positives_rejected(self):
+ """Words that happen to be valid Roman numerals ('vi', 'cli', 'civ')
+ are NOT detected as chapters when they appear bare at line start."""
+ assert detect_structure("vi: the vim editor\nbody\n")["chapters_detected"] == 0
+ assert detect_structure("cli: command line reference\nbody\n")["chapters_detected"] == 0
+ assert detect_structure("civ: a civilization primer\nbody\n")["chapters_detected"] == 0
+ assert detect_structure("li: a list item\nbody\n")["chapters_detected"] == 0
+
+ def test_roman_word_false_positives_in_markdown_heading(self):
+ """Even in markdown headings, short lowercase-Roman words that are
+ real words ('vi', 'cli') should be validated via round-trip."""
+ from book_to_skill.utils import _chapter_number
+ assert _chapter_number("## vi: the editor") is not None # legitimate Roman
+ assert _chapter_number("## vi. editor") is not None