Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

fix: generalize inline prose formula detection

myhloliba1059c9bb44
diff --git a/mineru/model/flash/pdf/formulas.py b/mineru/model/flash/pdf/formulas.py
index 23a90b57..8de407ba 100644
--- a/mineru/model/flash/pdf/formulas.py
+++ b/mineru/model/flash/pdf/formulas.py
@@ -715,12 +715,26 @@ def _formula_line_has_math_operator(text: str) -> bool:
return any(character in _FORMULA_OPERATOR_CHARS for character in text)
+def _formula_prefix_has_prose(prefix: str) -> bool:
+ """用通用文字数量识别公式前的正文片段,不依赖特定引导词或标点。"""
+
+ prose_prefix = re.sub(
+ r"[({\[([【{][^)}\])]】}]*[)}\])]】}]",
+ " ",
+ prefix,
+ )
+ if len(re.findall(r"[\u3400-\u9fff]", prose_prefix)) >= 2:
+ return True
+ latin_word_count = sum(re.search(r"[A-Za-z]{2,}", token) is not None for token in prose_prefix.split())
+ return latin_word_count >= 2
+
+
def _formula_component_has_left_prose(
members: list[tuple[_LineItem, BBox]],
lane: _TextLane,
median_height: float,
) -> bool:
- """识别贴栏左缘且在首个运算符前带正文引导语的伪行间公式。"""
+ """识别贴栏左缘且在首个运算符前带同行正文的伪行间公式。"""
for line, bbox in members:
if abs(bbox[0] - lane.left) > 0.75 * median_height:
@@ -730,23 +744,7 @@ def _formula_component_has_left_prose(
if not operator_positions:
continue
prefix = normalized[: min(operator_positions)]
- han_count = len(re.findall(r"[\u3400-\u9fff]", prefix))
- has_clause_punctuation = (
- re.search(
- r"[,;:。!?!?))]",
- prefix,
- )
- is not None
- )
- has_english_lead = (
- re.search(
- r"\b(?:where|when|with|given|using|denote[ds]?|defined)\b",
- prefix,
- re.IGNORECASE,
- )
- is not None
- )
- if (han_count >= 2 and has_clause_punctuation) or has_english_lead:
+ if _formula_prefix_has_prose(prefix):
return True
return False
diff --git a/tests/unittest/test_flash_pdf_formulas.py b/tests/unittest/test_flash_pdf_formulas.py
index 7c8ab235..669f46c3 100644
--- a/tests/unittest/test_flash_pdf_formulas.py
+++ b/tests/unittest/test_flash_pdf_formulas.py
@@ -70,11 +70,20 @@ def test_formula_members_expose_union_of_tight_bboxes_with_one_point_padding() -
)
-def test_formula_component_rejects_left_aligned_prose_lead() -> None:
- """验证同栏左缘带正文引导语的复杂行内分式不升级为行间公式。"""
+ "text",
+ [
+ "(℃/hm),用 I 表示:I=ΔT",
+ "温度可表示为T=ΔT",
+ "The temperature value T=ΔT",
+ ],
+ ids=["chinese-with-unit", "chinese-without-punctuation", "english-without-keyword"],
+)
+def test_formula_component_rejects_left_aligned_prose_prefix(text: str) -> None:
+ """验证同栏左缘带通用正文前缀的复杂行内分式不升级为行间公式。"""
prose = _text_line(
- "(℃/hm),用 I 表示:I=ΔT",
+ text,
(0.0, 20.0, 55.0, 30.0),
0,
effective_height=10.0,
@@ -107,11 +116,50 @@ def test_formula_component_rejects_left_aligned_prose_lead() -> None:
10.0,
)
- formula = replace(
- prose,
- text="I=ΔT",
- bbox=(20.0, 20.0, 55.0, 30.0),
+
+ "text",
+ [
+ "I=ΔT",
+ "x+y=z",
+ "Score=MLP(vCLS)",
+ "conf(Bm)=1",
+ "(摄氏度)=T",
+ "(temperature value)=T",
+ ],
+ ids=[
+ "single-variable",
+ "symbolic-expression",
+ "single-identifier",
+ "function-identifier",
+ "chinese-bracketed-unit",
+ "english-bracketed-unit",
+ ],
+)
+def test_formula_component_keeps_left_aligned_formula_identifiers(text: str) -> None:
+ """验证变量、符号表达式、无空白标识符和纯括号单位仍保留为独立公式。"""
+
+ formula = _text_line(
+ text,
+ (0.0, 20.0, 55.0, 30.0),
+ 0,
+ effective_height=10.0,
)
+ denominator = _text_line(
+ "ΔH",
+ (45.0, 30.0, 55.0, 38.0),
+ 1,
+ effective_height=8.0,
+ )
+ lane = models._TextLane(
+ left=0.0,
+ right=100.0,
+ lines=[
+ (formula, formula.bbox),
+ (denominator, denominator.bbox),
+ ],
+ )
+
assert not formulas._formula_component_has_left_prose(
[(formula, formula.bbox), (denominator, denominator.bbox)],
lane,
@@ -119,6 +167,57 @@ def test_formula_component_rejects_left_aligned_prose_lead() -> None:
)
+def test_inline_prose_formula_component_returns_to_paragraph_context() -> None:
+ """验证同行正文公式不生成公式块,并标记为后续正文聚合上下文。"""
+
+ body_font = ("Body", 0)
+ prose_formula = _text_line(
+ "温度可表示为T=ΔT",
+ (0.0, 40.0, 58.0, 50.0),
+ 0,
+ effective_height=10.0,
+ font_signature=("Math", 0),
+ font_coverage=0.6,
+ )
+ denominator = _text_line(
+ "ΔH",
+ (25.0, 51.0, 55.0, 61.0),
+ 1,
+ effective_height=10.0,
+ font_signature=("Math", 0),
+ font_coverage=0.6,
+ )
+ number = _text_line(
+ "(5)",
+ (91.0, 49.0, 100.0, 59.0),
+ 2,
+ effective_height=10.0,
+ )
+ body_lines = [
+ _text_line(
+ f"body-{index}",
+ (0.0, top, 100.0, top + 10.0),
+ 3 + index,
+ effective_height=10.0,
+ font_signature=body_font,
+ font_coverage=1.0,
+ )
+ for index, top in enumerate((76.0, 88.0, 100.0, 112.0))
+ ]
+
+ blocks, remaining = formulas._build_formula_like_blocks(
+ [prose_formula, denominator, number, *body_lines],
+ [],
+ (100.0, 140.0),
+ )
+
+ assert blocks == []
+ assert prose_formula.paragraph_formula_context
+ assert denominator.paragraph_formula_context
+ assert number.paragraph_formula_context
+ assert {line.source_index for line in remaining} == set(range(7))
+
+
def _vector_path(
bbox: tuple[float, float, float, float],
source_index: int,