kreuzberg 4.0.7 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +24 -16
- data/README.md +4 -1
- data/ext/kreuzberg_rb/native/Cargo.lock +94 -98
- data/ext/kreuzberg_rb/native/Cargo.toml +4 -2
- data/ext/kreuzberg_rb/native/src/batch.rs +139 -0
- data/ext/kreuzberg_rb/native/src/config/mod.rs +10 -0
- data/ext/kreuzberg_rb/native/src/config/types.rs +1058 -0
- data/ext/kreuzberg_rb/native/src/error_handling.rs +125 -0
- data/ext/kreuzberg_rb/native/src/extraction.rs +79 -0
- data/ext/kreuzberg_rb/native/src/gc_guarded_value.rs +35 -0
- data/ext/kreuzberg_rb/native/src/helpers.rs +176 -0
- data/ext/kreuzberg_rb/native/src/lib.rs +342 -3622
- data/ext/kreuzberg_rb/native/src/metadata.rs +34 -0
- data/ext/kreuzberg_rb/native/src/plugins/mod.rs +92 -0
- data/ext/kreuzberg_rb/native/src/plugins/ocr_backend.rs +159 -0
- data/ext/kreuzberg_rb/native/src/plugins/post_processor.rs +126 -0
- data/ext/kreuzberg_rb/native/src/plugins/validator.rs +99 -0
- data/ext/kreuzberg_rb/native/src/result.rs +326 -0
- data/ext/kreuzberg_rb/native/src/validation.rs +4 -0
- data/lib/kreuzberg/config.rb +66 -0
- data/lib/kreuzberg/result.rb +107 -2
- data/lib/kreuzberg/types.rb +104 -0
- data/lib/kreuzberg/version.rb +1 -1
- data/lib/kreuzberg.rb +0 -4
- data/sig/kreuzberg.rbs +105 -1
- data/vendor/Cargo.toml +3 -3
- data/vendor/kreuzberg/Cargo.toml +4 -3
- data/vendor/kreuzberg/README.md +1 -1
- data/vendor/kreuzberg/src/api/config.rs +69 -0
- data/vendor/kreuzberg/src/api/handlers.rs +99 -2
- data/vendor/kreuzberg/src/api/mod.rs +14 -7
- data/vendor/kreuzberg/src/api/router.rs +214 -0
- data/vendor/kreuzberg/src/api/startup.rs +243 -0
- data/vendor/kreuzberg/src/api/types.rs +78 -0
- data/vendor/kreuzberg/src/cache/cleanup.rs +277 -0
- data/vendor/kreuzberg/src/cache/core.rs +428 -0
- data/vendor/kreuzberg/src/cache/mod.rs +21 -843
- data/vendor/kreuzberg/src/cache/utilities.rs +156 -0
- data/vendor/kreuzberg/src/chunking/boundaries.rs +301 -0
- data/vendor/kreuzberg/src/chunking/builder.rs +294 -0
- data/vendor/kreuzberg/src/chunking/config.rs +52 -0
- data/vendor/kreuzberg/src/chunking/core.rs +1017 -0
- data/vendor/kreuzberg/src/chunking/mod.rs +14 -2211
- data/vendor/kreuzberg/src/chunking/processor.rs +10 -0
- data/vendor/kreuzberg/src/chunking/validation.rs +686 -0
- data/vendor/kreuzberg/src/core/config/extraction/core.rs +169 -0
- data/vendor/kreuzberg/src/core/config/extraction/env.rs +179 -0
- data/vendor/kreuzberg/src/core/config/extraction/loaders.rs +204 -0
- data/vendor/kreuzberg/src/core/config/extraction/mod.rs +42 -0
- data/vendor/kreuzberg/src/core/config/extraction/types.rs +93 -0
- data/vendor/kreuzberg/src/core/config/formats.rs +135 -0
- data/vendor/kreuzberg/src/core/config/mod.rs +20 -0
- data/vendor/kreuzberg/src/core/config/ocr.rs +73 -0
- data/vendor/kreuzberg/src/core/config/page.rs +57 -0
- data/vendor/kreuzberg/src/core/config/pdf.rs +111 -0
- data/vendor/kreuzberg/src/core/config/processing.rs +312 -0
- data/vendor/kreuzberg/src/core/config_validation/dependencies.rs +187 -0
- data/vendor/kreuzberg/src/core/config_validation/mod.rs +386 -0
- data/vendor/kreuzberg/src/core/config_validation/sections.rs +401 -0
- data/vendor/kreuzberg/src/core/extractor/batch.rs +246 -0
- data/vendor/kreuzberg/src/core/extractor/bytes.rs +116 -0
- data/vendor/kreuzberg/src/core/extractor/file.rs +240 -0
- data/vendor/kreuzberg/src/core/extractor/helpers.rs +71 -0
- data/vendor/kreuzberg/src/core/extractor/legacy.rs +62 -0
- data/vendor/kreuzberg/src/core/extractor/mod.rs +490 -0
- data/vendor/kreuzberg/src/core/extractor/sync.rs +208 -0
- data/vendor/kreuzberg/src/core/mod.rs +4 -1
- data/vendor/kreuzberg/src/core/pipeline/cache.rs +60 -0
- data/vendor/kreuzberg/src/core/pipeline/execution.rs +89 -0
- data/vendor/kreuzberg/src/core/pipeline/features.rs +108 -0
- data/vendor/kreuzberg/src/core/pipeline/format.rs +392 -0
- data/vendor/kreuzberg/src/core/pipeline/initialization.rs +67 -0
- data/vendor/kreuzberg/src/core/pipeline/mod.rs +135 -0
- data/vendor/kreuzberg/src/core/pipeline/tests.rs +975 -0
- data/vendor/kreuzberg/src/core/server_config/env.rs +90 -0
- data/vendor/kreuzberg/src/core/server_config/loader.rs +202 -0
- data/vendor/kreuzberg/src/core/server_config/mod.rs +380 -0
- data/vendor/kreuzberg/src/core/server_config/tests/basic_tests.rs +124 -0
- data/vendor/kreuzberg/src/core/server_config/tests/env_tests.rs +216 -0
- data/vendor/kreuzberg/src/core/server_config/tests/file_loading_tests.rs +341 -0
- data/vendor/kreuzberg/src/core/server_config/tests/mod.rs +5 -0
- data/vendor/kreuzberg/src/core/server_config/validation.rs +17 -0
- data/vendor/kreuzberg/src/embeddings.rs +136 -13
- data/vendor/kreuzberg/src/extraction/{archive.rs → archive/mod.rs} +45 -239
- data/vendor/kreuzberg/src/extraction/archive/sevenz.rs +98 -0
- data/vendor/kreuzberg/src/extraction/archive/tar.rs +118 -0
- data/vendor/kreuzberg/src/extraction/archive/zip.rs +101 -0
- data/vendor/kreuzberg/src/extraction/html/converter.rs +592 -0
- data/vendor/kreuzberg/src/extraction/html/image_handling.rs +95 -0
- data/vendor/kreuzberg/src/extraction/html/mod.rs +53 -0
- data/vendor/kreuzberg/src/extraction/html/processor.rs +659 -0
- data/vendor/kreuzberg/src/extraction/html/stack_management.rs +103 -0
- data/vendor/kreuzberg/src/extraction/html/types.rs +28 -0
- data/vendor/kreuzberg/src/extraction/mod.rs +6 -2
- data/vendor/kreuzberg/src/extraction/pptx/container.rs +159 -0
- data/vendor/kreuzberg/src/extraction/pptx/content_builder.rs +168 -0
- data/vendor/kreuzberg/src/extraction/pptx/elements.rs +132 -0
- data/vendor/kreuzberg/src/extraction/pptx/image_handling.rs +57 -0
- data/vendor/kreuzberg/src/extraction/pptx/metadata.rs +160 -0
- data/vendor/kreuzberg/src/extraction/pptx/mod.rs +558 -0
- data/vendor/kreuzberg/src/extraction/pptx/parser.rs +379 -0
- data/vendor/kreuzberg/src/extraction/transform/content.rs +205 -0
- data/vendor/kreuzberg/src/extraction/transform/elements.rs +211 -0
- data/vendor/kreuzberg/src/extraction/transform/mod.rs +480 -0
- data/vendor/kreuzberg/src/extraction/transform/types.rs +27 -0
- data/vendor/kreuzberg/src/extractors/archive.rs +2 -0
- data/vendor/kreuzberg/src/extractors/bibtex.rs +2 -0
- data/vendor/kreuzberg/src/extractors/djot_format/attributes.rs +134 -0
- data/vendor/kreuzberg/src/extractors/djot_format/conversion.rs +223 -0
- data/vendor/kreuzberg/src/extractors/djot_format/extractor.rs +172 -0
- data/vendor/kreuzberg/src/extractors/djot_format/mod.rs +24 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/block_handlers.rs +271 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/content_extraction.rs +257 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/event_handlers.rs +101 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/inline_handlers.rs +201 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/mod.rs +16 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/state.rs +78 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/table_extraction.rs +68 -0
- data/vendor/kreuzberg/src/extractors/djot_format/parsing/text_extraction.rs +61 -0
- data/vendor/kreuzberg/src/extractors/djot_format/rendering.rs +452 -0
- data/vendor/kreuzberg/src/extractors/docbook.rs +2 -0
- data/vendor/kreuzberg/src/extractors/docx.rs +12 -1
- data/vendor/kreuzberg/src/extractors/email.rs +2 -0
- data/vendor/kreuzberg/src/extractors/epub/content.rs +333 -0
- data/vendor/kreuzberg/src/extractors/epub/metadata.rs +137 -0
- data/vendor/kreuzberg/src/extractors/epub/mod.rs +186 -0
- data/vendor/kreuzberg/src/extractors/epub/parsing.rs +86 -0
- data/vendor/kreuzberg/src/extractors/excel.rs +4 -0
- data/vendor/kreuzberg/src/extractors/fictionbook.rs +2 -0
- data/vendor/kreuzberg/src/extractors/frontmatter_utils.rs +466 -0
- data/vendor/kreuzberg/src/extractors/html.rs +80 -8
- data/vendor/kreuzberg/src/extractors/image.rs +8 -1
- data/vendor/kreuzberg/src/extractors/jats/elements.rs +350 -0
- data/vendor/kreuzberg/src/extractors/jats/metadata.rs +21 -0
- data/vendor/kreuzberg/src/extractors/{jats.rs → jats/mod.rs} +10 -412
- data/vendor/kreuzberg/src/extractors/jats/parser.rs +52 -0
- data/vendor/kreuzberg/src/extractors/jupyter.rs +2 -0
- data/vendor/kreuzberg/src/extractors/latex/commands.rs +93 -0
- data/vendor/kreuzberg/src/extractors/latex/environments.rs +157 -0
- data/vendor/kreuzberg/src/extractors/latex/metadata.rs +27 -0
- data/vendor/kreuzberg/src/extractors/latex/mod.rs +146 -0
- data/vendor/kreuzberg/src/extractors/latex/parser.rs +231 -0
- data/vendor/kreuzberg/src/extractors/latex/utilities.rs +126 -0
- data/vendor/kreuzberg/src/extractors/markdown.rs +39 -162
- data/vendor/kreuzberg/src/extractors/mod.rs +9 -1
- data/vendor/kreuzberg/src/extractors/odt.rs +2 -0
- data/vendor/kreuzberg/src/extractors/opml/core.rs +165 -0
- data/vendor/kreuzberg/src/extractors/opml/mod.rs +31 -0
- data/vendor/kreuzberg/src/extractors/opml/parser.rs +479 -0
- data/vendor/kreuzberg/src/extractors/orgmode.rs +2 -0
- data/vendor/kreuzberg/src/extractors/pdf/extraction.rs +106 -0
- data/vendor/kreuzberg/src/extractors/{pdf.rs → pdf/mod.rs} +25 -324
- data/vendor/kreuzberg/src/extractors/pdf/ocr.rs +214 -0
- data/vendor/kreuzberg/src/extractors/pdf/pages.rs +51 -0
- data/vendor/kreuzberg/src/extractors/pptx.rs +9 -2
- data/vendor/kreuzberg/src/extractors/rst.rs +2 -0
- data/vendor/kreuzberg/src/extractors/rtf/encoding.rs +116 -0
- data/vendor/kreuzberg/src/extractors/rtf/formatting.rs +24 -0
- data/vendor/kreuzberg/src/extractors/rtf/images.rs +72 -0
- data/vendor/kreuzberg/src/extractors/rtf/metadata.rs +216 -0
- data/vendor/kreuzberg/src/extractors/rtf/mod.rs +142 -0
- data/vendor/kreuzberg/src/extractors/rtf/parser.rs +259 -0
- data/vendor/kreuzberg/src/extractors/rtf/tables.rs +83 -0
- data/vendor/kreuzberg/src/extractors/structured.rs +2 -0
- data/vendor/kreuzberg/src/extractors/text.rs +4 -0
- data/vendor/kreuzberg/src/extractors/typst.rs +2 -0
- data/vendor/kreuzberg/src/extractors/xml.rs +2 -0
- data/vendor/kreuzberg/src/keywords/processor.rs +14 -0
- data/vendor/kreuzberg/src/language_detection/processor.rs +10 -0
- data/vendor/kreuzberg/src/lib.rs +2 -2
- data/vendor/kreuzberg/src/mcp/errors.rs +312 -0
- data/vendor/kreuzberg/src/mcp/format.rs +211 -0
- data/vendor/kreuzberg/src/mcp/mod.rs +9 -3
- data/vendor/kreuzberg/src/mcp/params.rs +196 -0
- data/vendor/kreuzberg/src/mcp/server.rs +39 -1438
- data/vendor/kreuzberg/src/mcp/tools/cache.rs +179 -0
- data/vendor/kreuzberg/src/mcp/tools/extraction.rs +403 -0
- data/vendor/kreuzberg/src/mcp/tools/mime.rs +150 -0
- data/vendor/kreuzberg/src/mcp/tools/mod.rs +11 -0
- data/vendor/kreuzberg/src/ocr/backends/easyocr.rs +96 -0
- data/vendor/kreuzberg/src/ocr/backends/mod.rs +7 -0
- data/vendor/kreuzberg/src/ocr/backends/paddleocr.rs +27 -0
- data/vendor/kreuzberg/src/ocr/backends/tesseract.rs +134 -0
- data/vendor/kreuzberg/src/ocr/hocr.rs +60 -16
- data/vendor/kreuzberg/src/ocr/language_registry.rs +11 -235
- data/vendor/kreuzberg/src/ocr/mod.rs +1 -0
- data/vendor/kreuzberg/src/ocr/processor/config.rs +203 -0
- data/vendor/kreuzberg/src/ocr/processor/execution.rs +494 -0
- data/vendor/kreuzberg/src/ocr/processor/mod.rs +265 -0
- data/vendor/kreuzberg/src/ocr/processor/validation.rs +145 -0
- data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +41 -24
- data/vendor/kreuzberg/src/pdf/bindings.rs +21 -8
- data/vendor/kreuzberg/src/pdf/hierarchy/bounding_box.rs +289 -0
- data/vendor/kreuzberg/src/pdf/hierarchy/clustering.rs +199 -0
- data/vendor/kreuzberg/src/pdf/{hierarchy.rs → hierarchy/extraction.rs} +6 -346
- data/vendor/kreuzberg/src/pdf/hierarchy/mod.rs +18 -0
- data/vendor/kreuzberg/src/plugins/extractor/mod.rs +319 -0
- data/vendor/kreuzberg/src/plugins/extractor/registry.rs +434 -0
- data/vendor/kreuzberg/src/plugins/extractor/trait.rs +391 -0
- data/vendor/kreuzberg/src/plugins/mod.rs +13 -0
- data/vendor/kreuzberg/src/plugins/ocr.rs +11 -0
- data/vendor/kreuzberg/src/plugins/processor/mod.rs +365 -0
- data/vendor/kreuzberg/src/plugins/processor/registry.rs +37 -0
- data/vendor/kreuzberg/src/plugins/processor/trait.rs +284 -0
- data/vendor/kreuzberg/src/plugins/registry/extractor.rs +416 -0
- data/vendor/kreuzberg/src/plugins/registry/mod.rs +116 -0
- data/vendor/kreuzberg/src/plugins/registry/ocr.rs +293 -0
- data/vendor/kreuzberg/src/plugins/registry/processor.rs +304 -0
- data/vendor/kreuzberg/src/plugins/registry/validator.rs +238 -0
- data/vendor/kreuzberg/src/plugins/validator/mod.rs +424 -0
- data/vendor/kreuzberg/src/plugins/validator/registry.rs +355 -0
- data/vendor/kreuzberg/src/plugins/validator/trait.rs +276 -0
- data/vendor/kreuzberg/src/stopwords/languages/asian.rs +40 -0
- data/vendor/kreuzberg/src/stopwords/languages/germanic.rs +36 -0
- data/vendor/kreuzberg/src/stopwords/languages/mod.rs +10 -0
- data/vendor/kreuzberg/src/stopwords/languages/other.rs +44 -0
- data/vendor/kreuzberg/src/stopwords/languages/romance.rs +36 -0
- data/vendor/kreuzberg/src/stopwords/languages/slavic.rs +36 -0
- data/vendor/kreuzberg/src/stopwords/mod.rs +7 -33
- data/vendor/kreuzberg/src/text/quality.rs +1 -1
- data/vendor/kreuzberg/src/text/quality_processor.rs +10 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/analysis.rs +238 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/mod.rs +8 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/punctuation.rs +54 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/reducer.rs +384 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/sentence_selection.rs +68 -0
- data/vendor/kreuzberg/src/text/token_reduction/core/word_filtering.rs +156 -0
- data/vendor/kreuzberg/src/text/token_reduction/filters/general.rs +377 -0
- data/vendor/kreuzberg/src/text/token_reduction/filters/html.rs +51 -0
- data/vendor/kreuzberg/src/text/token_reduction/filters/markdown.rs +285 -0
- data/vendor/kreuzberg/src/text/token_reduction/filters.rs +131 -246
- data/vendor/kreuzberg/src/types/djot.rs +209 -0
- data/vendor/kreuzberg/src/types/extraction.rs +301 -0
- data/vendor/kreuzberg/src/types/formats.rs +443 -0
- data/vendor/kreuzberg/src/types/metadata.rs +560 -0
- data/vendor/kreuzberg/src/types/mod.rs +281 -0
- data/vendor/kreuzberg/src/types/page.rs +182 -0
- data/vendor/kreuzberg/src/types/serde_helpers.rs +132 -0
- data/vendor/kreuzberg/src/types/tables.rs +39 -0
- data/vendor/kreuzberg/src/utils/quality/heuristics.rs +58 -0
- data/vendor/kreuzberg/src/utils/{quality.rs → quality/mod.rs} +168 -489
- data/vendor/kreuzberg/src/utils/quality/patterns.rs +117 -0
- data/vendor/kreuzberg/src/utils/quality/scoring.rs +178 -0
- data/vendor/kreuzberg/src/utils/string_pool/buffer_pool.rs +325 -0
- data/vendor/kreuzberg/src/utils/string_pool/interned.rs +102 -0
- data/vendor/kreuzberg/src/utils/string_pool/language_pool.rs +119 -0
- data/vendor/kreuzberg/src/utils/string_pool/mime_pool.rs +235 -0
- data/vendor/kreuzberg/src/utils/string_pool/mod.rs +41 -0
- data/vendor/kreuzberg/tests/api_chunk.rs +313 -0
- data/vendor/kreuzberg/tests/api_embed.rs +6 -9
- data/vendor/kreuzberg/tests/batch_orchestration.rs +1 -0
- data/vendor/kreuzberg/tests/concurrency_stress.rs +7 -0
- data/vendor/kreuzberg/tests/core_integration.rs +1 -0
- data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +130 -0
- data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +5 -14
- data/vendor/kreuzberg/tests/format_integration.rs +2 -0
- data/vendor/kreuzberg/tests/helpers/mod.rs +1 -0
- data/vendor/kreuzberg/tests/html_table_test.rs +11 -11
- data/vendor/kreuzberg/tests/ocr_configuration.rs +16 -0
- data/vendor/kreuzberg/tests/ocr_errors.rs +18 -0
- data/vendor/kreuzberg/tests/ocr_quality.rs +9 -0
- data/vendor/kreuzberg/tests/ocr_stress.rs +1 -0
- data/vendor/kreuzberg/tests/pipeline_integration.rs +50 -0
- data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +13 -0
- data/vendor/kreuzberg/tests/plugin_system.rs +12 -0
- data/vendor/kreuzberg/tests/registry_integration_tests.rs +2 -0
- data/vendor/kreuzberg-ffi/Cargo.toml +2 -1
- data/vendor/kreuzberg-ffi/benches/result_view_benchmark.rs +2 -0
- data/vendor/kreuzberg-ffi/kreuzberg.h +347 -178
- data/vendor/kreuzberg-ffi/src/config/html.rs +318 -0
- data/vendor/kreuzberg-ffi/src/config/loader.rs +154 -0
- data/vendor/kreuzberg-ffi/src/config/merge.rs +104 -0
- data/vendor/kreuzberg-ffi/src/config/mod.rs +385 -0
- data/vendor/kreuzberg-ffi/src/config/parse.rs +91 -0
- data/vendor/kreuzberg-ffi/src/config/serialize.rs +118 -0
- data/vendor/kreuzberg-ffi/src/config_builder.rs +598 -0
- data/vendor/kreuzberg-ffi/src/error.rs +46 -14
- data/vendor/kreuzberg-ffi/src/helpers.rs +26 -353
- data/vendor/kreuzberg-ffi/src/html_options.rs +421 -0
- data/vendor/kreuzberg-ffi/src/lib.rs +16 -0
- data/vendor/kreuzberg-ffi/src/panic_shield.rs +11 -0
- data/vendor/kreuzberg-ffi/src/plugins/ocr_backend.rs +2 -0
- data/vendor/kreuzberg-ffi/src/result.rs +148 -122
- data/vendor/kreuzberg-ffi/src/result_view.rs +4 -0
- data/vendor/kreuzberg-tesseract/Cargo.toml +3 -3
- metadata +200 -28
- data/vendor/kreuzberg/src/api/server.rs +0 -518
- data/vendor/kreuzberg/src/core/config.rs +0 -1914
- data/vendor/kreuzberg/src/core/config_validation.rs +0 -949
- data/vendor/kreuzberg/src/core/extractor.rs +0 -1200
- data/vendor/kreuzberg/src/core/pipeline.rs +0 -1223
- data/vendor/kreuzberg/src/core/server_config.rs +0 -1220
- data/vendor/kreuzberg/src/extraction/html.rs +0 -1830
- data/vendor/kreuzberg/src/extraction/pptx.rs +0 -3102
- data/vendor/kreuzberg/src/extractors/epub.rs +0 -696
- data/vendor/kreuzberg/src/extractors/latex.rs +0 -653
- data/vendor/kreuzberg/src/extractors/opml.rs +0 -635
- data/vendor/kreuzberg/src/extractors/rtf.rs +0 -809
- data/vendor/kreuzberg/src/ocr/processor.rs +0 -858
- data/vendor/kreuzberg/src/plugins/extractor.rs +0 -1042
- data/vendor/kreuzberg/src/plugins/processor.rs +0 -650
- data/vendor/kreuzberg/src/plugins/registry.rs +0 -1339
- data/vendor/kreuzberg/src/plugins/validator.rs +0 -967
- data/vendor/kreuzberg/src/text/token_reduction/core.rs +0 -832
- data/vendor/kreuzberg/src/types.rs +0 -1713
- data/vendor/kreuzberg/src/utils/string_pool.rs +0 -762
- data/vendor/kreuzberg-ffi/src/config.rs +0 -1341
|
@@ -1,858 +0,0 @@
|
|
|
1
|
-
use std::collections::HashMap;
|
|
2
|
-
use std::env;
|
|
3
|
-
use std::path::Path;
|
|
4
|
-
use std::time::{SystemTime, UNIX_EPOCH};
|
|
5
|
-
|
|
6
|
-
use kreuzberg_tesseract::{TessPageSegMode, TesseractAPI};
|
|
7
|
-
|
|
8
|
-
use super::cache::OcrCache;
|
|
9
|
-
use super::error::OcrError;
|
|
10
|
-
use super::hocr::convert_hocr_to_markdown;
|
|
11
|
-
use super::table::{extract_words_from_tsv, reconstruct_table, table_to_markdown};
|
|
12
|
-
use super::types::{BatchItemResult, TesseractConfig};
|
|
13
|
-
use crate::types::{OcrExtractionResult, OcrTable};
|
|
14
|
-
|
|
15
|
-
fn strip_control_characters(text: &str) -> String {
|
|
16
|
-
if text
|
|
17
|
-
.chars()
|
|
18
|
-
.any(|c| matches!(c, '\u{0000}'..='\u{001F}' | '\u{007F}') && c != '\n' && c != '\r' && c != '\t')
|
|
19
|
-
{
|
|
20
|
-
text.chars()
|
|
21
|
-
.filter(|c| !matches!(c, '\u{0000}'..='\u{001F}' | '\u{007F}') || matches!(c, '\n' | '\r' | '\t'))
|
|
22
|
-
.collect()
|
|
23
|
-
} else {
|
|
24
|
-
text.to_string()
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
fn log_ci_debug<F>(enabled: bool, stage: &str, details: F)
|
|
29
|
-
where
|
|
30
|
-
F: FnOnce() -> String,
|
|
31
|
-
{
|
|
32
|
-
if !enabled {
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
let timestamp = SystemTime::now()
|
|
37
|
-
.duration_since(UNIX_EPOCH)
|
|
38
|
-
.map(|d| d.as_secs_f64())
|
|
39
|
-
.unwrap_or(0.0);
|
|
40
|
-
|
|
41
|
-
tracing::debug!("[ci-debug][ocr::processor::{stage}] {timestamp:.3}s {}", details());
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
pub struct OcrProcessor {
|
|
45
|
-
cache: OcrCache,
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
impl OcrProcessor {
|
|
49
|
-
pub fn new(cache_dir: Option<std::path::PathBuf>) -> Result<Self, OcrError> {
|
|
50
|
-
let cache = OcrCache::new(cache_dir)?;
|
|
51
|
-
Ok(Self { cache })
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
#[cfg_attr(feature = "otel", tracing::instrument(
|
|
55
|
-
skip(self, image_bytes),
|
|
56
|
-
fields(
|
|
57
|
-
ocr.backend = "tesseract",
|
|
58
|
-
ocr.language = %config.language,
|
|
59
|
-
image.size_bytes = image_bytes.len(),
|
|
60
|
-
)
|
|
61
|
-
))]
|
|
62
|
-
pub fn process_image(&self, image_bytes: &[u8], config: &TesseractConfig) -> Result<OcrExtractionResult, OcrError> {
|
|
63
|
-
config.validate().map_err(OcrError::InvalidConfiguration)?;
|
|
64
|
-
|
|
65
|
-
let mut hasher = ahash::AHasher::default();
|
|
66
|
-
use std::hash::{Hash, Hasher};
|
|
67
|
-
image_bytes.hash(&mut hasher);
|
|
68
|
-
let image_hash = format!("{:016x}", hasher.finish());
|
|
69
|
-
|
|
70
|
-
let config_str = self.hash_config(config);
|
|
71
|
-
|
|
72
|
-
if config.use_cache
|
|
73
|
-
&& let Some(cached_result) = self.cache.get_cached_result(&image_hash, "tesseract", &config_str)?
|
|
74
|
-
{
|
|
75
|
-
#[cfg(feature = "otel")]
|
|
76
|
-
tracing::Span::current().record("cache.hit", true);
|
|
77
|
-
return Ok(cached_result);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
#[cfg(feature = "otel")]
|
|
81
|
-
tracing::Span::current().record("cache.hit", false);
|
|
82
|
-
|
|
83
|
-
let result = self.perform_ocr(image_bytes, config)?;
|
|
84
|
-
|
|
85
|
-
if config.use_cache {
|
|
86
|
-
let _ = self
|
|
87
|
-
.cache
|
|
88
|
-
.set_cached_result(&image_hash, "tesseract", &config_str, &result);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
Ok(result)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
pub fn clear_cache(&self) -> Result<(), OcrError> {
|
|
95
|
-
self.cache.clear()
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
pub fn get_cache_stats(&self) -> Result<super::cache::OcrCacheStats, OcrError> {
|
|
99
|
-
self.cache.get_stats()
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
pub fn process_file(&self, file_path: &str, config: &TesseractConfig) -> Result<OcrExtractionResult, OcrError> {
|
|
103
|
-
let image_bytes = std::fs::read(file_path)
|
|
104
|
-
.map_err(|e| OcrError::IOError(format!("Failed to read file '{}': {}", file_path, e)))?;
|
|
105
|
-
self.process_image(&image_bytes, config)
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/// Process multiple image files in parallel using Rayon.
|
|
109
|
-
///
|
|
110
|
-
/// This method processes OCR operations in parallel across CPU cores for improved throughput.
|
|
111
|
-
/// Results are returned in the same order as the input file paths.
|
|
112
|
-
pub fn process_files_batch(&self, file_paths: Vec<String>, config: &TesseractConfig) -> Vec<BatchItemResult> {
|
|
113
|
-
use rayon::prelude::*;
|
|
114
|
-
|
|
115
|
-
file_paths
|
|
116
|
-
.par_iter()
|
|
117
|
-
.map(|path| match self.process_file(path, config) {
|
|
118
|
-
Ok(result) => BatchItemResult {
|
|
119
|
-
file_path: path.clone(),
|
|
120
|
-
success: true,
|
|
121
|
-
result: Some(result),
|
|
122
|
-
error: None,
|
|
123
|
-
},
|
|
124
|
-
Err(e) => BatchItemResult {
|
|
125
|
-
file_path: path.clone(),
|
|
126
|
-
success: false,
|
|
127
|
-
result: None,
|
|
128
|
-
error: Some(e.to_string()),
|
|
129
|
-
},
|
|
130
|
-
})
|
|
131
|
-
.collect()
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
fn hash_config(&self, config: &TesseractConfig) -> String {
|
|
135
|
-
use ahash::AHasher;
|
|
136
|
-
use std::hash::{Hash, Hasher};
|
|
137
|
-
|
|
138
|
-
let mut hasher = AHasher::default();
|
|
139
|
-
config.language.hash(&mut hasher);
|
|
140
|
-
config.psm.hash(&mut hasher);
|
|
141
|
-
config.output_format.hash(&mut hasher);
|
|
142
|
-
config.enable_table_detection.hash(&mut hasher);
|
|
143
|
-
config.table_min_confidence.to_bits().hash(&mut hasher);
|
|
144
|
-
config.table_column_threshold.hash(&mut hasher);
|
|
145
|
-
config.table_row_threshold_ratio.to_bits().hash(&mut hasher);
|
|
146
|
-
config.classify_use_pre_adapted_templates.hash(&mut hasher);
|
|
147
|
-
config.language_model_ngram_on.hash(&mut hasher);
|
|
148
|
-
config.tessedit_dont_blkrej_good_wds.hash(&mut hasher);
|
|
149
|
-
config.tessedit_dont_rowrej_good_wds.hash(&mut hasher);
|
|
150
|
-
config.tessedit_enable_dict_correction.hash(&mut hasher);
|
|
151
|
-
config.tessedit_char_whitelist.hash(&mut hasher);
|
|
152
|
-
config.tessedit_use_primary_params_model.hash(&mut hasher);
|
|
153
|
-
config.textord_space_size_is_variable.hash(&mut hasher);
|
|
154
|
-
config.thresholding_method.hash(&mut hasher);
|
|
155
|
-
|
|
156
|
-
format!("{:016x}", hasher.finish())
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
fn perform_ocr(&self, image_bytes: &[u8], config: &TesseractConfig) -> Result<OcrExtractionResult, OcrError> {
|
|
160
|
-
let ci_debug_enabled = env::var_os("KREUZBERG_CI_DEBUG").is_some();
|
|
161
|
-
log_ci_debug(ci_debug_enabled, "perform_ocr:start", || {
|
|
162
|
-
format!(
|
|
163
|
-
"bytes={} language={} output={} use_cache={}",
|
|
164
|
-
image_bytes.len(),
|
|
165
|
-
config.language,
|
|
166
|
-
config.output_format,
|
|
167
|
-
config.use_cache
|
|
168
|
-
)
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
let img = image::load_from_memory(image_bytes)
|
|
172
|
-
.map_err(|e| OcrError::ImageProcessingFailed(format!("Failed to decode image: {}", e)))?;
|
|
173
|
-
|
|
174
|
-
let rgb_image = img.to_rgb8();
|
|
175
|
-
let (width, height) = rgb_image.dimensions();
|
|
176
|
-
let bytes_per_pixel = 3;
|
|
177
|
-
let bytes_per_line = width * bytes_per_pixel;
|
|
178
|
-
|
|
179
|
-
log_ci_debug(ci_debug_enabled, "image", || {
|
|
180
|
-
format!(
|
|
181
|
-
"dimensions={}x{} bytes_per_line={} color_type=RGB8",
|
|
182
|
-
width, height, bytes_per_line
|
|
183
|
-
)
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
let api = TesseractAPI::new();
|
|
187
|
-
|
|
188
|
-
let tessdata_env = env::var("TESSDATA_PREFIX").ok();
|
|
189
|
-
let fallback_paths = [
|
|
190
|
-
"/opt/homebrew/share/tessdata",
|
|
191
|
-
"/opt/homebrew/opt/tesseract/share/tessdata",
|
|
192
|
-
"/usr/local/opt/tesseract/share/tessdata",
|
|
193
|
-
"/usr/share/tesseract-ocr/5/tessdata",
|
|
194
|
-
"/usr/share/tesseract-ocr/4/tessdata",
|
|
195
|
-
"/usr/share/tessdata",
|
|
196
|
-
"/usr/local/share/tessdata",
|
|
197
|
-
r#"C:\Program Files\Tesseract-OCR\tessdata"#,
|
|
198
|
-
r#"C:\ProgramData\Tesseract-OCR\tessdata"#,
|
|
199
|
-
];
|
|
200
|
-
let tessdata_path = tessdata_env
|
|
201
|
-
.clone()
|
|
202
|
-
.or_else(|| {
|
|
203
|
-
fallback_paths
|
|
204
|
-
.iter()
|
|
205
|
-
.find(|p| Path::new(p).exists())
|
|
206
|
-
.map(|p| (*p).to_string())
|
|
207
|
-
})
|
|
208
|
-
.unwrap_or_default();
|
|
209
|
-
|
|
210
|
-
log_ci_debug(ci_debug_enabled, "tessdata", || {
|
|
211
|
-
let path_preview = env::var_os("PATH").map(|paths| {
|
|
212
|
-
env::split_paths(&paths)
|
|
213
|
-
.take(6)
|
|
214
|
-
.map(|p| p.display().to_string())
|
|
215
|
-
.collect::<Vec<_>>()
|
|
216
|
-
.join(", ")
|
|
217
|
-
});
|
|
218
|
-
let resolved_exists = !tessdata_path.is_empty() && Path::new(&tessdata_path).exists();
|
|
219
|
-
let available_fallbacks = fallback_paths
|
|
220
|
-
.iter()
|
|
221
|
-
.filter(|p| Path::new(p).exists())
|
|
222
|
-
.map(|p| (*p).to_string())
|
|
223
|
-
.collect::<Vec<_>>();
|
|
224
|
-
|
|
225
|
-
format!(
|
|
226
|
-
"env={:?} resolved={} exists={} fallbacks_found={:?} path_preview={:?}",
|
|
227
|
-
tessdata_env,
|
|
228
|
-
if tessdata_path.is_empty() {
|
|
229
|
-
"unset"
|
|
230
|
-
} else {
|
|
231
|
-
&tessdata_path
|
|
232
|
-
},
|
|
233
|
-
resolved_exists,
|
|
234
|
-
available_fallbacks,
|
|
235
|
-
path_preview
|
|
236
|
-
)
|
|
237
|
-
});
|
|
238
|
-
|
|
239
|
-
log_ci_debug(ci_debug_enabled, "tesseract_version", || {
|
|
240
|
-
format!("version={}", TesseractAPI::version())
|
|
241
|
-
});
|
|
242
|
-
|
|
243
|
-
// Validate language before initializing to prevent segfault ~keep
|
|
244
|
-
if config.language.trim().is_empty() {
|
|
245
|
-
return Err(OcrError::TesseractInitializationFailed(
|
|
246
|
-
"Language cannot be empty. Please specify a valid language code (e.g., 'eng')".to_string(),
|
|
247
|
-
));
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
// Validate language file exists before initializing to prevent segfault ~keep
|
|
251
|
-
if !tessdata_path.is_empty() {
|
|
252
|
-
let languages: Vec<&str> = config.language.split('+').collect();
|
|
253
|
-
for lang in languages {
|
|
254
|
-
let lang = lang.trim();
|
|
255
|
-
if lang.is_empty() {
|
|
256
|
-
continue;
|
|
257
|
-
}
|
|
258
|
-
let traineddata_path = Path::new(&tessdata_path).join(format!("{}.traineddata", lang));
|
|
259
|
-
if !traineddata_path.exists() {
|
|
260
|
-
return Err(OcrError::TesseractInitializationFailed(format!(
|
|
261
|
-
"Language '{}' not found. Traineddata file does not exist: {}",
|
|
262
|
-
lang,
|
|
263
|
-
traineddata_path.display()
|
|
264
|
-
)));
|
|
265
|
-
}
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
let init_result = api.init(&tessdata_path, &config.language);
|
|
270
|
-
log_ci_debug(ci_debug_enabled, "init", || match &init_result {
|
|
271
|
-
Ok(_) => format!("language={} datapath='{}'", config.language, tessdata_path),
|
|
272
|
-
Err(err) => format!(
|
|
273
|
-
"language={} datapath='{}' error={:?}",
|
|
274
|
-
config.language, tessdata_path, err
|
|
275
|
-
),
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
init_result.map_err(|e| {
|
|
279
|
-
OcrError::TesseractInitializationFailed(format!(
|
|
280
|
-
"Failed to initialize language '{}': {}",
|
|
281
|
-
config.language, e
|
|
282
|
-
))
|
|
283
|
-
})?;
|
|
284
|
-
|
|
285
|
-
if ci_debug_enabled {
|
|
286
|
-
match api.get_available_languages() {
|
|
287
|
-
Ok(languages) => {
|
|
288
|
-
log_ci_debug(ci_debug_enabled, "available_languages", move || {
|
|
289
|
-
let preview = languages.iter().take(10).cloned().collect::<Vec<_>>();
|
|
290
|
-
format!("count={} preview={:?}", languages.len(), preview)
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
Err(err) => {
|
|
294
|
-
log_ci_debug(ci_debug_enabled, "available_languages_error", move || {
|
|
295
|
-
format!("error={:?}", err)
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
let psm_mode = TessPageSegMode::from_int(config.psm as i32);
|
|
302
|
-
let psm_result = api.set_page_seg_mode(psm_mode);
|
|
303
|
-
log_ci_debug(ci_debug_enabled, "set_psm", || match &psm_result {
|
|
304
|
-
Ok(_) => format!("mode={}", config.psm),
|
|
305
|
-
Err(err) => format!("error={:?}", err),
|
|
306
|
-
});
|
|
307
|
-
psm_result.map_err(|e| OcrError::InvalidConfiguration(format!("Failed to set PSM mode: {}", e)))?;
|
|
308
|
-
|
|
309
|
-
api.set_variable(
|
|
310
|
-
"classify_use_pre_adapted_templates",
|
|
311
|
-
&config.classify_use_pre_adapted_templates.to_string(),
|
|
312
|
-
)
|
|
313
|
-
.map_err(|e| {
|
|
314
|
-
OcrError::InvalidConfiguration(format!("Failed to set classify_use_pre_adapted_templates: {}", e))
|
|
315
|
-
})?;
|
|
316
|
-
|
|
317
|
-
api.set_variable("language_model_ngram_on", &config.language_model_ngram_on.to_string())
|
|
318
|
-
.map_err(|e| OcrError::InvalidConfiguration(format!("Failed to set language_model_ngram_on: {}", e)))?;
|
|
319
|
-
|
|
320
|
-
api.set_variable(
|
|
321
|
-
"tessedit_dont_blkrej_good_wds",
|
|
322
|
-
&config.tessedit_dont_blkrej_good_wds.to_string(),
|
|
323
|
-
)
|
|
324
|
-
.map_err(|e| OcrError::InvalidConfiguration(format!("Failed to set tessedit_dont_blkrej_good_wds: {}", e)))?;
|
|
325
|
-
|
|
326
|
-
api.set_variable(
|
|
327
|
-
"tessedit_dont_rowrej_good_wds",
|
|
328
|
-
&config.tessedit_dont_rowrej_good_wds.to_string(),
|
|
329
|
-
)
|
|
330
|
-
.map_err(|e| OcrError::InvalidConfiguration(format!("Failed to set tessedit_dont_rowrej_good_wds: {}", e)))?;
|
|
331
|
-
|
|
332
|
-
api.set_variable(
|
|
333
|
-
"tessedit_enable_dict_correction",
|
|
334
|
-
&config.tessedit_enable_dict_correction.to_string(),
|
|
335
|
-
)
|
|
336
|
-
.map_err(|e| OcrError::InvalidConfiguration(format!("Failed to set tessedit_enable_dict_correction: {}", e)))?;
|
|
337
|
-
|
|
338
|
-
if !config.tessedit_char_whitelist.is_empty() {
|
|
339
|
-
api.set_variable("tessedit_char_whitelist", &config.tessedit_char_whitelist)
|
|
340
|
-
.map_err(|e| OcrError::InvalidConfiguration(format!("Failed to set tessedit_char_whitelist: {}", e)))?;
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
api.set_variable(
|
|
344
|
-
"tessedit_use_primary_params_model",
|
|
345
|
-
&config.tessedit_use_primary_params_model.to_string(),
|
|
346
|
-
)
|
|
347
|
-
.map_err(|e| {
|
|
348
|
-
OcrError::InvalidConfiguration(format!("Failed to set tessedit_use_primary_params_model: {}", e))
|
|
349
|
-
})?;
|
|
350
|
-
|
|
351
|
-
api.set_variable(
|
|
352
|
-
"textord_space_size_is_variable",
|
|
353
|
-
&config.textord_space_size_is_variable.to_string(),
|
|
354
|
-
)
|
|
355
|
-
.map_err(|e| OcrError::InvalidConfiguration(format!("Failed to set textord_space_size_is_variable: {}", e)))?;
|
|
356
|
-
|
|
357
|
-
api.set_variable("thresholding_method", &config.thresholding_method.to_string())
|
|
358
|
-
.map_err(|e| OcrError::InvalidConfiguration(format!("Failed to set thresholding_method: {}", e)))?;
|
|
359
|
-
|
|
360
|
-
api.set_image(
|
|
361
|
-
rgb_image.as_raw(),
|
|
362
|
-
width as i32,
|
|
363
|
-
height as i32,
|
|
364
|
-
bytes_per_pixel as i32,
|
|
365
|
-
bytes_per_line as i32,
|
|
366
|
-
)
|
|
367
|
-
.map_err(|e| OcrError::ProcessingFailed(format!("Failed to set image: {}", e)))?;
|
|
368
|
-
|
|
369
|
-
log_ci_debug(ci_debug_enabled, "set_image", || {
|
|
370
|
-
format!(
|
|
371
|
-
"width={} height={} bytes_per_pixel={} bytes_per_line={}",
|
|
372
|
-
width, height, bytes_per_pixel, bytes_per_line
|
|
373
|
-
)
|
|
374
|
-
});
|
|
375
|
-
|
|
376
|
-
api.recognize()
|
|
377
|
-
.map_err(|e| OcrError::ProcessingFailed(format!("Failed to recognize text: {}", e)))?;
|
|
378
|
-
|
|
379
|
-
log_ci_debug(ci_debug_enabled, "recognize", || "completed".to_string());
|
|
380
|
-
|
|
381
|
-
let tsv_data_for_tables = if config.enable_table_detection || config.output_format == "tsv" {
|
|
382
|
-
Some(
|
|
383
|
-
api.get_tsv_text(0)
|
|
384
|
-
.map_err(|e| OcrError::ProcessingFailed(format!("Failed to extract TSV: {}", e)))?,
|
|
385
|
-
)
|
|
386
|
-
} else {
|
|
387
|
-
None
|
|
388
|
-
};
|
|
389
|
-
|
|
390
|
-
let (raw_content, mime_type) = match config.output_format.as_str() {
|
|
391
|
-
"text" => {
|
|
392
|
-
let text = api
|
|
393
|
-
.get_utf8_text()
|
|
394
|
-
.map_err(|e| OcrError::ProcessingFailed(format!("Failed to extract text: {}", e)))?;
|
|
395
|
-
(text, "text/plain".to_string())
|
|
396
|
-
}
|
|
397
|
-
"markdown" => {
|
|
398
|
-
let hocr = api
|
|
399
|
-
.get_hocr_text(0)
|
|
400
|
-
.map_err(|e| OcrError::ProcessingFailed(format!("Failed to extract hOCR: {}", e)))?;
|
|
401
|
-
|
|
402
|
-
let markdown = convert_hocr_to_markdown(&hocr, None)?;
|
|
403
|
-
(markdown, "text/markdown".to_string())
|
|
404
|
-
}
|
|
405
|
-
"hocr" => {
|
|
406
|
-
let hocr = api
|
|
407
|
-
.get_hocr_text(0)
|
|
408
|
-
.map_err(|e| OcrError::ProcessingFailed(format!("Failed to extract hOCR: {}", e)))?;
|
|
409
|
-
(hocr, "text/html".to_string())
|
|
410
|
-
}
|
|
411
|
-
"tsv" => {
|
|
412
|
-
let tsv = tsv_data_for_tables
|
|
413
|
-
.as_ref()
|
|
414
|
-
.expect("TSV data should be extracted when output_format is 'tsv'")
|
|
415
|
-
.clone();
|
|
416
|
-
(tsv, "text/plain".to_string())
|
|
417
|
-
}
|
|
418
|
-
_ => {
|
|
419
|
-
return Err(OcrError::InvalidConfiguration(format!(
|
|
420
|
-
"Unsupported output format: {}",
|
|
421
|
-
config.output_format
|
|
422
|
-
)));
|
|
423
|
-
}
|
|
424
|
-
};
|
|
425
|
-
|
|
426
|
-
let mut metadata = HashMap::new();
|
|
427
|
-
metadata.insert(
|
|
428
|
-
"language".to_string(),
|
|
429
|
-
serde_json::Value::String(config.language.clone()),
|
|
430
|
-
);
|
|
431
|
-
metadata.insert("psm".to_string(), serde_json::Value::String(config.psm.to_string()));
|
|
432
|
-
metadata.insert(
|
|
433
|
-
"output_format".to_string(),
|
|
434
|
-
serde_json::Value::String(config.output_format.clone()),
|
|
435
|
-
);
|
|
436
|
-
metadata.insert("table_count".to_string(), serde_json::Value::String("0".to_string()));
|
|
437
|
-
metadata.insert(
|
|
438
|
-
"tables_detected".to_string(),
|
|
439
|
-
serde_json::Value::String("0".to_string()),
|
|
440
|
-
);
|
|
441
|
-
if config.output_format == "markdown" {
|
|
442
|
-
metadata.insert(
|
|
443
|
-
"source_format".to_string(),
|
|
444
|
-
serde_json::Value::String("hocr".to_string()),
|
|
445
|
-
);
|
|
446
|
-
}
|
|
447
|
-
|
|
448
|
-
let mut tables = Vec::new();
|
|
449
|
-
|
|
450
|
-
if config.enable_table_detection {
|
|
451
|
-
let tsv_data = tsv_data_for_tables.unwrap();
|
|
452
|
-
|
|
453
|
-
let words = extract_words_from_tsv(&tsv_data, config.table_min_confidence)?;
|
|
454
|
-
|
|
455
|
-
if !words.is_empty() {
|
|
456
|
-
let table = reconstruct_table(&words, config.table_column_threshold, config.table_row_threshold_ratio);
|
|
457
|
-
if !table.is_empty() {
|
|
458
|
-
metadata.insert("table_count".to_string(), serde_json::Value::String("1".to_string()));
|
|
459
|
-
metadata.insert(
|
|
460
|
-
"tables_detected".to_string(),
|
|
461
|
-
serde_json::Value::String("1".to_string()),
|
|
462
|
-
);
|
|
463
|
-
metadata.insert(
|
|
464
|
-
"table_rows".to_string(),
|
|
465
|
-
serde_json::Value::String(table.len().to_string()),
|
|
466
|
-
);
|
|
467
|
-
metadata.insert(
|
|
468
|
-
"table_cols".to_string(),
|
|
469
|
-
serde_json::Value::String(table[0].len().to_string()),
|
|
470
|
-
);
|
|
471
|
-
|
|
472
|
-
let markdown_table = table_to_markdown(&table);
|
|
473
|
-
tables.push(OcrTable {
|
|
474
|
-
cells: table,
|
|
475
|
-
markdown: markdown_table,
|
|
476
|
-
page_number: 0,
|
|
477
|
-
});
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
}
|
|
481
|
-
|
|
482
|
-
let content = strip_control_characters(&raw_content);
|
|
483
|
-
|
|
484
|
-
Ok(OcrExtractionResult {
|
|
485
|
-
content,
|
|
486
|
-
mime_type,
|
|
487
|
-
metadata,
|
|
488
|
-
tables,
|
|
489
|
-
})
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
|
|
493
|
-
#[cfg(test)]
|
|
494
|
-
mod tests {
|
|
495
|
-
use super::*;
|
|
496
|
-
use tempfile::tempdir;
|
|
497
|
-
|
|
498
|
-
fn create_test_config() -> TesseractConfig {
|
|
499
|
-
TesseractConfig {
|
|
500
|
-
output_format: "text".to_string(),
|
|
501
|
-
enable_table_detection: false,
|
|
502
|
-
use_cache: false,
|
|
503
|
-
..TesseractConfig::default()
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
#[allow(dead_code)]
|
|
508
|
-
fn create_simple_test_image() -> Vec<u8> {
|
|
509
|
-
use image::{ImageBuffer, Rgb};
|
|
510
|
-
|
|
511
|
-
let img = ImageBuffer::from_fn(200, 100, |x, y| {
|
|
512
|
-
if x < 100 && y < 50 {
|
|
513
|
-
Rgb([0u8, 0u8, 0u8])
|
|
514
|
-
} else {
|
|
515
|
-
Rgb([255u8, 255u8, 255u8])
|
|
516
|
-
}
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
let mut buffer = Vec::new();
|
|
520
|
-
img.write_to(&mut std::io::Cursor::new(&mut buffer), image::ImageFormat::Png)
|
|
521
|
-
.unwrap();
|
|
522
|
-
buffer
|
|
523
|
-
}
|
|
524
|
-
|
|
525
|
-
#[test]
|
|
526
|
-
fn test_processor_creation() {
|
|
527
|
-
let temp_dir = tempdir().unwrap();
|
|
528
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf()));
|
|
529
|
-
assert!(processor.is_ok());
|
|
530
|
-
}
|
|
531
|
-
|
|
532
|
-
#[test]
|
|
533
|
-
fn test_processor_creation_default_cache_dir() {
|
|
534
|
-
let processor = OcrProcessor::new(None);
|
|
535
|
-
assert!(processor.is_ok());
|
|
536
|
-
}
|
|
537
|
-
|
|
538
|
-
#[test]
|
|
539
|
-
fn test_cache_operations() {
|
|
540
|
-
let temp_dir = tempdir().unwrap();
|
|
541
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
542
|
-
|
|
543
|
-
assert!(processor.clear_cache().is_ok());
|
|
544
|
-
|
|
545
|
-
let stats = processor.get_cache_stats();
|
|
546
|
-
assert!(stats.is_ok());
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
#[test]
|
|
550
|
-
fn test_compute_image_hash_deterministic() {
|
|
551
|
-
use ahash::AHasher;
|
|
552
|
-
use std::hash::{Hash, Hasher};
|
|
553
|
-
|
|
554
|
-
let image_bytes = vec![1, 2, 3, 4, 5];
|
|
555
|
-
|
|
556
|
-
let mut hasher1 = AHasher::default();
|
|
557
|
-
image_bytes.hash(&mut hasher1);
|
|
558
|
-
let hash1 = format!("{:016x}", hasher1.finish());
|
|
559
|
-
|
|
560
|
-
let mut hasher2 = AHasher::default();
|
|
561
|
-
image_bytes.hash(&mut hasher2);
|
|
562
|
-
let hash2 = format!("{:016x}", hasher2.finish());
|
|
563
|
-
|
|
564
|
-
assert_eq!(hash1, hash2);
|
|
565
|
-
assert_eq!(hash1.len(), 16);
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
#[test]
|
|
569
|
-
fn test_hash_config_deterministic() {
|
|
570
|
-
let temp_dir = tempdir().unwrap();
|
|
571
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
572
|
-
let config = create_test_config();
|
|
573
|
-
|
|
574
|
-
let hash1 = processor.hash_config(&config);
|
|
575
|
-
let hash2 = processor.hash_config(&config);
|
|
576
|
-
|
|
577
|
-
assert_eq!(hash1, hash2);
|
|
578
|
-
assert_eq!(hash1.len(), 16);
|
|
579
|
-
}
|
|
580
|
-
|
|
581
|
-
#[test]
|
|
582
|
-
fn test_process_file_nonexistent() {
|
|
583
|
-
let temp_dir = tempdir().unwrap();
|
|
584
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
585
|
-
let config = create_test_config();
|
|
586
|
-
|
|
587
|
-
let result = processor.process_file("/nonexistent/file.png", &config);
|
|
588
|
-
assert!(result.is_err());
|
|
589
|
-
assert!(result.unwrap_err().to_string().contains("Failed to read file"));
|
|
590
|
-
}
|
|
591
|
-
|
|
592
|
-
#[test]
|
|
593
|
-
fn test_process_files_batch_empty() {
|
|
594
|
-
let temp_dir = tempdir().unwrap();
|
|
595
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
596
|
-
let config = create_test_config();
|
|
597
|
-
|
|
598
|
-
let results = processor.process_files_batch(vec![], &config);
|
|
599
|
-
assert_eq!(results.len(), 0);
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
#[test]
|
|
603
|
-
fn test_process_image_invalid_image_data() {
|
|
604
|
-
let temp_dir = tempdir().unwrap();
|
|
605
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
606
|
-
let config = create_test_config();
|
|
607
|
-
|
|
608
|
-
let invalid_data = vec![0, 1, 2, 3, 4];
|
|
609
|
-
let result = processor.process_image(&invalid_data, &config);
|
|
610
|
-
|
|
611
|
-
assert!(result.is_err());
|
|
612
|
-
}
|
|
613
|
-
|
|
614
|
-
#[test]
|
|
615
|
-
fn test_strip_control_characters() {
|
|
616
|
-
let input = "Hello\x00World\x01Test";
|
|
617
|
-
let output = strip_control_characters(input);
|
|
618
|
-
assert_eq!(output, "HelloWorldTest");
|
|
619
|
-
|
|
620
|
-
let input_with_newlines = "Hello\nWorld\rTest\t!";
|
|
621
|
-
let output = strip_control_characters(input_with_newlines);
|
|
622
|
-
assert_eq!(output, "Hello\nWorld\rTest\t!");
|
|
623
|
-
}
|
|
624
|
-
|
|
625
|
-
#[test]
|
|
626
|
-
fn test_strip_control_characters_all_control() {
|
|
627
|
-
let input = "\x00\x01\x02\x03";
|
|
628
|
-
let output = strip_control_characters(input);
|
|
629
|
-
assert_eq!(output, "");
|
|
630
|
-
}
|
|
631
|
-
|
|
632
|
-
#[test]
|
|
633
|
-
fn test_strip_control_characters_no_control() {
|
|
634
|
-
let input = "Hello World Test";
|
|
635
|
-
let output = strip_control_characters(input);
|
|
636
|
-
assert_eq!(output, "Hello World Test");
|
|
637
|
-
}
|
|
638
|
-
|
|
639
|
-
#[test]
|
|
640
|
-
fn test_strip_control_characters_delete_char() {
|
|
641
|
-
let input = "Hello\x7FWorld";
|
|
642
|
-
let output = strip_control_characters(input);
|
|
643
|
-
assert_eq!(output, "HelloWorld");
|
|
644
|
-
}
|
|
645
|
-
|
|
646
|
-
#[test]
|
|
647
|
-
fn test_process_files_batch_single_file() {
|
|
648
|
-
let temp_dir = tempdir().unwrap();
|
|
649
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
650
|
-
let config = create_test_config();
|
|
651
|
-
|
|
652
|
-
let results = processor.process_files_batch(vec!["/nonexistent.png".to_string()], &config);
|
|
653
|
-
assert_eq!(results.len(), 1);
|
|
654
|
-
assert!(!results[0].success);
|
|
655
|
-
assert!(results[0].error.is_some());
|
|
656
|
-
assert!(results[0].result.is_none());
|
|
657
|
-
}
|
|
658
|
-
|
|
659
|
-
#[test]
|
|
660
|
-
fn test_process_files_batch_multiple_files() {
|
|
661
|
-
let temp_dir = tempdir().unwrap();
|
|
662
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
663
|
-
let config = create_test_config();
|
|
664
|
-
|
|
665
|
-
let file_paths = vec![
|
|
666
|
-
"/nonexistent1.png".to_string(),
|
|
667
|
-
"/nonexistent2.png".to_string(),
|
|
668
|
-
"/nonexistent3.png".to_string(),
|
|
669
|
-
];
|
|
670
|
-
|
|
671
|
-
let results = processor.process_files_batch(file_paths, &config);
|
|
672
|
-
assert_eq!(results.len(), 3);
|
|
673
|
-
|
|
674
|
-
for result in &results {
|
|
675
|
-
assert!(!result.success);
|
|
676
|
-
assert!(result.error.is_some());
|
|
677
|
-
assert!(result.result.is_none());
|
|
678
|
-
}
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
#[test]
|
|
682
|
-
fn test_batch_item_result_structure() {
|
|
683
|
-
let success_result = BatchItemResult {
|
|
684
|
-
file_path: "test.png".to_string(),
|
|
685
|
-
success: true,
|
|
686
|
-
result: Some(OcrExtractionResult {
|
|
687
|
-
content: "test".to_string(),
|
|
688
|
-
mime_type: "text/plain".to_string(),
|
|
689
|
-
metadata: HashMap::new(),
|
|
690
|
-
tables: vec![],
|
|
691
|
-
}),
|
|
692
|
-
error: None,
|
|
693
|
-
};
|
|
694
|
-
|
|
695
|
-
assert!(success_result.success);
|
|
696
|
-
assert!(success_result.result.is_some());
|
|
697
|
-
assert!(success_result.error.is_none());
|
|
698
|
-
|
|
699
|
-
let error_result = BatchItemResult {
|
|
700
|
-
file_path: "error.png".to_string(),
|
|
701
|
-
success: false,
|
|
702
|
-
result: None,
|
|
703
|
-
error: Some("Test error".to_string()),
|
|
704
|
-
};
|
|
705
|
-
|
|
706
|
-
assert!(!error_result.success);
|
|
707
|
-
assert!(error_result.result.is_none());
|
|
708
|
-
assert!(error_result.error.is_some());
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
#[test]
|
|
712
|
-
fn test_hash_config_different_languages() {
|
|
713
|
-
let temp_dir = tempdir().unwrap();
|
|
714
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
715
|
-
|
|
716
|
-
let mut config1 = create_test_config();
|
|
717
|
-
config1.language = "eng".to_string();
|
|
718
|
-
|
|
719
|
-
let mut config2 = create_test_config();
|
|
720
|
-
config2.language = "fra".to_string();
|
|
721
|
-
|
|
722
|
-
let hash1 = processor.hash_config(&config1);
|
|
723
|
-
let hash2 = processor.hash_config(&config2);
|
|
724
|
-
|
|
725
|
-
assert_ne!(hash1, hash2);
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
#[test]
|
|
729
|
-
fn test_hash_config_different_psm() {
|
|
730
|
-
let temp_dir = tempdir().unwrap();
|
|
731
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
732
|
-
|
|
733
|
-
let mut config1 = create_test_config();
|
|
734
|
-
config1.psm = 3;
|
|
735
|
-
|
|
736
|
-
let mut config2 = create_test_config();
|
|
737
|
-
config2.psm = 6;
|
|
738
|
-
|
|
739
|
-
let hash1 = processor.hash_config(&config1);
|
|
740
|
-
let hash2 = processor.hash_config(&config2);
|
|
741
|
-
|
|
742
|
-
assert_ne!(hash1, hash2);
|
|
743
|
-
}
|
|
744
|
-
|
|
745
|
-
#[test]
|
|
746
|
-
fn test_hash_config_different_output_format() {
|
|
747
|
-
let temp_dir = tempdir().unwrap();
|
|
748
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
749
|
-
|
|
750
|
-
let mut config1 = create_test_config();
|
|
751
|
-
config1.output_format = "text".to_string();
|
|
752
|
-
|
|
753
|
-
let mut config2 = create_test_config();
|
|
754
|
-
config2.output_format = "markdown".to_string();
|
|
755
|
-
|
|
756
|
-
let hash1 = processor.hash_config(&config1);
|
|
757
|
-
let hash2 = processor.hash_config(&config2);
|
|
758
|
-
|
|
759
|
-
assert_ne!(hash1, hash2);
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
#[test]
|
|
763
|
-
fn test_hash_config_table_detection_flag() {
|
|
764
|
-
let temp_dir = tempdir().unwrap();
|
|
765
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
766
|
-
|
|
767
|
-
let mut config1 = create_test_config();
|
|
768
|
-
config1.enable_table_detection = false;
|
|
769
|
-
|
|
770
|
-
let mut config2 = create_test_config();
|
|
771
|
-
config2.enable_table_detection = true;
|
|
772
|
-
|
|
773
|
-
let hash1 = processor.hash_config(&config1);
|
|
774
|
-
let hash2 = processor.hash_config(&config2);
|
|
775
|
-
|
|
776
|
-
assert_ne!(hash1, hash2);
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
#[test]
|
|
780
|
-
fn test_hash_config_whitelist() {
|
|
781
|
-
let temp_dir = tempdir().unwrap();
|
|
782
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
783
|
-
|
|
784
|
-
let mut config1 = create_test_config();
|
|
785
|
-
config1.tessedit_char_whitelist = "".to_string();
|
|
786
|
-
|
|
787
|
-
let mut config2 = create_test_config();
|
|
788
|
-
config2.tessedit_char_whitelist = "0123456789".to_string();
|
|
789
|
-
|
|
790
|
-
let hash1 = processor.hash_config(&config1);
|
|
791
|
-
let hash2 = processor.hash_config(&config2);
|
|
792
|
-
|
|
793
|
-
assert_ne!(hash1, hash2);
|
|
794
|
-
}
|
|
795
|
-
|
|
796
|
-
#[test]
|
|
797
|
-
fn test_process_image_with_cache_disabled() {
|
|
798
|
-
let temp_dir = tempdir().unwrap();
|
|
799
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
800
|
-
|
|
801
|
-
let mut config = create_test_config();
|
|
802
|
-
config.use_cache = false;
|
|
803
|
-
|
|
804
|
-
let invalid_data = vec![0, 1, 2, 3];
|
|
805
|
-
let result = processor.process_image(&invalid_data, &config);
|
|
806
|
-
|
|
807
|
-
assert!(result.is_err());
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
#[test]
|
|
811
|
-
fn test_process_files_batch_preserves_order() {
|
|
812
|
-
let temp_dir = tempdir().unwrap();
|
|
813
|
-
let processor = OcrProcessor::new(Some(temp_dir.path().to_path_buf())).unwrap();
|
|
814
|
-
let config = create_test_config();
|
|
815
|
-
|
|
816
|
-
let file_paths = vec![
|
|
817
|
-
"file1.png".to_string(),
|
|
818
|
-
"file2.png".to_string(),
|
|
819
|
-
"file3.png".to_string(),
|
|
820
|
-
];
|
|
821
|
-
|
|
822
|
-
let results = processor.process_files_batch(file_paths.clone(), &config);
|
|
823
|
-
|
|
824
|
-
assert_eq!(results.len(), 3);
|
|
825
|
-
assert_eq!(results[0].file_path, "file1.png");
|
|
826
|
-
assert_eq!(results[1].file_path, "file2.png");
|
|
827
|
-
assert_eq!(results[2].file_path, "file3.png");
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
#[test]
|
|
831
|
-
fn test_compute_image_hash_different_data() {
|
|
832
|
-
use ahash::AHasher;
|
|
833
|
-
use std::hash::{Hash, Hasher};
|
|
834
|
-
|
|
835
|
-
let image_bytes1 = vec![1, 2, 3, 4, 5];
|
|
836
|
-
let image_bytes2 = vec![5, 4, 3, 2, 1];
|
|
837
|
-
|
|
838
|
-
let mut hasher1 = AHasher::default();
|
|
839
|
-
image_bytes1.hash(&mut hasher1);
|
|
840
|
-
let hash1 = format!("{:016x}", hasher1.finish());
|
|
841
|
-
|
|
842
|
-
let mut hasher2 = AHasher::default();
|
|
843
|
-
image_bytes2.hash(&mut hasher2);
|
|
844
|
-
let hash2 = format!("{:016x}", hasher2.finish());
|
|
845
|
-
|
|
846
|
-
assert_ne!(hash1, hash2);
|
|
847
|
-
}
|
|
848
|
-
|
|
849
|
-
#[test]
|
|
850
|
-
fn test_log_ci_debug_disabled() {
|
|
851
|
-
log_ci_debug(false, "test_stage", || "test message".to_string());
|
|
852
|
-
}
|
|
853
|
-
|
|
854
|
-
#[test]
|
|
855
|
-
fn test_log_ci_debug_enabled() {
|
|
856
|
-
log_ci_debug(true, "test_stage", || "test message".to_string());
|
|
857
|
-
}
|
|
858
|
-
}
|