mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-06-29 15:31:05 +08:00
fix(rag/nlp): handle non-numbered DOCX heading styles (#16219)
## What problem does this PR solve?
DOCX parsing could crash when a paragraph used a `Heading`-prefixed
style without a trailing numeric level, such as `Heading`, `Heading1`,
or `Heading Title`.
`docx_question_level()` assumed every heading style looked like `Heading
N` and called `int(p.style.name.split(" ")[-1])`. For non-numbered
heading styles, that raises `ValueError` and breaks Manual, Q&A, and
Laws chunking.
This PR parses heading levels safely and falls back to level 1 for
Heading-prefixed styles without an explicit numeric suffix.
Closes #16163.
## Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] Test Case (non-breaking change which adds test coverage)
This commit is contained in:
@@ -14,30 +14,32 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
import importlib
|
||||||
import sys
|
import sys
|
||||||
import types
|
import types
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def _stub(name, **attrs):
|
def _stub(monkeypatch, name, **attrs):
|
||||||
mod = types.ModuleType(name)
|
mod = types.ModuleType(name)
|
||||||
for key, value in attrs.items():
|
for key, value in attrs.items():
|
||||||
setattr(mod, key, value)
|
setattr(mod, key, value)
|
||||||
sys.modules.setdefault(name, mod)
|
monkeypatch.setitem(sys.modules, name, mod)
|
||||||
return mod
|
return mod
|
||||||
|
|
||||||
|
|
||||||
# Stub heavy module-level imports so rag.nlp can be imported in isolation.
|
@pytest.fixture()
|
||||||
_stub("common.token_utils", num_tokens_from_string=lambda *a, **k: 0)
|
def docx_question_level(monkeypatch):
|
||||||
_stub("roman_numbers")
|
_stub(monkeypatch, "common.token_utils", num_tokens_from_string=lambda *a, **k: 0)
|
||||||
_stub("word2number", w2n=types.SimpleNamespace())
|
_stub(monkeypatch, "roman_numbers")
|
||||||
_stub("cn2an", cn2an=lambda *a, **k: 0)
|
_stub(monkeypatch, "word2number", w2n=types.SimpleNamespace())
|
||||||
_pil = _stub("PIL")
|
_stub(monkeypatch, "cn2an", cn2an=lambda *a, **k: 0)
|
||||||
_pil.Image = _stub("PIL.Image")
|
pil = _stub(monkeypatch, "PIL")
|
||||||
_stub("chardet")
|
pil.Image = _stub(monkeypatch, "PIL.Image")
|
||||||
|
_stub(monkeypatch, "chardet")
|
||||||
from rag.nlp import docx_question_level
|
monkeypatch.delitem(sys.modules, "rag.nlp", raising=False)
|
||||||
|
return importlib.import_module("rag.nlp").docx_question_level
|
||||||
|
|
||||||
|
|
||||||
class _Style:
|
class _Style:
|
||||||
@@ -65,14 +67,14 @@ class _Paragraph:
|
|||||||
("Heading Title", 1), # custom prefix with space, no number -> top level
|
("Heading Title", 1), # custom prefix with space, no number -> top level
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_docx_question_level_heading_styles(style_name, expected_level):
|
def test_docx_question_level_heading_styles(docx_question_level, style_name, expected_level):
|
||||||
level, text = docx_question_level(_Paragraph(style_name))
|
level, text = docx_question_level(_Paragraph(style_name))
|
||||||
assert level == expected_level
|
assert level == expected_level
|
||||||
assert text == "Some title"
|
assert text == "Some title"
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.p2
|
@pytest.mark.p2
|
||||||
def test_docx_question_level_no_number_does_not_raise():
|
def test_docx_question_level_no_number_does_not_raise(docx_question_level):
|
||||||
# Regression for #16163: a "Heading"-prefixed style without a parseable
|
# Regression for #16163: a "Heading"-prefixed style without a parseable
|
||||||
# number used to raise ValueError: invalid literal for int().
|
# number used to raise ValueError: invalid literal for int().
|
||||||
for name in ("Heading", "HeadingTitle", "Heading Title"):
|
for name in ("Heading", "HeadingTitle", "Heading Title"):
|
||||||
@@ -81,7 +83,7 @@ def test_docx_question_level_no_number_does_not_raise():
|
|||||||
|
|
||||||
|
|
||||||
@pytest.mark.p2
|
@pytest.mark.p2
|
||||||
def test_docx_question_level_non_heading_default_bull():
|
def test_docx_question_level_non_heading_default_bull(docx_question_level):
|
||||||
# Non-heading paragraph with the default bull=-1 returns level 0 (body text).
|
# Non-heading paragraph with the default bull=-1 returns level 0 (body text).
|
||||||
level, text = docx_question_level(_Paragraph("Normal", text="just a body line"))
|
level, text = docx_question_level(_Paragraph("Normal", text="just a body line"))
|
||||||
assert level == 0
|
assert level == 0
|
||||||
|
|||||||
Reference in New Issue
Block a user