kreuzberg 4.0.0.pre.rc.29 → 4.0.0.rc1
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/.gitignore +0 -6
- data/.rubocop.yaml +534 -1
- data/Gemfile +2 -1
- data/Gemfile.lock +28 -116
- data/README.md +269 -629
- data/Rakefile +0 -9
- data/Steepfile +4 -8
- data/examples/async_patterns.rb +58 -1
- data/ext/kreuzberg_rb/extconf.rb +5 -35
- data/ext/kreuzberg_rb/native/Cargo.toml +16 -55
- data/ext/kreuzberg_rb/native/build.rs +14 -12
- data/ext/kreuzberg_rb/native/include/ieeefp.h +1 -1
- data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +1 -1
- data/ext/kreuzberg_rb/native/include/strings.h +2 -2
- data/ext/kreuzberg_rb/native/include/unistd.h +1 -1
- data/ext/kreuzberg_rb/native/src/lib.rs +34 -897
- data/extconf.rb +6 -38
- data/kreuzberg.gemspec +20 -114
- data/lib/kreuzberg/api_proxy.rb +18 -2
- data/lib/kreuzberg/cache_api.rb +0 -22
- data/lib/kreuzberg/cli.rb +10 -2
- data/lib/kreuzberg/cli_proxy.rb +10 -0
- data/lib/kreuzberg/config.rb +22 -274
- data/lib/kreuzberg/errors.rb +7 -73
- data/lib/kreuzberg/extraction_api.rb +8 -237
- data/lib/kreuzberg/mcp_proxy.rb +11 -2
- data/lib/kreuzberg/ocr_backend_protocol.rb +73 -0
- data/lib/kreuzberg/post_processor_protocol.rb +71 -0
- data/lib/kreuzberg/result.rb +33 -151
- data/lib/kreuzberg/setup_lib_path.rb +2 -22
- data/lib/kreuzberg/validator_protocol.rb +73 -0
- data/lib/kreuzberg/version.rb +1 -1
- data/lib/kreuzberg.rb +13 -27
- data/pkg/kreuzberg-4.0.0.rc1.gem +0 -0
- data/sig/kreuzberg.rbs +12 -105
- data/spec/binding/cache_spec.rb +22 -22
- data/spec/binding/cli_proxy_spec.rb +4 -2
- data/spec/binding/cli_spec.rb +11 -12
- data/spec/binding/config_spec.rb +0 -74
- data/spec/binding/config_validation_spec.rb +6 -100
- data/spec/binding/error_handling_spec.rb +97 -283
- data/spec/binding/plugins/ocr_backend_spec.rb +8 -8
- data/spec/binding/plugins/postprocessor_spec.rb +11 -11
- data/spec/binding/plugins/validator_spec.rb +13 -12
- data/spec/examples.txt +104 -0
- data/spec/fixtures/config.toml +1 -0
- data/spec/fixtures/config.yaml +1 -0
- data/spec/fixtures/invalid_config.toml +1 -0
- data/spec/smoke/package_spec.rb +3 -2
- data/spec/spec_helper.rb +3 -1
- data/vendor/kreuzberg/Cargo.toml +67 -192
- data/vendor/kreuzberg/README.md +9 -97
- data/vendor/kreuzberg/build.rs +194 -516
- data/vendor/kreuzberg/src/api/handlers.rs +9 -130
- data/vendor/kreuzberg/src/api/mod.rs +3 -18
- data/vendor/kreuzberg/src/api/server.rs +71 -236
- data/vendor/kreuzberg/src/api/types.rs +7 -43
- data/vendor/kreuzberg/src/bin/profile_extract.rs +455 -0
- data/vendor/kreuzberg/src/cache/mod.rs +3 -27
- data/vendor/kreuzberg/src/chunking/mod.rs +79 -1705
- data/vendor/kreuzberg/src/core/batch_mode.rs +0 -60
- data/vendor/kreuzberg/src/core/config.rs +23 -905
- data/vendor/kreuzberg/src/core/extractor.rs +106 -403
- data/vendor/kreuzberg/src/core/io.rs +2 -4
- data/vendor/kreuzberg/src/core/mime.rs +12 -2
- data/vendor/kreuzberg/src/core/mod.rs +3 -22
- data/vendor/kreuzberg/src/core/pipeline.rs +78 -395
- data/vendor/kreuzberg/src/embeddings.rs +21 -169
- data/vendor/kreuzberg/src/error.rs +2 -2
- data/vendor/kreuzberg/src/extraction/archive.rs +31 -36
- data/vendor/kreuzberg/src/extraction/docx.rs +1 -365
- data/vendor/kreuzberg/src/extraction/email.rs +11 -12
- data/vendor/kreuzberg/src/extraction/excel.rs +129 -138
- data/vendor/kreuzberg/src/extraction/html.rs +170 -1447
- data/vendor/kreuzberg/src/extraction/image.rs +14 -138
- data/vendor/kreuzberg/src/extraction/libreoffice.rs +3 -13
- data/vendor/kreuzberg/src/extraction/mod.rs +5 -21
- data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +0 -2
- data/vendor/kreuzberg/src/extraction/pandoc/batch.rs +275 -0
- data/vendor/kreuzberg/src/extraction/pandoc/mime_types.rs +178 -0
- data/vendor/kreuzberg/src/extraction/pandoc/mod.rs +491 -0
- data/vendor/kreuzberg/src/extraction/pandoc/server.rs +496 -0
- data/vendor/kreuzberg/src/extraction/pandoc/subprocess.rs +1188 -0
- data/vendor/kreuzberg/src/extraction/pandoc/version.rs +162 -0
- data/vendor/kreuzberg/src/extraction/pptx.rs +94 -196
- data/vendor/kreuzberg/src/extraction/structured.rs +4 -5
- data/vendor/kreuzberg/src/extraction/table.rs +1 -2
- data/vendor/kreuzberg/src/extraction/text.rs +10 -18
- data/vendor/kreuzberg/src/extractors/archive.rs +0 -22
- data/vendor/kreuzberg/src/extractors/docx.rs +148 -69
- data/vendor/kreuzberg/src/extractors/email.rs +9 -37
- data/vendor/kreuzberg/src/extractors/excel.rs +40 -81
- data/vendor/kreuzberg/src/extractors/html.rs +173 -182
- data/vendor/kreuzberg/src/extractors/image.rs +8 -32
- data/vendor/kreuzberg/src/extractors/mod.rs +10 -171
- data/vendor/kreuzberg/src/extractors/pandoc.rs +201 -0
- data/vendor/kreuzberg/src/extractors/pdf.rs +64 -329
- data/vendor/kreuzberg/src/extractors/pptx.rs +34 -79
- data/vendor/kreuzberg/src/extractors/structured.rs +0 -16
- data/vendor/kreuzberg/src/extractors/text.rs +7 -30
- data/vendor/kreuzberg/src/extractors/xml.rs +8 -27
- data/vendor/kreuzberg/src/keywords/processor.rs +1 -9
- data/vendor/kreuzberg/src/keywords/rake.rs +1 -0
- data/vendor/kreuzberg/src/language_detection/mod.rs +51 -94
- data/vendor/kreuzberg/src/lib.rs +5 -17
- data/vendor/kreuzberg/src/mcp/mod.rs +1 -4
- data/vendor/kreuzberg/src/mcp/server.rs +21 -145
- data/vendor/kreuzberg/src/ocr/mod.rs +0 -2
- data/vendor/kreuzberg/src/ocr/processor.rs +8 -19
- data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +0 -2
- data/vendor/kreuzberg/src/pdf/error.rs +1 -93
- data/vendor/kreuzberg/src/pdf/metadata.rs +100 -263
- data/vendor/kreuzberg/src/pdf/mod.rs +2 -33
- data/vendor/kreuzberg/src/pdf/rendering.rs +12 -12
- data/vendor/kreuzberg/src/pdf/table.rs +64 -61
- data/vendor/kreuzberg/src/pdf/text.rs +24 -416
- data/vendor/kreuzberg/src/plugins/extractor.rs +8 -40
- data/vendor/kreuzberg/src/plugins/mod.rs +0 -3
- data/vendor/kreuzberg/src/plugins/ocr.rs +14 -22
- data/vendor/kreuzberg/src/plugins/processor.rs +1 -10
- data/vendor/kreuzberg/src/plugins/registry.rs +0 -15
- data/vendor/kreuzberg/src/plugins/validator.rs +8 -20
- data/vendor/kreuzberg/src/stopwords/mod.rs +2 -2
- data/vendor/kreuzberg/src/text/mod.rs +0 -8
- data/vendor/kreuzberg/src/text/quality.rs +15 -28
- data/vendor/kreuzberg/src/text/string_utils.rs +10 -22
- data/vendor/kreuzberg/src/text/token_reduction/core.rs +50 -86
- data/vendor/kreuzberg/src/text/token_reduction/filters.rs +16 -37
- data/vendor/kreuzberg/src/text/token_reduction/simd_text.rs +1 -2
- data/vendor/kreuzberg/src/types.rs +67 -907
- data/vendor/kreuzberg/src/utils/mod.rs +0 -14
- data/vendor/kreuzberg/src/utils/quality.rs +3 -12
- data/vendor/kreuzberg/tests/api_tests.rs +0 -506
- data/vendor/kreuzberg/tests/archive_integration.rs +0 -2
- data/vendor/kreuzberg/tests/batch_orchestration.rs +12 -57
- data/vendor/kreuzberg/tests/batch_processing.rs +8 -32
- data/vendor/kreuzberg/tests/chunking_offset_demo.rs +92 -0
- data/vendor/kreuzberg/tests/concurrency_stress.rs +8 -40
- data/vendor/kreuzberg/tests/config_features.rs +1 -33
- data/vendor/kreuzberg/tests/config_loading_tests.rs +39 -16
- data/vendor/kreuzberg/tests/core_integration.rs +9 -35
- data/vendor/kreuzberg/tests/csv_integration.rs +81 -71
- data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +25 -23
- data/vendor/kreuzberg/tests/email_integration.rs +1 -3
- data/vendor/kreuzberg/tests/error_handling.rs +34 -43
- data/vendor/kreuzberg/tests/format_integration.rs +1 -7
- data/vendor/kreuzberg/tests/helpers/mod.rs +0 -60
- data/vendor/kreuzberg/tests/image_integration.rs +0 -2
- data/vendor/kreuzberg/tests/mime_detection.rs +16 -17
- data/vendor/kreuzberg/tests/ocr_configuration.rs +0 -4
- data/vendor/kreuzberg/tests/ocr_errors.rs +0 -22
- data/vendor/kreuzberg/tests/ocr_quality.rs +0 -2
- data/vendor/kreuzberg/tests/pandoc_integration.rs +503 -0
- data/vendor/kreuzberg/tests/pdf_integration.rs +0 -2
- data/vendor/kreuzberg/tests/pipeline_integration.rs +2 -36
- data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +0 -5
- data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +1 -17
- data/vendor/kreuzberg/tests/plugin_system.rs +0 -6
- data/vendor/kreuzberg/tests/registry_integration_tests.rs +22 -2
- data/vendor/kreuzberg/tests/security_validation.rs +1 -13
- data/vendor/kreuzberg/tests/test_fastembed.rs +23 -45
- metadata +25 -171
- data/.rubocop.yml +0 -543
- data/ext/kreuzberg_rb/native/.cargo/config.toml +0 -23
- data/ext/kreuzberg_rb/native/Cargo.lock +0 -7619
- data/lib/kreuzberg/error_context.rb +0 -136
- data/lib/kreuzberg/types.rb +0 -170
- data/lib/libpdfium.so +0 -0
- data/spec/binding/async_operations_spec.rb +0 -473
- data/spec/binding/batch_operations_spec.rb +0 -595
- data/spec/binding/batch_spec.rb +0 -359
- data/spec/binding/config_result_spec.rb +0 -377
- data/spec/binding/embeddings_spec.rb +0 -816
- data/spec/binding/error_recovery_spec.rb +0 -488
- data/spec/binding/font_config_spec.rb +0 -220
- data/spec/binding/images_spec.rb +0 -738
- data/spec/binding/keywords_extraction_spec.rb +0 -600
- data/spec/binding/metadata_types_spec.rb +0 -1228
- data/spec/binding/pages_extraction_spec.rb +0 -471
- data/spec/binding/tables_spec.rb +0 -641
- data/spec/unit/config/chunking_config_spec.rb +0 -213
- data/spec/unit/config/embedding_config_spec.rb +0 -343
- data/spec/unit/config/extraction_config_spec.rb +0 -438
- data/spec/unit/config/font_config_spec.rb +0 -285
- data/spec/unit/config/hierarchy_config_spec.rb +0 -314
- data/spec/unit/config/image_extraction_config_spec.rb +0 -209
- data/spec/unit/config/image_preprocessing_config_spec.rb +0 -249
- data/spec/unit/config/keyword_config_spec.rb +0 -229
- data/spec/unit/config/language_detection_config_spec.rb +0 -258
- data/spec/unit/config/ocr_config_spec.rb +0 -171
- data/spec/unit/config/page_config_spec.rb +0 -221
- data/spec/unit/config/pdf_config_spec.rb +0 -267
- data/spec/unit/config/postprocessor_config_spec.rb +0 -290
- data/spec/unit/config/tesseract_config_spec.rb +0 -181
- data/spec/unit/config/token_reduction_config_spec.rb +0 -251
- data/test/metadata_types_test.rb +0 -959
- data/vendor/Cargo.toml +0 -61
- data/vendor/kreuzberg/examples/bench_fixes.rs +0 -71
- data/vendor/kreuzberg/examples/test_pdfium_fork.rs +0 -62
- data/vendor/kreuzberg/src/chunking/processor.rs +0 -219
- data/vendor/kreuzberg/src/core/batch_optimizations.rs +0 -385
- data/vendor/kreuzberg/src/core/config_validation.rs +0 -949
- data/vendor/kreuzberg/src/core/formats.rs +0 -235
- data/vendor/kreuzberg/src/core/server_config.rs +0 -1220
- data/vendor/kreuzberg/src/extraction/capacity.rs +0 -263
- data/vendor/kreuzberg/src/extraction/markdown.rs +0 -216
- data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +0 -284
- data/vendor/kreuzberg/src/extractors/bibtex.rs +0 -470
- data/vendor/kreuzberg/src/extractors/docbook.rs +0 -504
- data/vendor/kreuzberg/src/extractors/epub.rs +0 -696
- data/vendor/kreuzberg/src/extractors/fictionbook.rs +0 -492
- data/vendor/kreuzberg/src/extractors/jats.rs +0 -1054
- data/vendor/kreuzberg/src/extractors/jupyter.rs +0 -368
- data/vendor/kreuzberg/src/extractors/latex.rs +0 -653
- data/vendor/kreuzberg/src/extractors/markdown.rs +0 -701
- data/vendor/kreuzberg/src/extractors/odt.rs +0 -628
- data/vendor/kreuzberg/src/extractors/opml.rs +0 -635
- data/vendor/kreuzberg/src/extractors/orgmode.rs +0 -529
- data/vendor/kreuzberg/src/extractors/rst.rs +0 -577
- data/vendor/kreuzberg/src/extractors/rtf.rs +0 -809
- data/vendor/kreuzberg/src/extractors/security.rs +0 -484
- data/vendor/kreuzberg/src/extractors/security_tests.rs +0 -367
- data/vendor/kreuzberg/src/extractors/typst.rs +0 -651
- data/vendor/kreuzberg/src/language_detection/processor.rs +0 -218
- data/vendor/kreuzberg/src/ocr/language_registry.rs +0 -520
- data/vendor/kreuzberg/src/panic_context.rs +0 -154
- data/vendor/kreuzberg/src/pdf/bindings.rs +0 -306
- data/vendor/kreuzberg/src/pdf/bundled.rs +0 -408
- data/vendor/kreuzberg/src/pdf/fonts.rs +0 -358
- data/vendor/kreuzberg/src/pdf/hierarchy.rs +0 -903
- data/vendor/kreuzberg/src/text/quality_processor.rs +0 -231
- data/vendor/kreuzberg/src/text/utf8_validation.rs +0 -193
- data/vendor/kreuzberg/src/utils/pool.rs +0 -503
- data/vendor/kreuzberg/src/utils/pool_sizing.rs +0 -364
- data/vendor/kreuzberg/src/utils/string_pool.rs +0 -761
- data/vendor/kreuzberg/tests/api_embed.rs +0 -360
- data/vendor/kreuzberg/tests/api_extract_multipart.rs +0 -52
- data/vendor/kreuzberg/tests/api_large_pdf_extraction.rs +0 -471
- data/vendor/kreuzberg/tests/api_large_pdf_extraction_diagnostics.rs +0 -289
- data/vendor/kreuzberg/tests/batch_pooling_benchmark.rs +0 -154
- data/vendor/kreuzberg/tests/bibtex_parity_test.rs +0 -421
- data/vendor/kreuzberg/tests/config_integration_test.rs +0 -753
- data/vendor/kreuzberg/tests/data/hierarchy_ground_truth.json +0 -294
- data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +0 -500
- data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +0 -370
- data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +0 -275
- data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +0 -228
- data/vendor/kreuzberg/tests/html_table_test.rs +0 -551
- data/vendor/kreuzberg/tests/instrumentation_test.rs +0 -139
- data/vendor/kreuzberg/tests/jats_extractor_tests.rs +0 -639
- data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +0 -704
- data/vendor/kreuzberg/tests/latex_extractor_tests.rs +0 -496
- data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +0 -490
- data/vendor/kreuzberg/tests/ocr_language_registry.rs +0 -191
- data/vendor/kreuzberg/tests/odt_extractor_tests.rs +0 -674
- data/vendor/kreuzberg/tests/opml_extractor_tests.rs +0 -616
- data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +0 -822
- data/vendor/kreuzberg/tests/page_markers.rs +0 -297
- data/vendor/kreuzberg/tests/pdf_hierarchy_detection.rs +0 -301
- data/vendor/kreuzberg/tests/pdf_hierarchy_quality.rs +0 -589
- data/vendor/kreuzberg/tests/pdf_ocr_triggering.rs +0 -301
- data/vendor/kreuzberg/tests/pdf_text_merging.rs +0 -475
- data/vendor/kreuzberg/tests/pdfium_linking.rs +0 -340
- data/vendor/kreuzberg/tests/rst_extractor_tests.rs +0 -694
- data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +0 -775
- data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +0 -1260
- data/vendor/kreuzberg/tests/typst_extractor_tests.rs +0 -648
- data/vendor/kreuzberg-ffi/Cargo.toml +0 -67
- data/vendor/kreuzberg-ffi/README.md +0 -851
- data/vendor/kreuzberg-ffi/benches/result_view_benchmark.rs +0 -227
- data/vendor/kreuzberg-ffi/build.rs +0 -168
- data/vendor/kreuzberg-ffi/cbindgen.toml +0 -37
- data/vendor/kreuzberg-ffi/kreuzberg-ffi.pc.in +0 -12
- data/vendor/kreuzberg-ffi/kreuzberg.h +0 -3012
- data/vendor/kreuzberg-ffi/src/batch_streaming.rs +0 -588
- data/vendor/kreuzberg-ffi/src/config.rs +0 -1341
- data/vendor/kreuzberg-ffi/src/error.rs +0 -901
- data/vendor/kreuzberg-ffi/src/extraction.rs +0 -555
- data/vendor/kreuzberg-ffi/src/helpers.rs +0 -879
- data/vendor/kreuzberg-ffi/src/lib.rs +0 -977
- data/vendor/kreuzberg-ffi/src/memory.rs +0 -493
- data/vendor/kreuzberg-ffi/src/mime.rs +0 -329
- data/vendor/kreuzberg-ffi/src/panic_shield.rs +0 -265
- data/vendor/kreuzberg-ffi/src/plugins/document_extractor.rs +0 -442
- data/vendor/kreuzberg-ffi/src/plugins/mod.rs +0 -14
- data/vendor/kreuzberg-ffi/src/plugins/ocr_backend.rs +0 -628
- data/vendor/kreuzberg-ffi/src/plugins/post_processor.rs +0 -438
- data/vendor/kreuzberg-ffi/src/plugins/validator.rs +0 -329
- data/vendor/kreuzberg-ffi/src/result.rs +0 -510
- data/vendor/kreuzberg-ffi/src/result_pool.rs +0 -639
- data/vendor/kreuzberg-ffi/src/result_view.rs +0 -773
- data/vendor/kreuzberg-ffi/src/string_intern.rs +0 -568
- data/vendor/kreuzberg-ffi/src/types.rs +0 -363
- data/vendor/kreuzberg-ffi/src/util.rs +0 -210
- data/vendor/kreuzberg-ffi/src/validation.rs +0 -848
- data/vendor/kreuzberg-ffi/tests.disabled/README.md +0 -48
- data/vendor/kreuzberg-ffi/tests.disabled/config_loading_tests.rs +0 -299
- data/vendor/kreuzberg-ffi/tests.disabled/config_tests.rs +0 -346
- data/vendor/kreuzberg-ffi/tests.disabled/extractor_tests.rs +0 -232
- data/vendor/kreuzberg-ffi/tests.disabled/plugin_registration_tests.rs +0 -470
- data/vendor/kreuzberg-tesseract/.commitlintrc.json +0 -13
- data/vendor/kreuzberg-tesseract/.crate-ignore +0 -2
- data/vendor/kreuzberg-tesseract/Cargo.lock +0 -2933
- data/vendor/kreuzberg-tesseract/Cargo.toml +0 -57
- data/vendor/kreuzberg-tesseract/LICENSE +0 -22
- data/vendor/kreuzberg-tesseract/README.md +0 -399
- data/vendor/kreuzberg-tesseract/build.rs +0 -1127
- data/vendor/kreuzberg-tesseract/patches/README.md +0 -71
- data/vendor/kreuzberg-tesseract/patches/tesseract.diff +0 -199
- data/vendor/kreuzberg-tesseract/src/api.rs +0 -1371
- data/vendor/kreuzberg-tesseract/src/choice_iterator.rs +0 -77
- data/vendor/kreuzberg-tesseract/src/enums.rs +0 -297
- data/vendor/kreuzberg-tesseract/src/error.rs +0 -81
- data/vendor/kreuzberg-tesseract/src/lib.rs +0 -145
- data/vendor/kreuzberg-tesseract/src/monitor.rs +0 -57
- data/vendor/kreuzberg-tesseract/src/mutable_iterator.rs +0 -197
- data/vendor/kreuzberg-tesseract/src/page_iterator.rs +0 -253
- data/vendor/kreuzberg-tesseract/src/result_iterator.rs +0 -286
- data/vendor/kreuzberg-tesseract/src/result_renderer.rs +0 -183
- data/vendor/kreuzberg-tesseract/tests/integration_test.rs +0 -211
|
@@ -2,19 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
module Kreuzberg
|
|
4
4
|
module ExtractionAPI
|
|
5
|
-
|
|
6
|
-
# @param mime_type [String, nil] Optional MIME type for the file (e.g., 'application/pdf').
|
|
7
|
-
# @param config [Config::Extraction, Hash, nil] Extraction configuration controlling
|
|
8
|
-
# @return [Result] Extraction result containing content, metadata, tables, and images
|
|
9
|
-
# @raise [Errors::IOError] If the file cannot be read or access is denied
|
|
10
|
-
# @raise [Errors::ParsingError] If document parsing fails
|
|
11
|
-
# @raise [Errors::UnsupportedFormatError] If the file format is not supported
|
|
12
|
-
# @raise [Errors::OCRError] If OCR is enabled and fails
|
|
13
|
-
# @raise [Errors::MissingDependencyError] If a required dependency is missing
|
|
14
|
-
# @example Extract a PDF file
|
|
15
|
-
# @example Extract with explicit MIME type
|
|
16
|
-
# @example Extract with OCR enabled
|
|
17
|
-
def extract_file_sync(path:, mime_type: nil, config: nil)
|
|
5
|
+
def extract_file_sync(path, mime_type: nil, config: nil)
|
|
18
6
|
opts = normalize_config(config)
|
|
19
7
|
hash = if mime_type
|
|
20
8
|
native_extract_file_sync(path.to_s, mime_type.to_s, **opts)
|
|
@@ -26,33 +14,7 @@ module Kreuzberg
|
|
|
26
14
|
result
|
|
27
15
|
end
|
|
28
16
|
|
|
29
|
-
|
|
30
|
-
#
|
|
31
|
-
# Performs document extraction directly from binary data in memory. Useful for
|
|
32
|
-
# extracting content from files already loaded into memory or from network streams.
|
|
33
|
-
#
|
|
34
|
-
# @param data [String] Binary document data (can contain any byte values)
|
|
35
|
-
# @param mime_type [String] MIME type of the data (required, e.g., 'application/pdf').
|
|
36
|
-
# This parameter is mandatory to guide the extraction engine.
|
|
37
|
-
# @param config [Config::Extraction, Hash, nil] Extraction configuration. Accepts
|
|
38
|
-
# either a {Config::Extraction} object or a configuration hash.
|
|
39
|
-
#
|
|
40
|
-
# @return [Result] Extraction result containing content, metadata, tables, and images
|
|
41
|
-
#
|
|
42
|
-
# @raise [Errors::ParsingError] If document parsing fails
|
|
43
|
-
# @raise [Errors::UnsupportedFormatError] If the MIME type is not supported
|
|
44
|
-
# @raise [Errors::OCRError] If OCR is enabled and fails
|
|
45
|
-
# @raise [Errors::MissingDependencyError] If a required dependency is missing
|
|
46
|
-
#
|
|
47
|
-
# @example Extract PDF from memory
|
|
48
|
-
# pdf_data = File.read("document.pdf", binmode: true)
|
|
49
|
-
# result = Kreuzberg.extract_bytes_sync(pdf_data, "application/pdf")
|
|
50
|
-
# puts result.content
|
|
51
|
-
#
|
|
52
|
-
# @example Extract from a network stream
|
|
53
|
-
# response = HTTParty.get("https://example.com/document.docx")
|
|
54
|
-
# result = Kreuzberg.extract_bytes_sync(response.body, "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
|
55
|
-
def extract_bytes_sync(data:, mime_type:, config: nil)
|
|
17
|
+
def extract_bytes_sync(data, mime_type, config: nil)
|
|
56
18
|
opts = normalize_config(config)
|
|
57
19
|
hash = native_extract_bytes_sync(data.to_s, mime_type.to_s, **opts)
|
|
58
20
|
result = Result.new(hash)
|
|
@@ -60,38 +22,7 @@ module Kreuzberg
|
|
|
60
22
|
result
|
|
61
23
|
end
|
|
62
24
|
|
|
63
|
-
|
|
64
|
-
#
|
|
65
|
-
# Processes multiple files in a single batch operation. Files are extracted sequentially,
|
|
66
|
-
# and results maintain the same order as the input paths. This is useful for bulk
|
|
67
|
-
# processing multiple documents with consistent configuration.
|
|
68
|
-
#
|
|
69
|
-
# @param paths [Array<String, Pathname>] Array of file paths to extract. Each path
|
|
70
|
-
# is converted to a string and MIME type is auto-detected from extension.
|
|
71
|
-
# @param config [Config::Extraction, Hash, nil] Extraction configuration applied to all files.
|
|
72
|
-
# Accepts either a {Config::Extraction} object or a configuration hash.
|
|
73
|
-
#
|
|
74
|
-
# @return [Array<Result>] Array of extraction results in the same order as input paths.
|
|
75
|
-
# Array length matches the input paths length.
|
|
76
|
-
#
|
|
77
|
-
# @raise [Errors::IOError] If any file cannot be read
|
|
78
|
-
# @raise [Errors::ParsingError] If any document parsing fails
|
|
79
|
-
# @raise [Errors::UnsupportedFormatError] If any file format is not supported
|
|
80
|
-
# @raise [Errors::OCRError] If OCR is enabled and fails on any document
|
|
81
|
-
# @raise [Errors::MissingDependencyError] If a required dependency is missing
|
|
82
|
-
#
|
|
83
|
-
# @example Batch extract multiple PDFs
|
|
84
|
-
# paths = ["doc1.pdf", "doc2.pdf", "doc3.pdf"]
|
|
85
|
-
# results = Kreuzberg.batch_extract_files_sync(paths)
|
|
86
|
-
# results.each_with_index do |result, idx|
|
|
87
|
-
# puts "File #{idx}: #{result.content.length} characters"
|
|
88
|
-
# end
|
|
89
|
-
#
|
|
90
|
-
# @example Batch extract with consistent configuration
|
|
91
|
-
# paths = Dir.glob("documents/*.pdf")
|
|
92
|
-
# config = Kreuzberg::Config::Extraction.new(force_ocr: true)
|
|
93
|
-
# results = Kreuzberg.batch_extract_files_sync(paths, config: config)
|
|
94
|
-
def batch_extract_files_sync(paths:, config: nil)
|
|
25
|
+
def batch_extract_files_sync(paths, config: nil)
|
|
95
26
|
opts = normalize_config(config)
|
|
96
27
|
hashes = native_batch_extract_files_sync(paths.map(&:to_s), **opts)
|
|
97
28
|
results = hashes.map { |hash| Result.new(hash) }
|
|
@@ -99,37 +30,7 @@ module Kreuzberg
|
|
|
99
30
|
results
|
|
100
31
|
end
|
|
101
32
|
|
|
102
|
-
|
|
103
|
-
#
|
|
104
|
-
# Non-blocking extraction that returns a {Result} promise. Extraction is performed
|
|
105
|
-
# in the background using native threads or the Tokio runtime. This method is
|
|
106
|
-
# preferred for I/O-bound operations and integrating with async workflows.
|
|
107
|
-
#
|
|
108
|
-
# @param path [String, Pathname] Path to the document file to extract
|
|
109
|
-
# @param mime_type [String, nil] Optional MIME type for the file (e.g., 'application/pdf').
|
|
110
|
-
# If omitted, type is detected from file extension.
|
|
111
|
-
# @param config [Config::Extraction, Hash, nil] Extraction configuration. Accepts
|
|
112
|
-
# either a {Config::Extraction} object or a configuration hash.
|
|
113
|
-
#
|
|
114
|
-
# @return [Result] Extraction result containing content, metadata, tables, and images.
|
|
115
|
-
# In async contexts, this result is available upon method return.
|
|
116
|
-
#
|
|
117
|
-
# @raise [Errors::IOError] If the file cannot be read or access is denied
|
|
118
|
-
# @raise [Errors::ParsingError] If document parsing fails
|
|
119
|
-
# @raise [Errors::UnsupportedFormatError] If the file format is not supported
|
|
120
|
-
# @raise [Errors::OCRError] If OCR is enabled and fails
|
|
121
|
-
# @raise [Errors::MissingDependencyError] If a required dependency is missing
|
|
122
|
-
#
|
|
123
|
-
# @example Extract a PDF file asynchronously
|
|
124
|
-
# result = Kreuzberg.extract_file("large_document.pdf")
|
|
125
|
-
# puts result.content
|
|
126
|
-
#
|
|
127
|
-
# @example Extract with custom OCR configuration
|
|
128
|
-
# config = Kreuzberg::Config::Extraction.new(
|
|
129
|
-
# ocr: Kreuzberg::Config::OCR.new(language: "deu")
|
|
130
|
-
# )
|
|
131
|
-
# result = Kreuzberg.extract_file("document.pdf", config: config)
|
|
132
|
-
def extract_file(path:, mime_type: nil, config: nil)
|
|
33
|
+
def extract_file(path, mime_type: nil, config: nil)
|
|
133
34
|
opts = normalize_config(config)
|
|
134
35
|
hash = if mime_type
|
|
135
36
|
native_extract_file(path.to_s, mime_type.to_s, **opts)
|
|
@@ -141,37 +42,7 @@ module Kreuzberg
|
|
|
141
42
|
result
|
|
142
43
|
end
|
|
143
44
|
|
|
144
|
-
|
|
145
|
-
#
|
|
146
|
-
# Non-blocking extraction from in-memory binary data. Like {#extract_file},
|
|
147
|
-
# this performs extraction in the background, making it suitable for handling
|
|
148
|
-
# high-volume extraction workloads without blocking the main thread.
|
|
149
|
-
#
|
|
150
|
-
# @param data [String] Binary document data (can contain any byte values)
|
|
151
|
-
# @param mime_type [String] MIME type of the data (required, e.g., 'application/pdf').
|
|
152
|
-
# This parameter is mandatory to guide the extraction engine.
|
|
153
|
-
# @param config [Config::Extraction, Hash, nil] Extraction configuration. Accepts
|
|
154
|
-
# either a {Config::Extraction} object or a configuration hash.
|
|
155
|
-
#
|
|
156
|
-
# @return [Result] Extraction result containing content, metadata, tables, and images
|
|
157
|
-
#
|
|
158
|
-
# @raise [Errors::ParsingError] If document parsing fails
|
|
159
|
-
# @raise [Errors::UnsupportedFormatError] If the MIME type is not supported
|
|
160
|
-
# @raise [Errors::OCRError] If OCR is enabled and fails
|
|
161
|
-
# @raise [Errors::MissingDependencyError] If a required dependency is missing
|
|
162
|
-
#
|
|
163
|
-
# @example Extract PDF from memory asynchronously
|
|
164
|
-
# pdf_data = File.read("document.pdf", binmode: true)
|
|
165
|
-
# result = Kreuzberg.extract_bytes(pdf_data, "application/pdf")
|
|
166
|
-
# puts result.content
|
|
167
|
-
#
|
|
168
|
-
# @example Extract with image extraction
|
|
169
|
-
# data = File.read("file.docx", binmode: true)
|
|
170
|
-
# config = Kreuzberg::Config::Extraction.new(
|
|
171
|
-
# image_extraction: Kreuzberg::Config::ImageExtraction.new(extract_images: true)
|
|
172
|
-
# )
|
|
173
|
-
# result = Kreuzberg.extract_bytes(data, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", config: config)
|
|
174
|
-
def extract_bytes(data:, mime_type:, config: nil)
|
|
45
|
+
def extract_bytes(data, mime_type, config: nil)
|
|
175
46
|
opts = normalize_config(config)
|
|
176
47
|
hash = native_extract_bytes(data.to_s, mime_type.to_s, **opts)
|
|
177
48
|
result = Result.new(hash)
|
|
@@ -179,40 +50,7 @@ module Kreuzberg
|
|
|
179
50
|
result
|
|
180
51
|
end
|
|
181
52
|
|
|
182
|
-
|
|
183
|
-
#
|
|
184
|
-
# Non-blocking batch extraction from multiple files. Results maintain the same order
|
|
185
|
-
# as input paths. This is the preferred method for bulk processing when non-blocking
|
|
186
|
-
# I/O is required (e.g., in web servers or async applications).
|
|
187
|
-
#
|
|
188
|
-
# @param paths [Array<String, Pathname>] Array of file paths to extract. Each path
|
|
189
|
-
# is converted to a string and MIME type is auto-detected from extension.
|
|
190
|
-
# @param config [Config::Extraction, Hash, nil] Extraction configuration applied to all files.
|
|
191
|
-
# Accepts either a {Config::Extraction} object or a configuration hash.
|
|
192
|
-
#
|
|
193
|
-
# @return [Array<Result>] Array of extraction results in the same order as input paths.
|
|
194
|
-
# Array length matches the input paths length.
|
|
195
|
-
#
|
|
196
|
-
# @raise [Errors::IOError] If any file cannot be read
|
|
197
|
-
# @raise [Errors::ParsingError] If any document parsing fails
|
|
198
|
-
# @raise [Errors::UnsupportedFormatError] If any file format is not supported
|
|
199
|
-
# @raise [Errors::OCRError] If OCR is enabled and fails on any document
|
|
200
|
-
# @raise [Errors::MissingDependencyError] If a required dependency is missing
|
|
201
|
-
#
|
|
202
|
-
# @example Batch extract multiple files asynchronously
|
|
203
|
-
# paths = ["invoice_1.pdf", "invoice_2.pdf", "invoice_3.pdf"]
|
|
204
|
-
# results = Kreuzberg.batch_extract_files(paths)
|
|
205
|
-
# results.each_with_index do |result, idx|
|
|
206
|
-
# puts "Invoice #{idx}: #{result.detected_languages}"
|
|
207
|
-
# end
|
|
208
|
-
#
|
|
209
|
-
# @example Batch extract with chunking
|
|
210
|
-
# paths = Dir.glob("reports/*.docx")
|
|
211
|
-
# config = Kreuzberg::Config::Extraction.new(
|
|
212
|
-
# chunking: Kreuzberg::Config::Chunking.new(max_chars: 1000, max_overlap: 200)
|
|
213
|
-
# )
|
|
214
|
-
# results = Kreuzberg.batch_extract_files(paths, config: config)
|
|
215
|
-
def batch_extract_files(paths:, config: nil)
|
|
53
|
+
def batch_extract_files(paths, config: nil)
|
|
216
54
|
opts = normalize_config(config)
|
|
217
55
|
hashes = native_batch_extract_files(paths.map(&:to_s), **opts)
|
|
218
56
|
results = hashes.map { |hash| Result.new(hash) }
|
|
@@ -220,38 +58,7 @@ module Kreuzberg
|
|
|
220
58
|
results
|
|
221
59
|
end
|
|
222
60
|
|
|
223
|
-
|
|
224
|
-
#
|
|
225
|
-
# Processes multiple in-memory binary documents in a single batch operation. Results
|
|
226
|
-
# maintain the same order as the input data array. The mime_types array must have
|
|
227
|
-
# the same length as the data_array.
|
|
228
|
-
#
|
|
229
|
-
# @param data_array [Array<String>] Array of binary document data. Each element can
|
|
230
|
-
# contain any byte values (e.g., PDF binary data).
|
|
231
|
-
# @param mime_types [Array<String>] Array of MIME types corresponding to each data item.
|
|
232
|
-
# Must be the same length as data_array (e.g., ["application/pdf", "application/msword"]).
|
|
233
|
-
# @param config [Config::Extraction, Hash, nil] Extraction configuration applied to all items.
|
|
234
|
-
# Accepts either a {Config::Extraction} object or a configuration hash.
|
|
235
|
-
#
|
|
236
|
-
# @return [Array<Result>] Array of extraction results in the same order as input data.
|
|
237
|
-
# Array length matches the data_array length.
|
|
238
|
-
#
|
|
239
|
-
# @raise [ArgumentError] If data_array and mime_types have different lengths
|
|
240
|
-
# @raise [Errors::ParsingError] If any document parsing fails
|
|
241
|
-
# @raise [Errors::UnsupportedFormatError] If any MIME type is not supported
|
|
242
|
-
# @raise [Errors::OCRError] If OCR is enabled and fails on any document
|
|
243
|
-
# @raise [Errors::MissingDependencyError] If a required dependency is missing
|
|
244
|
-
#
|
|
245
|
-
# @example Batch extract binary documents
|
|
246
|
-
# pdf_data_1 = File.read("doc1.pdf", binmode: true)
|
|
247
|
-
# pdf_data_2 = File.read("doc2.pdf", binmode: true)
|
|
248
|
-
# docx_data = File.read("report.docx", binmode: true)
|
|
249
|
-
#
|
|
250
|
-
# data = [pdf_data_1, pdf_data_2, docx_data]
|
|
251
|
-
# types = ["application/pdf", "application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]
|
|
252
|
-
# results = Kreuzberg.batch_extract_bytes_sync(data, types)
|
|
253
|
-
# results.each { |r| puts r.content }
|
|
254
|
-
def batch_extract_bytes_sync(data_array:, mime_types:, config: nil)
|
|
61
|
+
def batch_extract_bytes_sync(data_array, mime_types, config: nil)
|
|
255
62
|
opts = normalize_config(config)
|
|
256
63
|
hashes = native_batch_extract_bytes_sync(data_array.map(&:to_s), mime_types.map(&:to_s), **opts)
|
|
257
64
|
results = hashes.map { |hash| Result.new(hash) }
|
|
@@ -259,43 +66,7 @@ module Kreuzberg
|
|
|
259
66
|
results
|
|
260
67
|
end
|
|
261
68
|
|
|
262
|
-
|
|
263
|
-
#
|
|
264
|
-
# Non-blocking batch extraction from multiple in-memory binary documents. Results
|
|
265
|
-
# maintain the same order as the input data array. This method is preferred when
|
|
266
|
-
# processing multiple documents without blocking (e.g., handling multiple uploads).
|
|
267
|
-
#
|
|
268
|
-
# @param data_array [Array<String>] Array of binary document data. Each element can
|
|
269
|
-
# contain any byte values (e.g., PDF binary data).
|
|
270
|
-
# @param mime_types [Array<String>] Array of MIME types corresponding to each data item.
|
|
271
|
-
# Must be the same length as data_array (e.g., ["application/pdf", "application/msword"]).
|
|
272
|
-
# @param config [Config::Extraction, Hash, nil] Extraction configuration applied to all items.
|
|
273
|
-
# Accepts either a {Config::Extraction} object or a configuration hash.
|
|
274
|
-
#
|
|
275
|
-
# @return [Array<Result>] Array of extraction results in the same order as input data.
|
|
276
|
-
# Array length matches the data_array length.
|
|
277
|
-
#
|
|
278
|
-
# @raise [ArgumentError] If data_array and mime_types have different lengths
|
|
279
|
-
# @raise [Errors::ParsingError] If any document parsing fails
|
|
280
|
-
# @raise [Errors::UnsupportedFormatError] If any MIME type is not supported
|
|
281
|
-
# @raise [Errors::OCRError] If OCR is enabled and fails on any document
|
|
282
|
-
# @raise [Errors::MissingDependencyError] If a required dependency is missing
|
|
283
|
-
#
|
|
284
|
-
# @example Batch extract uploaded documents asynchronously
|
|
285
|
-
# # From a web request with multiple file uploads
|
|
286
|
-
# uploaded_files = params[:files] # Array of uploaded file objects
|
|
287
|
-
# data = uploaded_files.map(&:read)
|
|
288
|
-
# types = uploaded_files.map(&:content_type)
|
|
289
|
-
#
|
|
290
|
-
# results = Kreuzberg.batch_extract_bytes(data, types)
|
|
291
|
-
# results.each { |r| puts r.content }
|
|
292
|
-
#
|
|
293
|
-
# @example Batch extract with OCR
|
|
294
|
-
# data = [scan_1_bytes, scan_2_bytes, scan_3_bytes]
|
|
295
|
-
# types = ["image/png", "image/png", "image/png"]
|
|
296
|
-
# config = Kreuzberg::Config::Extraction.new(force_ocr: true)
|
|
297
|
-
# results = Kreuzberg.batch_extract_bytes(data, types, config: config)
|
|
298
|
-
def batch_extract_bytes(data_array:, mime_types:, config: nil)
|
|
69
|
+
def batch_extract_bytes(data_array, mime_types, config: nil)
|
|
299
70
|
opts = normalize_config(config)
|
|
300
71
|
hashes = native_batch_extract_bytes(data_array.map(&:to_s), mime_types.map(&:to_s), **opts)
|
|
301
72
|
results = hashes.map { |hash| Result.new(hash) }
|
data/lib/kreuzberg/mcp_proxy.rb
CHANGED
|
@@ -5,7 +5,14 @@ require 'pathname'
|
|
|
5
5
|
require 'json'
|
|
6
6
|
|
|
7
7
|
module Kreuzberg
|
|
8
|
+
# MCP (Model Context Protocol) server proxy
|
|
9
|
+
#
|
|
10
|
+
# Starts and manages the Kreuzberg MCP server for Claude Desktop integration.
|
|
11
|
+
#
|
|
8
12
|
# @example Start MCP server
|
|
13
|
+
# server = Kreuzberg::MCPProxy.new
|
|
14
|
+
# server.start
|
|
15
|
+
#
|
|
9
16
|
module MCPProxy
|
|
10
17
|
Error = Class.new(Kreuzberg::Errors::Error)
|
|
11
18
|
MissingBinaryError = Class.new(Error)
|
|
@@ -53,7 +60,8 @@ module Kreuzberg
|
|
|
53
60
|
|
|
54
61
|
Process.kill('TERM', @pid)
|
|
55
62
|
Process.wait(@pid)
|
|
56
|
-
rescue Errno::ESRCH, Errno::ECHILD
|
|
63
|
+
rescue Errno::ESRCH, Errno::ECHILD
|
|
64
|
+
# Process already dead
|
|
57
65
|
ensure
|
|
58
66
|
@pid = nil
|
|
59
67
|
close_pipes
|
|
@@ -114,7 +122,7 @@ module Kreuzberg
|
|
|
114
122
|
err: $stderr
|
|
115
123
|
)
|
|
116
124
|
Process.detach(@pid)
|
|
117
|
-
sleep 1
|
|
125
|
+
sleep 1 # Give server time to start
|
|
118
126
|
@pid
|
|
119
127
|
end
|
|
120
128
|
|
|
@@ -154,6 +162,7 @@ module Kreuzberg
|
|
|
154
162
|
# @raise [MissingBinaryError] If not found
|
|
155
163
|
#
|
|
156
164
|
def find_mcp_binary
|
|
165
|
+
# MCP is served by kreuzberg CLI
|
|
157
166
|
binary_name = Gem.win_platform? ? 'kreuzberg.exe' : 'kreuzberg'
|
|
158
167
|
found = CLIProxy.search_paths(binary_name).find(&:file?)
|
|
159
168
|
return found if found
|
|
@@ -1,11 +1,84 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Kreuzberg
|
|
4
|
+
# OCR backend protocol interface.
|
|
5
|
+
#
|
|
6
|
+
# This module defines the protocol that all Ruby OCR backends must implement
|
|
7
|
+
# to be registered with the Rust core via the FFI bridge.
|
|
8
|
+
#
|
|
9
|
+
# OCR backends implement optical character recognition for images and scanned documents.
|
|
10
|
+
# They are called when OCR is enabled in the extraction configuration.
|
|
11
|
+
#
|
|
4
12
|
# @example Implementing a custom OCR backend
|
|
13
|
+
# class CustomOcrBackend
|
|
14
|
+
# include Kreuzberg::OcrBackendProtocol
|
|
15
|
+
#
|
|
16
|
+
# def name
|
|
17
|
+
# "custom-ocr"
|
|
18
|
+
# end
|
|
19
|
+
#
|
|
20
|
+
# def process_image(image_bytes, config)
|
|
21
|
+
# # Perform OCR on image_bytes
|
|
22
|
+
# # This is a placeholder - integrate with a real OCR engine
|
|
23
|
+
# text = my_ocr_engine.recognize(image_bytes, language: config["language"])
|
|
24
|
+
# text
|
|
25
|
+
# end
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# backend = CustomOcrBackend.new
|
|
29
|
+
# Kreuzberg.register_ocr_backend(backend.name, backend)
|
|
30
|
+
#
|
|
31
|
+
# # Use in extraction
|
|
32
|
+
# result = Kreuzberg.extract_file_sync(
|
|
33
|
+
# "scanned.pdf",
|
|
34
|
+
# config: { ocr: { backend: "custom-ocr", language: "eng" } }
|
|
35
|
+
# )
|
|
36
|
+
#
|
|
5
37
|
# @example Implementing an OCR backend with initialization
|
|
38
|
+
# class ModelBasedOcr
|
|
39
|
+
# include Kreuzberg::OcrBackendProtocol
|
|
40
|
+
#
|
|
41
|
+
# def initialize
|
|
42
|
+
# @model = nil
|
|
43
|
+
# end
|
|
44
|
+
#
|
|
45
|
+
# def name
|
|
46
|
+
# "model-ocr"
|
|
47
|
+
# end
|
|
48
|
+
#
|
|
49
|
+
# def process_image(image_bytes, config)
|
|
50
|
+
# # Load model on first use (lazy initialization)
|
|
51
|
+
# @model ||= load_model
|
|
52
|
+
#
|
|
53
|
+
# # Run OCR
|
|
54
|
+
# @model.recognize(image_bytes, config)
|
|
55
|
+
# end
|
|
56
|
+
#
|
|
57
|
+
# private
|
|
58
|
+
#
|
|
59
|
+
# def load_model
|
|
60
|
+
# # Load ML model for OCR
|
|
61
|
+
# MyOcrModel.load("path/to/model")
|
|
62
|
+
# end
|
|
63
|
+
# end
|
|
64
|
+
#
|
|
65
|
+
# Kreuzberg.register_ocr_backend("model-ocr", ModelBasedOcr.new)
|
|
66
|
+
#
|
|
6
67
|
module OcrBackendProtocol
|
|
68
|
+
# Return the unique name of this OCR backend.
|
|
69
|
+
#
|
|
70
|
+
# This name is used in ExtractionConfig to select the backend:
|
|
71
|
+
#
|
|
72
|
+
# config = { ocr: { backend: "custom-ocr", language: "eng" } }
|
|
73
|
+
#
|
|
74
|
+
# The name should be a lowercase string with hyphens (e.g., "custom-ocr", "tesseract").
|
|
75
|
+
#
|
|
7
76
|
# @return [String] Unique backend identifier
|
|
77
|
+
#
|
|
8
78
|
# @example
|
|
79
|
+
# def name
|
|
80
|
+
# "custom-ocr"
|
|
81
|
+
# end
|
|
9
82
|
def name
|
|
10
83
|
raise NotImplementedError, "#{self.class} must implement #name"
|
|
11
84
|
end
|
|
@@ -1,13 +1,84 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Kreuzberg
|
|
4
|
+
# PostProcessor protocol interface.
|
|
5
|
+
#
|
|
6
|
+
# This module defines the protocol that all Ruby post-processors must implement
|
|
7
|
+
# to be registered with the Rust core via the FFI bridge.
|
|
8
|
+
#
|
|
9
|
+
# Post-processors enrich extraction results by adding metadata, transforming content,
|
|
10
|
+
# or performing additional analysis. They are called after extraction completes.
|
|
11
|
+
#
|
|
4
12
|
# @example Implementing a simple post-processor
|
|
13
|
+
# class UpcaseProcessor
|
|
14
|
+
# include Kreuzberg::PostProcessorProtocol
|
|
15
|
+
#
|
|
16
|
+
# def call(result)
|
|
17
|
+
# result["content"] = result["content"].upcase
|
|
18
|
+
# result
|
|
19
|
+
# end
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# Kreuzberg.register_post_processor("upcase", UpcaseProcessor.new)
|
|
23
|
+
#
|
|
5
24
|
# @example Implementing a post-processor that adds metadata
|
|
25
|
+
# class EntityExtractor
|
|
26
|
+
# include Kreuzberg::PostProcessorProtocol
|
|
27
|
+
#
|
|
28
|
+
# def call(result)
|
|
29
|
+
# entities = extract_entities(result["content"])
|
|
30
|
+
# result["metadata"]["entities"] = entities
|
|
31
|
+
# result
|
|
32
|
+
# end
|
|
33
|
+
#
|
|
34
|
+
# private
|
|
35
|
+
#
|
|
36
|
+
# def extract_entities(text)
|
|
37
|
+
# # Extract named entities from text
|
|
38
|
+
# # This is a placeholder - use a real NER library in production
|
|
39
|
+
# text.scan(/[A-Z][a-z]+(?:\s[A-Z][a-z]+)*/)
|
|
40
|
+
# end
|
|
41
|
+
# end
|
|
42
|
+
#
|
|
43
|
+
# Kreuzberg.register_post_processor("entities", EntityExtractor.new)
|
|
44
|
+
#
|
|
6
45
|
# @example Using a Proc as a post-processor
|
|
46
|
+
# Kreuzberg.register_post_processor("word_count", ->(result) {
|
|
47
|
+
# word_count = result["content"].split.length
|
|
48
|
+
# result["metadata"]["word_count"] = word_count
|
|
49
|
+
# result
|
|
50
|
+
# })
|
|
51
|
+
#
|
|
7
52
|
module PostProcessorProtocol
|
|
53
|
+
# Process and enrich an extraction result.
|
|
54
|
+
#
|
|
55
|
+
# This method is called after extraction completes. It receives the extraction result
|
|
56
|
+
# as a hash and must return the modified hash. The processor can:
|
|
57
|
+
# - Add new keys to result["metadata"]
|
|
58
|
+
# - Transform result["content"]
|
|
59
|
+
# - Add entries to result["tables"]
|
|
60
|
+
# - Modify any other result fields
|
|
61
|
+
#
|
|
62
|
+
# Existing metadata keys will not be overwritten by the FFI bridge, so it's safe
|
|
63
|
+
# to add new keys without worrying about conflicts.
|
|
64
|
+
#
|
|
8
65
|
# @param result [Hash] Extraction result with the following structure:
|
|
66
|
+
# - "content" [String] - Extracted text content
|
|
67
|
+
# - "mime_type" [String] - MIME type of the source document
|
|
68
|
+
# - "metadata" [Hash] - Document metadata (title, author, etc.)
|
|
69
|
+
# - "tables" [Array<Hash>] - Extracted tables
|
|
70
|
+
# - "detected_languages" [Array<String>, nil] - Detected language codes
|
|
71
|
+
# - "chunks" [Array<String>, nil] - Content chunks (if chunking enabled)
|
|
72
|
+
#
|
|
9
73
|
# @return [Hash] Modified extraction result with enriched metadata
|
|
74
|
+
#
|
|
10
75
|
# @example
|
|
76
|
+
# def call(result)
|
|
77
|
+
# text = result["content"]
|
|
78
|
+
# entities = extract_entities(text)
|
|
79
|
+
# result["metadata"]["entities"] = entities
|
|
80
|
+
# result
|
|
81
|
+
# end
|
|
11
82
|
def call(result)
|
|
12
83
|
raise NotImplementedError, "#{self.class} must implement #call(result)"
|
|
13
84
|
end
|