ocr update

This commit is contained in:
Pavel committed 2026-08-27 23:46:12 +04:00
1 parent d7a7d4d084
commit d4e92be0ab
27 files changed
+1656 -134

No files matched your search

+112
View File
@@ -628,3 +628,115 @@ class TestParserEngineSwitch:
assert ".xhtml" in extractor
for suffix in (".pdf", ".docx", ".csv", ".xlsx", ".html", ".xhtml", ".pptx"):
extractor[suffix].init_parser()
# =====================================================================
# Gained formats (anydoc-only: legacy/macro Office, OpenDocument, RTF)
# =====================================================================
@pytest.mark.unit
class TestGainedFormats:
def test_every_supported_extension_has_a_parser_under_both_engines(self):
"""The invariant that keeps constants.py and the parser maps in lockstep.
Without it, an extension accepted at upload but missing from the map
falls to SimpleDirectoryReader's plain-text read — for a binary
format that means mojibake silently ingested and embedded.
(`.txt` is the one deliberate plain-text read.)
"""
pytest.importorskip("anydoc")
from application.parser.file.bulk import get_default_file_extractor
from application.parser.file.constants import (
SUPPORTED_SOURCE_DOCUMENT_EXTENSIONS,
)
for engine in ("anydoc", "docling"):
extractor = get_default_file_extractor(engine=engine)
missing = [
suffix
for suffix in SUPPORTED_SOURCE_DOCUMENT_EXTENSIONS
if suffix != ".txt" and suffix not in extractor
]
assert missing == [], (engine, missing)
def test_gained_formats_map_to_anydoc_under_both_engines(self):
pytest.importorskip("anydoc")
from application.parser.file.anydoc_parser import (
ANYDOC_GAINED_SUFFIXES,
AnydocParser,
)
from application.parser.file.bulk import get_default_file_extractor
for engine in ("anydoc", "docling"):
extractor = get_default_file_extractor(engine=engine)
for suffix in ANYDOC_GAINED_SUFFIXES:
assert isinstance(extractor[suffix], AnydocParser), (engine, suffix)
def test_gained_formats_never_fall_back_to_anydoc_itself(self):
pytest.importorskip("anydoc")
from application.parser.file.anydoc_parser import AnydocParser
from application.parser.file.bulk import get_default_file_extractor
extractor = get_default_file_extractor(engine="anydoc")
assert extractor[".doc"].fallback_parser is None
assert extractor[".rtf"].fallback_parser is None
# ...while the core five keep their real fallback.
assert extractor[".pdf"].fallback_parser is not None
assert not isinstance(extractor[".pdf"].fallback_parser, AnydocParser)
def test_gained_entries_absent_without_anydoc(self, monkeypatch):
import sys
from application.parser.file.bulk import get_default_file_extractor
monkeypatch.setitem(sys.modules, "anydoc", None)
extractor = get_default_file_extractor(engine="docling")
assert ".doc" not in extractor # degrades to the pre-anydoc map
def test_rtf_converts_end_to_end(self, tmp_path):
pytest.importorskip("anydoc")
from application.parser.file.bulk import get_default_file_extractor
path = tmp_path / "note.rtf"
path.write_text(r"{\rtf1\ansi Hello {\b bold} world.\par Second paragraph.}")
parser = get_default_file_extractor()[".rtf"]
parser.init_parser()
out = parser.parse_file(path)
assert "Hello **bold** world." in out
assert "Second paragraph." in out
def test_odt_converts_end_to_end(self, tmp_path):
pytest.importorskip("anydoc")
import zipfile
from application.parser.file.bulk import get_default_file_extractor
path = tmp_path / "doc.odt"
with zipfile.ZipFile(path, "w") as z:
z.writestr(
"mimetype",
"application/vnd.oasis.opendocument.text",
compress_type=zipfile.ZIP_STORED,
)
z.writestr(
"META-INF/manifest.xml",
'<?xml version="1.0"?><manifest:manifest xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0">'
'<manifest:file-entry manifest:full-path="/" manifest:media-type="application/vnd.oasis.opendocument.text"/>'
'<manifest:file-entry manifest:full-path="content.xml" manifest:media-type="text/xml"/></manifest:manifest>',
)
z.writestr(
"content.xml",
'<?xml version="1.0"?><office:document-content xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0" '
'xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0"><office:body><office:text>'
'<text:h text:outline-level="1">Title</text:h><text:p>Body text here.</text:p>'
"</office:text></office:body></office:document-content>",
)
parser = get_default_file_extractor()[".odt"]
out = parser.parse_file(path)
assert "# Title" in out
assert "Body text here." in out