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.
Files changed (321) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -6
  3. data/.rubocop.yaml +534 -1
  4. data/Gemfile +2 -1
  5. data/Gemfile.lock +28 -116
  6. data/README.md +269 -629
  7. data/Rakefile +0 -9
  8. data/Steepfile +4 -8
  9. data/examples/async_patterns.rb +58 -1
  10. data/ext/kreuzberg_rb/extconf.rb +5 -35
  11. data/ext/kreuzberg_rb/native/Cargo.toml +16 -55
  12. data/ext/kreuzberg_rb/native/build.rs +14 -12
  13. data/ext/kreuzberg_rb/native/include/ieeefp.h +1 -1
  14. data/ext/kreuzberg_rb/native/include/msvc_compat/strings.h +1 -1
  15. data/ext/kreuzberg_rb/native/include/strings.h +2 -2
  16. data/ext/kreuzberg_rb/native/include/unistd.h +1 -1
  17. data/ext/kreuzberg_rb/native/src/lib.rs +34 -897
  18. data/extconf.rb +6 -38
  19. data/kreuzberg.gemspec +20 -114
  20. data/lib/kreuzberg/api_proxy.rb +18 -2
  21. data/lib/kreuzberg/cache_api.rb +0 -22
  22. data/lib/kreuzberg/cli.rb +10 -2
  23. data/lib/kreuzberg/cli_proxy.rb +10 -0
  24. data/lib/kreuzberg/config.rb +22 -274
  25. data/lib/kreuzberg/errors.rb +7 -73
  26. data/lib/kreuzberg/extraction_api.rb +8 -237
  27. data/lib/kreuzberg/mcp_proxy.rb +11 -2
  28. data/lib/kreuzberg/ocr_backend_protocol.rb +73 -0
  29. data/lib/kreuzberg/post_processor_protocol.rb +71 -0
  30. data/lib/kreuzberg/result.rb +33 -151
  31. data/lib/kreuzberg/setup_lib_path.rb +2 -22
  32. data/lib/kreuzberg/validator_protocol.rb +73 -0
  33. data/lib/kreuzberg/version.rb +1 -1
  34. data/lib/kreuzberg.rb +13 -27
  35. data/pkg/kreuzberg-4.0.0.rc1.gem +0 -0
  36. data/sig/kreuzberg.rbs +12 -105
  37. data/spec/binding/cache_spec.rb +22 -22
  38. data/spec/binding/cli_proxy_spec.rb +4 -2
  39. data/spec/binding/cli_spec.rb +11 -12
  40. data/spec/binding/config_spec.rb +0 -74
  41. data/spec/binding/config_validation_spec.rb +6 -100
  42. data/spec/binding/error_handling_spec.rb +97 -283
  43. data/spec/binding/plugins/ocr_backend_spec.rb +8 -8
  44. data/spec/binding/plugins/postprocessor_spec.rb +11 -11
  45. data/spec/binding/plugins/validator_spec.rb +13 -12
  46. data/spec/examples.txt +104 -0
  47. data/spec/fixtures/config.toml +1 -0
  48. data/spec/fixtures/config.yaml +1 -0
  49. data/spec/fixtures/invalid_config.toml +1 -0
  50. data/spec/smoke/package_spec.rb +3 -2
  51. data/spec/spec_helper.rb +3 -1
  52. data/vendor/kreuzberg/Cargo.toml +67 -192
  53. data/vendor/kreuzberg/README.md +9 -97
  54. data/vendor/kreuzberg/build.rs +194 -516
  55. data/vendor/kreuzberg/src/api/handlers.rs +9 -130
  56. data/vendor/kreuzberg/src/api/mod.rs +3 -18
  57. data/vendor/kreuzberg/src/api/server.rs +71 -236
  58. data/vendor/kreuzberg/src/api/types.rs +7 -43
  59. data/vendor/kreuzberg/src/bin/profile_extract.rs +455 -0
  60. data/vendor/kreuzberg/src/cache/mod.rs +3 -27
  61. data/vendor/kreuzberg/src/chunking/mod.rs +79 -1705
  62. data/vendor/kreuzberg/src/core/batch_mode.rs +0 -60
  63. data/vendor/kreuzberg/src/core/config.rs +23 -905
  64. data/vendor/kreuzberg/src/core/extractor.rs +106 -403
  65. data/vendor/kreuzberg/src/core/io.rs +2 -4
  66. data/vendor/kreuzberg/src/core/mime.rs +12 -2
  67. data/vendor/kreuzberg/src/core/mod.rs +3 -22
  68. data/vendor/kreuzberg/src/core/pipeline.rs +78 -395
  69. data/vendor/kreuzberg/src/embeddings.rs +21 -169
  70. data/vendor/kreuzberg/src/error.rs +2 -2
  71. data/vendor/kreuzberg/src/extraction/archive.rs +31 -36
  72. data/vendor/kreuzberg/src/extraction/docx.rs +1 -365
  73. data/vendor/kreuzberg/src/extraction/email.rs +11 -12
  74. data/vendor/kreuzberg/src/extraction/excel.rs +129 -138
  75. data/vendor/kreuzberg/src/extraction/html.rs +170 -1447
  76. data/vendor/kreuzberg/src/extraction/image.rs +14 -138
  77. data/vendor/kreuzberg/src/extraction/libreoffice.rs +3 -13
  78. data/vendor/kreuzberg/src/extraction/mod.rs +5 -21
  79. data/vendor/kreuzberg/src/extraction/office_metadata/mod.rs +0 -2
  80. data/vendor/kreuzberg/src/extraction/pandoc/batch.rs +275 -0
  81. data/vendor/kreuzberg/src/extraction/pandoc/mime_types.rs +178 -0
  82. data/vendor/kreuzberg/src/extraction/pandoc/mod.rs +491 -0
  83. data/vendor/kreuzberg/src/extraction/pandoc/server.rs +496 -0
  84. data/vendor/kreuzberg/src/extraction/pandoc/subprocess.rs +1188 -0
  85. data/vendor/kreuzberg/src/extraction/pandoc/version.rs +162 -0
  86. data/vendor/kreuzberg/src/extraction/pptx.rs +94 -196
  87. data/vendor/kreuzberg/src/extraction/structured.rs +4 -5
  88. data/vendor/kreuzberg/src/extraction/table.rs +1 -2
  89. data/vendor/kreuzberg/src/extraction/text.rs +10 -18
  90. data/vendor/kreuzberg/src/extractors/archive.rs +0 -22
  91. data/vendor/kreuzberg/src/extractors/docx.rs +148 -69
  92. data/vendor/kreuzberg/src/extractors/email.rs +9 -37
  93. data/vendor/kreuzberg/src/extractors/excel.rs +40 -81
  94. data/vendor/kreuzberg/src/extractors/html.rs +173 -182
  95. data/vendor/kreuzberg/src/extractors/image.rs +8 -32
  96. data/vendor/kreuzberg/src/extractors/mod.rs +10 -171
  97. data/vendor/kreuzberg/src/extractors/pandoc.rs +201 -0
  98. data/vendor/kreuzberg/src/extractors/pdf.rs +64 -329
  99. data/vendor/kreuzberg/src/extractors/pptx.rs +34 -79
  100. data/vendor/kreuzberg/src/extractors/structured.rs +0 -16
  101. data/vendor/kreuzberg/src/extractors/text.rs +7 -30
  102. data/vendor/kreuzberg/src/extractors/xml.rs +8 -27
  103. data/vendor/kreuzberg/src/keywords/processor.rs +1 -9
  104. data/vendor/kreuzberg/src/keywords/rake.rs +1 -0
  105. data/vendor/kreuzberg/src/language_detection/mod.rs +51 -94
  106. data/vendor/kreuzberg/src/lib.rs +5 -17
  107. data/vendor/kreuzberg/src/mcp/mod.rs +1 -4
  108. data/vendor/kreuzberg/src/mcp/server.rs +21 -145
  109. data/vendor/kreuzberg/src/ocr/mod.rs +0 -2
  110. data/vendor/kreuzberg/src/ocr/processor.rs +8 -19
  111. data/vendor/kreuzberg/src/ocr/tesseract_backend.rs +0 -2
  112. data/vendor/kreuzberg/src/pdf/error.rs +1 -93
  113. data/vendor/kreuzberg/src/pdf/metadata.rs +100 -263
  114. data/vendor/kreuzberg/src/pdf/mod.rs +2 -33
  115. data/vendor/kreuzberg/src/pdf/rendering.rs +12 -12
  116. data/vendor/kreuzberg/src/pdf/table.rs +64 -61
  117. data/vendor/kreuzberg/src/pdf/text.rs +24 -416
  118. data/vendor/kreuzberg/src/plugins/extractor.rs +8 -40
  119. data/vendor/kreuzberg/src/plugins/mod.rs +0 -3
  120. data/vendor/kreuzberg/src/plugins/ocr.rs +14 -22
  121. data/vendor/kreuzberg/src/plugins/processor.rs +1 -10
  122. data/vendor/kreuzberg/src/plugins/registry.rs +0 -15
  123. data/vendor/kreuzberg/src/plugins/validator.rs +8 -20
  124. data/vendor/kreuzberg/src/stopwords/mod.rs +2 -2
  125. data/vendor/kreuzberg/src/text/mod.rs +0 -8
  126. data/vendor/kreuzberg/src/text/quality.rs +15 -28
  127. data/vendor/kreuzberg/src/text/string_utils.rs +10 -22
  128. data/vendor/kreuzberg/src/text/token_reduction/core.rs +50 -86
  129. data/vendor/kreuzberg/src/text/token_reduction/filters.rs +16 -37
  130. data/vendor/kreuzberg/src/text/token_reduction/simd_text.rs +1 -2
  131. data/vendor/kreuzberg/src/types.rs +67 -907
  132. data/vendor/kreuzberg/src/utils/mod.rs +0 -14
  133. data/vendor/kreuzberg/src/utils/quality.rs +3 -12
  134. data/vendor/kreuzberg/tests/api_tests.rs +0 -506
  135. data/vendor/kreuzberg/tests/archive_integration.rs +0 -2
  136. data/vendor/kreuzberg/tests/batch_orchestration.rs +12 -57
  137. data/vendor/kreuzberg/tests/batch_processing.rs +8 -32
  138. data/vendor/kreuzberg/tests/chunking_offset_demo.rs +92 -0
  139. data/vendor/kreuzberg/tests/concurrency_stress.rs +8 -40
  140. data/vendor/kreuzberg/tests/config_features.rs +1 -33
  141. data/vendor/kreuzberg/tests/config_loading_tests.rs +39 -16
  142. data/vendor/kreuzberg/tests/core_integration.rs +9 -35
  143. data/vendor/kreuzberg/tests/csv_integration.rs +81 -71
  144. data/vendor/kreuzberg/tests/docx_metadata_extraction_test.rs +25 -23
  145. data/vendor/kreuzberg/tests/email_integration.rs +1 -3
  146. data/vendor/kreuzberg/tests/error_handling.rs +34 -43
  147. data/vendor/kreuzberg/tests/format_integration.rs +1 -7
  148. data/vendor/kreuzberg/tests/helpers/mod.rs +0 -60
  149. data/vendor/kreuzberg/tests/image_integration.rs +0 -2
  150. data/vendor/kreuzberg/tests/mime_detection.rs +16 -17
  151. data/vendor/kreuzberg/tests/ocr_configuration.rs +0 -4
  152. data/vendor/kreuzberg/tests/ocr_errors.rs +0 -22
  153. data/vendor/kreuzberg/tests/ocr_quality.rs +0 -2
  154. data/vendor/kreuzberg/tests/pandoc_integration.rs +503 -0
  155. data/vendor/kreuzberg/tests/pdf_integration.rs +0 -2
  156. data/vendor/kreuzberg/tests/pipeline_integration.rs +2 -36
  157. data/vendor/kreuzberg/tests/plugin_ocr_backend_test.rs +0 -5
  158. data/vendor/kreuzberg/tests/plugin_postprocessor_test.rs +1 -17
  159. data/vendor/kreuzberg/tests/plugin_system.rs +0 -6
  160. data/vendor/kreuzberg/tests/registry_integration_tests.rs +22 -2
  161. data/vendor/kreuzberg/tests/security_validation.rs +1 -13
  162. data/vendor/kreuzberg/tests/test_fastembed.rs +23 -45
  163. metadata +25 -171
  164. data/.rubocop.yml +0 -543
  165. data/ext/kreuzberg_rb/native/.cargo/config.toml +0 -23
  166. data/ext/kreuzberg_rb/native/Cargo.lock +0 -7619
  167. data/lib/kreuzberg/error_context.rb +0 -136
  168. data/lib/kreuzberg/types.rb +0 -170
  169. data/lib/libpdfium.so +0 -0
  170. data/spec/binding/async_operations_spec.rb +0 -473
  171. data/spec/binding/batch_operations_spec.rb +0 -595
  172. data/spec/binding/batch_spec.rb +0 -359
  173. data/spec/binding/config_result_spec.rb +0 -377
  174. data/spec/binding/embeddings_spec.rb +0 -816
  175. data/spec/binding/error_recovery_spec.rb +0 -488
  176. data/spec/binding/font_config_spec.rb +0 -220
  177. data/spec/binding/images_spec.rb +0 -738
  178. data/spec/binding/keywords_extraction_spec.rb +0 -600
  179. data/spec/binding/metadata_types_spec.rb +0 -1228
  180. data/spec/binding/pages_extraction_spec.rb +0 -471
  181. data/spec/binding/tables_spec.rb +0 -641
  182. data/spec/unit/config/chunking_config_spec.rb +0 -213
  183. data/spec/unit/config/embedding_config_spec.rb +0 -343
  184. data/spec/unit/config/extraction_config_spec.rb +0 -438
  185. data/spec/unit/config/font_config_spec.rb +0 -285
  186. data/spec/unit/config/hierarchy_config_spec.rb +0 -314
  187. data/spec/unit/config/image_extraction_config_spec.rb +0 -209
  188. data/spec/unit/config/image_preprocessing_config_spec.rb +0 -249
  189. data/spec/unit/config/keyword_config_spec.rb +0 -229
  190. data/spec/unit/config/language_detection_config_spec.rb +0 -258
  191. data/spec/unit/config/ocr_config_spec.rb +0 -171
  192. data/spec/unit/config/page_config_spec.rb +0 -221
  193. data/spec/unit/config/pdf_config_spec.rb +0 -267
  194. data/spec/unit/config/postprocessor_config_spec.rb +0 -290
  195. data/spec/unit/config/tesseract_config_spec.rb +0 -181
  196. data/spec/unit/config/token_reduction_config_spec.rb +0 -251
  197. data/test/metadata_types_test.rb +0 -959
  198. data/vendor/Cargo.toml +0 -61
  199. data/vendor/kreuzberg/examples/bench_fixes.rs +0 -71
  200. data/vendor/kreuzberg/examples/test_pdfium_fork.rs +0 -62
  201. data/vendor/kreuzberg/src/chunking/processor.rs +0 -219
  202. data/vendor/kreuzberg/src/core/batch_optimizations.rs +0 -385
  203. data/vendor/kreuzberg/src/core/config_validation.rs +0 -949
  204. data/vendor/kreuzberg/src/core/formats.rs +0 -235
  205. data/vendor/kreuzberg/src/core/server_config.rs +0 -1220
  206. data/vendor/kreuzberg/src/extraction/capacity.rs +0 -263
  207. data/vendor/kreuzberg/src/extraction/markdown.rs +0 -216
  208. data/vendor/kreuzberg/src/extraction/office_metadata/odt_properties.rs +0 -284
  209. data/vendor/kreuzberg/src/extractors/bibtex.rs +0 -470
  210. data/vendor/kreuzberg/src/extractors/docbook.rs +0 -504
  211. data/vendor/kreuzberg/src/extractors/epub.rs +0 -696
  212. data/vendor/kreuzberg/src/extractors/fictionbook.rs +0 -492
  213. data/vendor/kreuzberg/src/extractors/jats.rs +0 -1054
  214. data/vendor/kreuzberg/src/extractors/jupyter.rs +0 -368
  215. data/vendor/kreuzberg/src/extractors/latex.rs +0 -653
  216. data/vendor/kreuzberg/src/extractors/markdown.rs +0 -701
  217. data/vendor/kreuzberg/src/extractors/odt.rs +0 -628
  218. data/vendor/kreuzberg/src/extractors/opml.rs +0 -635
  219. data/vendor/kreuzberg/src/extractors/orgmode.rs +0 -529
  220. data/vendor/kreuzberg/src/extractors/rst.rs +0 -577
  221. data/vendor/kreuzberg/src/extractors/rtf.rs +0 -809
  222. data/vendor/kreuzberg/src/extractors/security.rs +0 -484
  223. data/vendor/kreuzberg/src/extractors/security_tests.rs +0 -367
  224. data/vendor/kreuzberg/src/extractors/typst.rs +0 -651
  225. data/vendor/kreuzberg/src/language_detection/processor.rs +0 -218
  226. data/vendor/kreuzberg/src/ocr/language_registry.rs +0 -520
  227. data/vendor/kreuzberg/src/panic_context.rs +0 -154
  228. data/vendor/kreuzberg/src/pdf/bindings.rs +0 -306
  229. data/vendor/kreuzberg/src/pdf/bundled.rs +0 -408
  230. data/vendor/kreuzberg/src/pdf/fonts.rs +0 -358
  231. data/vendor/kreuzberg/src/pdf/hierarchy.rs +0 -903
  232. data/vendor/kreuzberg/src/text/quality_processor.rs +0 -231
  233. data/vendor/kreuzberg/src/text/utf8_validation.rs +0 -193
  234. data/vendor/kreuzberg/src/utils/pool.rs +0 -503
  235. data/vendor/kreuzberg/src/utils/pool_sizing.rs +0 -364
  236. data/vendor/kreuzberg/src/utils/string_pool.rs +0 -761
  237. data/vendor/kreuzberg/tests/api_embed.rs +0 -360
  238. data/vendor/kreuzberg/tests/api_extract_multipart.rs +0 -52
  239. data/vendor/kreuzberg/tests/api_large_pdf_extraction.rs +0 -471
  240. data/vendor/kreuzberg/tests/api_large_pdf_extraction_diagnostics.rs +0 -289
  241. data/vendor/kreuzberg/tests/batch_pooling_benchmark.rs +0 -154
  242. data/vendor/kreuzberg/tests/bibtex_parity_test.rs +0 -421
  243. data/vendor/kreuzberg/tests/config_integration_test.rs +0 -753
  244. data/vendor/kreuzberg/tests/data/hierarchy_ground_truth.json +0 -294
  245. data/vendor/kreuzberg/tests/docbook_extractor_tests.rs +0 -500
  246. data/vendor/kreuzberg/tests/docx_vs_pandoc_comparison.rs +0 -370
  247. data/vendor/kreuzberg/tests/epub_native_extractor_tests.rs +0 -275
  248. data/vendor/kreuzberg/tests/fictionbook_extractor_tests.rs +0 -228
  249. data/vendor/kreuzberg/tests/html_table_test.rs +0 -551
  250. data/vendor/kreuzberg/tests/instrumentation_test.rs +0 -139
  251. data/vendor/kreuzberg/tests/jats_extractor_tests.rs +0 -639
  252. data/vendor/kreuzberg/tests/jupyter_extractor_tests.rs +0 -704
  253. data/vendor/kreuzberg/tests/latex_extractor_tests.rs +0 -496
  254. data/vendor/kreuzberg/tests/markdown_extractor_tests.rs +0 -490
  255. data/vendor/kreuzberg/tests/ocr_language_registry.rs +0 -191
  256. data/vendor/kreuzberg/tests/odt_extractor_tests.rs +0 -674
  257. data/vendor/kreuzberg/tests/opml_extractor_tests.rs +0 -616
  258. data/vendor/kreuzberg/tests/orgmode_extractor_tests.rs +0 -822
  259. data/vendor/kreuzberg/tests/page_markers.rs +0 -297
  260. data/vendor/kreuzberg/tests/pdf_hierarchy_detection.rs +0 -301
  261. data/vendor/kreuzberg/tests/pdf_hierarchy_quality.rs +0 -589
  262. data/vendor/kreuzberg/tests/pdf_ocr_triggering.rs +0 -301
  263. data/vendor/kreuzberg/tests/pdf_text_merging.rs +0 -475
  264. data/vendor/kreuzberg/tests/pdfium_linking.rs +0 -340
  265. data/vendor/kreuzberg/tests/rst_extractor_tests.rs +0 -694
  266. data/vendor/kreuzberg/tests/rtf_extractor_tests.rs +0 -775
  267. data/vendor/kreuzberg/tests/typst_behavioral_tests.rs +0 -1260
  268. data/vendor/kreuzberg/tests/typst_extractor_tests.rs +0 -648
  269. data/vendor/kreuzberg-ffi/Cargo.toml +0 -67
  270. data/vendor/kreuzberg-ffi/README.md +0 -851
  271. data/vendor/kreuzberg-ffi/benches/result_view_benchmark.rs +0 -227
  272. data/vendor/kreuzberg-ffi/build.rs +0 -168
  273. data/vendor/kreuzberg-ffi/cbindgen.toml +0 -37
  274. data/vendor/kreuzberg-ffi/kreuzberg-ffi.pc.in +0 -12
  275. data/vendor/kreuzberg-ffi/kreuzberg.h +0 -3012
  276. data/vendor/kreuzberg-ffi/src/batch_streaming.rs +0 -588
  277. data/vendor/kreuzberg-ffi/src/config.rs +0 -1341
  278. data/vendor/kreuzberg-ffi/src/error.rs +0 -901
  279. data/vendor/kreuzberg-ffi/src/extraction.rs +0 -555
  280. data/vendor/kreuzberg-ffi/src/helpers.rs +0 -879
  281. data/vendor/kreuzberg-ffi/src/lib.rs +0 -977
  282. data/vendor/kreuzberg-ffi/src/memory.rs +0 -493
  283. data/vendor/kreuzberg-ffi/src/mime.rs +0 -329
  284. data/vendor/kreuzberg-ffi/src/panic_shield.rs +0 -265
  285. data/vendor/kreuzberg-ffi/src/plugins/document_extractor.rs +0 -442
  286. data/vendor/kreuzberg-ffi/src/plugins/mod.rs +0 -14
  287. data/vendor/kreuzberg-ffi/src/plugins/ocr_backend.rs +0 -628
  288. data/vendor/kreuzberg-ffi/src/plugins/post_processor.rs +0 -438
  289. data/vendor/kreuzberg-ffi/src/plugins/validator.rs +0 -329
  290. data/vendor/kreuzberg-ffi/src/result.rs +0 -510
  291. data/vendor/kreuzberg-ffi/src/result_pool.rs +0 -639
  292. data/vendor/kreuzberg-ffi/src/result_view.rs +0 -773
  293. data/vendor/kreuzberg-ffi/src/string_intern.rs +0 -568
  294. data/vendor/kreuzberg-ffi/src/types.rs +0 -363
  295. data/vendor/kreuzberg-ffi/src/util.rs +0 -210
  296. data/vendor/kreuzberg-ffi/src/validation.rs +0 -848
  297. data/vendor/kreuzberg-ffi/tests.disabled/README.md +0 -48
  298. data/vendor/kreuzberg-ffi/tests.disabled/config_loading_tests.rs +0 -299
  299. data/vendor/kreuzberg-ffi/tests.disabled/config_tests.rs +0 -346
  300. data/vendor/kreuzberg-ffi/tests.disabled/extractor_tests.rs +0 -232
  301. data/vendor/kreuzberg-ffi/tests.disabled/plugin_registration_tests.rs +0 -470
  302. data/vendor/kreuzberg-tesseract/.commitlintrc.json +0 -13
  303. data/vendor/kreuzberg-tesseract/.crate-ignore +0 -2
  304. data/vendor/kreuzberg-tesseract/Cargo.lock +0 -2933
  305. data/vendor/kreuzberg-tesseract/Cargo.toml +0 -57
  306. data/vendor/kreuzberg-tesseract/LICENSE +0 -22
  307. data/vendor/kreuzberg-tesseract/README.md +0 -399
  308. data/vendor/kreuzberg-tesseract/build.rs +0 -1127
  309. data/vendor/kreuzberg-tesseract/patches/README.md +0 -71
  310. data/vendor/kreuzberg-tesseract/patches/tesseract.diff +0 -199
  311. data/vendor/kreuzberg-tesseract/src/api.rs +0 -1371
  312. data/vendor/kreuzberg-tesseract/src/choice_iterator.rs +0 -77
  313. data/vendor/kreuzberg-tesseract/src/enums.rs +0 -297
  314. data/vendor/kreuzberg-tesseract/src/error.rs +0 -81
  315. data/vendor/kreuzberg-tesseract/src/lib.rs +0 -145
  316. data/vendor/kreuzberg-tesseract/src/monitor.rs +0 -57
  317. data/vendor/kreuzberg-tesseract/src/mutable_iterator.rs +0 -197
  318. data/vendor/kreuzberg-tesseract/src/page_iterator.rs +0 -253
  319. data/vendor/kreuzberg-tesseract/src/result_iterator.rs +0 -286
  320. data/vendor/kreuzberg-tesseract/src/result_renderer.rs +0 -183
  321. data/vendor/kreuzberg-tesseract/tests/integration_test.rs +0 -211
@@ -1,1054 +0,0 @@
1
- //! JATS (Journal Article Tag Suite) document extractor.
2
- //!
3
- //! This extractor handles JATS XML documents, the standard format for scientific journal articles
4
- //! used by PubMed Central and major academic publishers.
5
- //!
6
- //! It extracts:
7
- //! - Rich metadata (title, subtitle, authors with affiliations, DOI, PII, keywords, dates)
8
- //! - Article abstract (regular and graphical)
9
- //! - Section hierarchy and content (intro, methods, results, discussion)
10
- //! - Paragraphs and text content
11
- //! - Tables with captions
12
- //! - Figures with captions
13
- //! - Citations and references
14
- //! - Supplementary material information
15
-
16
- use crate::Result;
17
- use crate::core::config::ExtractionConfig;
18
- use crate::extraction::cells_to_markdown;
19
- use crate::plugins::{DocumentExtractor, Plugin};
20
- use crate::types::{ExtractionResult, Metadata, Table};
21
- use async_trait::async_trait;
22
- use quick_xml::Reader;
23
- use quick_xml::events::Event;
24
- #[cfg(feature = "tokio-runtime")]
25
- use std::path::Path;
26
-
27
- /// JATS document extractor.
28
- ///
29
- /// Supports JATS (Journal Article Tag Suite) XML documents in various versions,
30
- /// handling both the full article structure and minimal JATS subsets.
31
- pub struct JatsExtractor;
32
-
33
- impl Default for JatsExtractor {
34
- fn default() -> Self {
35
- Self::new()
36
- }
37
- }
38
-
39
- impl JatsExtractor {
40
- pub fn new() -> Self {
41
- Self
42
- }
43
- }
44
-
45
- /// Extract text content from a JATS element and its children.
46
- fn extract_text_content(reader: &mut Reader<&[u8]>) -> Result<String> {
47
- let mut text = String::new();
48
- let mut depth = 0;
49
-
50
- loop {
51
- match reader.read_event() {
52
- Ok(Event::Start(_)) => {
53
- depth += 1;
54
- }
55
- Ok(Event::End(_)) => {
56
- if depth == 0 {
57
- break;
58
- }
59
- depth -= 1;
60
- if !text.is_empty() && !text.ends_with('\n') {
61
- text.push(' ');
62
- }
63
- }
64
- Ok(Event::Text(t)) => {
65
- let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
66
- if !decoded.trim().is_empty() {
67
- text.push_str(&decoded);
68
- text.push(' ');
69
- }
70
- }
71
- Ok(Event::CData(t)) => {
72
- let decoded = std::str::from_utf8(t.as_ref()).unwrap_or("").to_string();
73
- if !decoded.trim().is_empty() {
74
- text.push_str(&decoded);
75
- text.push('\n');
76
- }
77
- }
78
- Ok(Event::Eof) => break,
79
- Err(e) => {
80
- return Err(crate::error::KreuzbergError::parsing(format!(
81
- "XML parsing error: {}",
82
- e
83
- )));
84
- }
85
- _ => {}
86
- }
87
- }
88
-
89
- Ok(text.trim().to_string())
90
- }
91
-
92
- /// Structure to hold extracted JATS metadata.
93
- #[derive(Debug, Clone, Default)]
94
- struct JatsMetadataExtracted {
95
- title: String,
96
- subtitle: Option<String>,
97
- authors: Vec<String>,
98
- affiliations: Vec<String>,
99
- doi: Option<String>,
100
- pii: Option<String>,
101
- keywords: Vec<String>,
102
- publication_date: Option<String>,
103
- volume: Option<String>,
104
- issue: Option<String>,
105
- pages: Option<String>,
106
- journal_title: Option<String>,
107
- article_type: Option<String>,
108
- abstract_text: Option<String>,
109
- corresponding_author: Option<String>,
110
- }
111
-
112
- /// Extract all content in a single optimized pass.
113
- /// Combines metadata extraction, content parsing, and table extraction into one pass.
114
- fn extract_jats_all_in_one(content: &str) -> Result<(JatsMetadataExtracted, String, String, Vec<Table>)> {
115
- let mut reader = Reader::from_str(content);
116
- let mut metadata = JatsMetadataExtracted::default();
117
- let mut body_content = String::new();
118
- let mut title = String::new();
119
-
120
- let mut in_article_meta = false;
121
- let mut in_article_title = false;
122
- let mut in_subtitle = false;
123
- let mut in_contrib = false;
124
- let mut in_name = false;
125
- let mut in_aff = false;
126
- let mut in_abstract = false;
127
- let mut in_kwd_group = false;
128
- let mut in_kwd = false;
129
- let mut current_author = String::new();
130
- let mut current_aff = String::new();
131
- let mut abstract_content = String::new();
132
-
133
- let mut in_body = false;
134
- let mut in_section = false;
135
- let mut in_para = false;
136
-
137
- let mut in_table = false;
138
- let mut in_thead = false;
139
- let mut in_tbody = false;
140
- let mut in_row = false;
141
- let mut current_table: Vec<Vec<String>> = Vec::new();
142
- let mut current_row: Vec<String> = Vec::new();
143
- let mut tables = Vec::new();
144
- let mut table_index = 0;
145
-
146
- loop {
147
- match reader.read_event() {
148
- Ok(Event::Start(e)) => {
149
- let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
150
-
151
- match tag.as_str() {
152
- "article" => {
153
- for attr in e.attributes() {
154
- if let Ok(attr) = attr
155
- && String::from_utf8_lossy(attr.key.as_ref()) == "article-type"
156
- {
157
- metadata.article_type = Some(String::from_utf8_lossy(attr.value.as_ref()).to_string());
158
- }
159
- }
160
- }
161
- "article-meta" => {
162
- in_article_meta = true;
163
- }
164
- "article-title" if in_article_meta => {
165
- in_article_title = true;
166
- }
167
- "subtitle" if in_article_meta => {
168
- in_subtitle = true;
169
- }
170
- "contrib" if in_article_meta => {
171
- in_contrib = true;
172
- current_author.clear();
173
- }
174
- "name" if in_contrib => {
175
- in_name = true;
176
- }
177
- "aff" if in_article_meta => {
178
- in_aff = true;
179
- current_aff.clear();
180
- }
181
- "article-id" if in_article_meta => {
182
- let mut id_type = String::new();
183
- for attr in e.attributes() {
184
- if let Ok(attr) = attr
185
- && String::from_utf8_lossy(attr.key.as_ref()) == "pub-id-type"
186
- {
187
- id_type = String::from_utf8_lossy(attr.value.as_ref()).to_string();
188
- }
189
- }
190
-
191
- let id_text = extract_text_content(&mut reader)?;
192
- match id_type.as_str() {
193
- "doi" => metadata.doi = Some(id_text),
194
- "pii" => metadata.pii = Some(id_text),
195
- _ => {}
196
- }
197
- continue;
198
- }
199
- "volume" if in_article_meta => {
200
- let vol_text = extract_text_content(&mut reader)?;
201
- metadata.volume = Some(vol_text);
202
- continue;
203
- }
204
- "issue" if in_article_meta => {
205
- let issue_text = extract_text_content(&mut reader)?;
206
- metadata.issue = Some(issue_text);
207
- continue;
208
- }
209
- "fpage" | "lpage" if in_article_meta => {
210
- let page_text = extract_text_content(&mut reader)?;
211
- if let Some(pages) = &mut metadata.pages {
212
- pages.push('-');
213
- pages.push_str(&page_text);
214
- } else {
215
- metadata.pages = Some(page_text);
216
- }
217
- continue;
218
- }
219
- "pub-date" if in_article_meta => {
220
- let date_text = extract_text_content(&mut reader)?;
221
- if metadata.publication_date.is_none() {
222
- metadata.publication_date = Some(date_text);
223
- }
224
- continue;
225
- }
226
- "journal-title" if in_article_meta => {
227
- let journal_text = extract_text_content(&mut reader)?;
228
- if metadata.journal_title.is_none() {
229
- metadata.journal_title = Some(journal_text);
230
- }
231
- continue;
232
- }
233
- "abstract" if in_article_meta => {
234
- in_abstract = true;
235
- abstract_content.clear();
236
- }
237
- "kwd-group" if in_article_meta => {
238
- in_kwd_group = true;
239
- }
240
- "kwd" if in_kwd_group => {
241
- in_kwd = true;
242
- }
243
- "corresp" if in_article_meta => {
244
- let corresp_text = extract_text_content(&mut reader)?;
245
- metadata.corresponding_author = Some(corresp_text);
246
- continue;
247
- }
248
- "body" => {
249
- in_body = true;
250
- }
251
- "sec" if in_body => {
252
- in_section = true;
253
- }
254
- "title" if (in_section || in_body) && !in_article_title => {
255
- let section_title = extract_text_content(&mut reader)?;
256
- if !section_title.is_empty() {
257
- body_content.push_str("## ");
258
- body_content.push_str(&section_title);
259
- body_content.push_str("\n\n");
260
- }
261
- continue;
262
- }
263
- "p" if in_body || in_section => {
264
- in_para = true;
265
- }
266
- "table" => {
267
- in_table = true;
268
- current_table.clear();
269
- }
270
- "thead" if in_table => {
271
- in_thead = true;
272
- }
273
- "tbody" if in_table => {
274
- in_tbody = true;
275
- }
276
- "tr" if (in_thead || in_tbody) && in_table => {
277
- in_row = true;
278
- current_row.clear();
279
- }
280
- "td" | "th" if in_row => {
281
- let mut cell_text = String::new();
282
- let mut cell_depth = 0;
283
-
284
- loop {
285
- match reader.read_event() {
286
- Ok(Event::Start(_)) => {
287
- cell_depth += 1;
288
- }
289
- Ok(Event::End(e)) => {
290
- let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
291
- if (tag == "td" || tag == "th") && cell_depth == 0 {
292
- break;
293
- }
294
- if cell_depth > 0 {
295
- cell_depth -= 1;
296
- }
297
- }
298
- Ok(Event::Text(t)) => {
299
- let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
300
- if !decoded.trim().is_empty() {
301
- if !cell_text.is_empty() {
302
- cell_text.push(' ');
303
- }
304
- cell_text.push_str(decoded.trim());
305
- }
306
- }
307
- Ok(Event::Eof) => break,
308
- Err(e) => {
309
- return Err(crate::error::KreuzbergError::parsing(format!(
310
- "XML parsing error: {}",
311
- e
312
- )));
313
- }
314
- _ => {}
315
- }
316
- }
317
-
318
- current_row.push(cell_text);
319
- }
320
- _ => {}
321
- }
322
- }
323
- Ok(Event::End(e)) => {
324
- let tag = String::from_utf8_lossy(e.name().as_ref()).to_string();
325
-
326
- match tag.as_str() {
327
- "article-meta" => {
328
- in_article_meta = false;
329
- }
330
- "article-title" if in_article_title => {
331
- in_article_title = false;
332
- }
333
- "subtitle" if in_subtitle => {
334
- in_subtitle = false;
335
- }
336
- "contrib" if in_contrib => {
337
- if !current_author.is_empty() {
338
- metadata.authors.push(current_author.clone());
339
- }
340
- in_contrib = false;
341
- current_author.clear();
342
- }
343
- "name" if in_name => {
344
- in_name = false;
345
- }
346
- "aff" if in_aff => {
347
- if !current_aff.is_empty() {
348
- metadata.affiliations.push(current_aff.clone());
349
- }
350
- in_aff = false;
351
- current_aff.clear();
352
- }
353
- "abstract" if in_abstract => {
354
- in_abstract = false;
355
- metadata.abstract_text = Some(abstract_content.trim().to_string());
356
- }
357
- "kwd-group" if in_kwd_group => {
358
- in_kwd_group = false;
359
- }
360
- "kwd" if in_kwd => {
361
- in_kwd = false;
362
- }
363
- "body" => {
364
- in_body = false;
365
- }
366
- "sec" if in_section => {
367
- in_section = false;
368
- }
369
- "p" if in_para => {
370
- in_para = false;
371
- }
372
- "table" if in_table => {
373
- if !current_table.is_empty() {
374
- let markdown = cells_to_markdown(&current_table);
375
- tables.push(Table {
376
- cells: current_table.clone(),
377
- markdown,
378
- page_number: table_index + 1,
379
- });
380
- table_index += 1;
381
- current_table.clear();
382
- }
383
- in_table = false;
384
- }
385
- "thead" if in_thead => {
386
- in_thead = false;
387
- }
388
- "tbody" if in_tbody => {
389
- in_tbody = false;
390
- }
391
- "tr" if in_row => {
392
- if !current_row.is_empty() {
393
- current_table.push(current_row.clone());
394
- current_row.clear();
395
- }
396
- in_row = false;
397
- }
398
- _ => {}
399
- }
400
- }
401
- Ok(Event::Text(t)) => {
402
- let decoded = String::from_utf8_lossy(t.as_ref()).to_string();
403
- let trimmed = decoded.trim();
404
-
405
- if !trimmed.is_empty() {
406
- if in_article_title && metadata.title.is_empty() {
407
- metadata.title.push_str(trimmed);
408
- } else if in_subtitle && metadata.subtitle.is_none() {
409
- metadata.subtitle = Some(trimmed.to_string());
410
- } else if in_name {
411
- if !current_author.is_empty() {
412
- current_author.push(' ');
413
- }
414
- current_author.push_str(trimmed);
415
- } else if in_aff {
416
- if !current_aff.is_empty() {
417
- current_aff.push(' ');
418
- }
419
- current_aff.push_str(trimmed);
420
- } else if in_abstract {
421
- if !abstract_content.is_empty() {
422
- abstract_content.push(' ');
423
- }
424
- abstract_content.push_str(trimmed);
425
- } else if in_kwd {
426
- metadata.keywords.push(trimmed.to_string());
427
- } else if in_para && in_body {
428
- body_content.push_str(trimmed);
429
- body_content.push_str("\n\n");
430
- }
431
- }
432
- }
433
- Ok(Event::Eof) => break,
434
- Err(e) => {
435
- return Err(crate::error::KreuzbergError::parsing(format!(
436
- "XML parsing error: {}",
437
- e
438
- )));
439
- }
440
- _ => {}
441
- }
442
- }
443
-
444
- let mut final_output = body_content;
445
- if !metadata.title.is_empty() {
446
- final_output = format!("# {}\n\n{}", metadata.title, final_output);
447
- title = metadata.title.clone();
448
- }
449
-
450
- Ok((metadata, final_output.trim().to_string(), title, tables))
451
- }
452
-
453
- impl Plugin for JatsExtractor {
454
- fn name(&self) -> &str {
455
- "jats-extractor"
456
- }
457
-
458
- fn version(&self) -> String {
459
- env!("CARGO_PKG_VERSION").to_string()
460
- }
461
-
462
- fn initialize(&self) -> Result<()> {
463
- Ok(())
464
- }
465
-
466
- fn shutdown(&self) -> Result<()> {
467
- Ok(())
468
- }
469
- }
470
-
471
- #[async_trait]
472
- impl DocumentExtractor for JatsExtractor {
473
- #[cfg_attr(
474
- feature = "otel",
475
- tracing::instrument(
476
- skip(self, content, config),
477
- fields(
478
- extractor.name = self.name(),
479
- content.size_bytes = content.len(),
480
- )
481
- )
482
- )]
483
- async fn extract_bytes(
484
- &self,
485
- content: &[u8],
486
- mime_type: &str,
487
- config: &ExtractionConfig,
488
- ) -> Result<ExtractionResult> {
489
- let _ = config;
490
- let jats_content = std::str::from_utf8(content)
491
- .map(|s| s.to_string())
492
- .unwrap_or_else(|_| String::from_utf8_lossy(content).to_string());
493
-
494
- let (jats_metadata, extracted_content, _title, tables) = extract_jats_all_in_one(&jats_content)?;
495
-
496
- let mut metadata = Metadata::default();
497
- let mut subject_parts = Vec::new();
498
-
499
- if !jats_metadata.title.is_empty() {
500
- metadata.subject = Some(jats_metadata.title.clone());
501
- subject_parts.push(format!("Title: {}", jats_metadata.title));
502
- }
503
-
504
- if let Some(subtitle) = &jats_metadata.subtitle {
505
- subject_parts.push(format!("Subtitle: {}", subtitle));
506
- }
507
-
508
- if !jats_metadata.authors.is_empty() {
509
- subject_parts.push(format!("Authors: {}", jats_metadata.authors.join("; ")));
510
- }
511
-
512
- if !jats_metadata.affiliations.is_empty() {
513
- subject_parts.push(format!("Affiliations: {}", jats_metadata.affiliations.join("; ")));
514
- }
515
-
516
- if let Some(doi) = &jats_metadata.doi {
517
- subject_parts.push(format!("DOI: {}", doi));
518
- }
519
-
520
- if let Some(pii) = &jats_metadata.pii {
521
- subject_parts.push(format!("PII: {}", pii));
522
- }
523
-
524
- if !jats_metadata.keywords.is_empty() {
525
- subject_parts.push(format!("Keywords: {}", jats_metadata.keywords.join("; ")));
526
- }
527
-
528
- if let Some(date) = &jats_metadata.publication_date {
529
- metadata.created_at = Some(date.clone());
530
- subject_parts.push(format!("Publication Date: {}", date));
531
- }
532
-
533
- if let Some(volume) = &jats_metadata.volume {
534
- subject_parts.push(format!("Volume: {}", volume));
535
- }
536
-
537
- if let Some(issue) = &jats_metadata.issue {
538
- subject_parts.push(format!("Issue: {}", issue));
539
- }
540
-
541
- if let Some(pages) = &jats_metadata.pages {
542
- subject_parts.push(format!("Pages: {}", pages));
543
- }
544
-
545
- if let Some(journal_title) = &jats_metadata.journal_title {
546
- subject_parts.push(format!("Journal: {}", journal_title));
547
- }
548
-
549
- if let Some(article_type) = &jats_metadata.article_type {
550
- subject_parts.push(format!("Article Type: {}", article_type));
551
- }
552
-
553
- if let Some(abstract_text) = &jats_metadata.abstract_text {
554
- subject_parts.push(format!("Abstract: {}", abstract_text));
555
- }
556
-
557
- if let Some(corresp_author) = &jats_metadata.corresponding_author {
558
- subject_parts.push(format!("Corresponding Author: {}", corresp_author));
559
- }
560
-
561
- if !subject_parts.is_empty() {
562
- metadata.subject = Some(subject_parts.join(" | "));
563
- }
564
-
565
- Ok(ExtractionResult {
566
- content: extracted_content,
567
- mime_type: mime_type.to_string(),
568
- metadata,
569
- tables,
570
- detected_languages: None,
571
- chunks: None,
572
- images: None,
573
- pages: None,
574
- })
575
- }
576
-
577
- #[cfg(feature = "tokio-runtime")]
578
- #[cfg_attr(
579
- feature = "otel",
580
- tracing::instrument(
581
- skip(self, path, config),
582
- fields(
583
- extractor.name = self.name(),
584
- )
585
- )
586
- )]
587
- #[cfg(feature = "tokio-runtime")]
588
- async fn extract_file(&self, path: &Path, mime_type: &str, config: &ExtractionConfig) -> Result<ExtractionResult> {
589
- let bytes = tokio::fs::read(path).await?;
590
- self.extract_bytes(&bytes, mime_type, config).await
591
- }
592
-
593
- fn supported_mime_types(&self) -> &[&str] {
594
- &["application/x-jats+xml", "text/jats"]
595
- }
596
-
597
- fn priority(&self) -> i32 {
598
- 50
599
- }
600
- }
601
-
602
- #[cfg(test)]
603
- mod tests {
604
- use super::*;
605
-
606
- #[test]
607
- fn test_jats_extractor_plugin_interface() {
608
- let extractor = JatsExtractor::new();
609
- assert_eq!(extractor.name(), "jats-extractor");
610
- assert!(extractor.initialize().is_ok());
611
- assert!(extractor.shutdown().is_ok());
612
- }
613
-
614
- #[test]
615
- fn test_jats_extractor_supported_mime_types() {
616
- let extractor = JatsExtractor::new();
617
- let mime_types = extractor.supported_mime_types();
618
- assert_eq!(mime_types.len(), 2);
619
- assert!(mime_types.contains(&"application/x-jats+xml"));
620
- assert!(mime_types.contains(&"text/jats"));
621
- }
622
-
623
- #[test]
624
- fn test_jats_extractor_priority() {
625
- let extractor = JatsExtractor::new();
626
- assert_eq!(extractor.priority(), 50);
627
- }
628
-
629
- #[test]
630
- fn test_parse_simple_jats_article() {
631
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
632
- <article>
633
- <front>
634
- <article-meta>
635
- <article-title>Test Article Title</article-title>
636
- </article-meta>
637
- </front>
638
- <body>
639
- <p>Test content paragraph.</p>
640
- </body>
641
- </article>"#;
642
-
643
- let (metadata, content, title, _tables) = extract_jats_all_in_one(jats).expect("Parse failed");
644
- assert_eq!(title, "Test Article Title");
645
- assert_eq!(metadata.title, "Test Article Title");
646
- assert!(content.contains("Test Article Title"));
647
- assert!(content.contains("Test content"));
648
- }
649
-
650
- #[test]
651
- fn test_extract_jats_title() {
652
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
653
- <article>
654
- <front>
655
- <article-meta>
656
- <article-title>Effects of Caffeine on Human Health</article-title>
657
- </article-meta>
658
- </front>
659
- </article>"#;
660
-
661
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
662
- assert_eq!(metadata.title, "Effects of Caffeine on Human Health");
663
- }
664
-
665
- #[test]
666
- fn test_extract_jats_subtitle() {
667
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
668
- <article>
669
- <front>
670
- <article-meta>
671
- <article-title>Main Title</article-title>
672
- <subtitle>A Systematic Review</subtitle>
673
- </article-meta>
674
- </front>
675
- </article>"#;
676
-
677
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
678
- assert_eq!(metadata.title, "Main Title");
679
- assert_eq!(metadata.subtitle, Some("A Systematic Review".to_string()));
680
- }
681
-
682
- #[test]
683
- fn test_extract_jats_authors() {
684
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
685
- <article>
686
- <front>
687
- <article-meta>
688
- <contrib-group>
689
- <contrib contrib-type="author">
690
- <name>
691
- <surname>Smith</surname>
692
- <given-names>John A.</given-names>
693
- </name>
694
- </contrib>
695
- <contrib contrib-type="author">
696
- <name>
697
- <surname>Johnson</surname>
698
- <given-names>Jane B.</given-names>
699
- </name>
700
- </contrib>
701
- </contrib-group>
702
- </article-meta>
703
- </front>
704
- </article>"#;
705
-
706
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
707
- assert_eq!(metadata.authors.len(), 2);
708
- assert!(metadata.authors[0].contains("Smith"));
709
- assert!(metadata.authors[1].contains("Johnson"));
710
- }
711
-
712
- #[test]
713
- fn test_extract_jats_affiliations() {
714
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
715
- <article>
716
- <front>
717
- <article-meta>
718
- <aff id="aff1">Department of Medicine, Harvard University, Cambridge, MA</aff>
719
- <aff id="aff2">Center for Health Research, Boston Medical Center, Boston, MA</aff>
720
- </article-meta>
721
- </front>
722
- </article>"#;
723
-
724
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
725
- assert_eq!(metadata.affiliations.len(), 2);
726
- assert!(metadata.affiliations[0].contains("Harvard"));
727
- assert!(metadata.affiliations[1].contains("Boston"));
728
- }
729
-
730
- #[test]
731
- fn test_extract_jats_doi_and_pii() {
732
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
733
- <article>
734
- <front>
735
- <article-meta>
736
- <article-id pub-id-type="doi">10.1371/journal.pmed.0020124</article-id>
737
- <article-id pub-id-type="pii">05-PLME-RA-0071R2</article-id>
738
- </article-meta>
739
- </front>
740
- </article>"#;
741
-
742
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
743
- assert_eq!(metadata.doi, Some("10.1371/journal.pmed.0020124".to_string()));
744
- assert_eq!(metadata.pii, Some("05-PLME-RA-0071R2".to_string()));
745
- }
746
-
747
- #[test]
748
- fn test_extract_jats_keywords() {
749
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
750
- <article>
751
- <front>
752
- <article-meta>
753
- <kwd-group>
754
- <kwd>caffeine</kwd>
755
- <kwd>meta-analysis</kwd>
756
- <kwd>systematic review</kwd>
757
- </kwd-group>
758
- </article-meta>
759
- </front>
760
- </article>"#;
761
-
762
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
763
- assert_eq!(metadata.keywords.len(), 3);
764
- assert!(metadata.keywords.contains(&"caffeine".to_string()));
765
- assert!(metadata.keywords.contains(&"meta-analysis".to_string()));
766
- }
767
-
768
- #[test]
769
- fn test_extract_jats_publication_info() {
770
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
771
- <article>
772
- <front>
773
- <article-meta>
774
- <pub-date pub-type="epub">
775
- <day>18</day>
776
- <month>04</month>
777
- <year>2005</year>
778
- </pub-date>
779
- <volume>2</volume>
780
- <issue>4</issue>
781
- <fpage>e124</fpage>
782
- <lpage>e132</lpage>
783
- </article-meta>
784
- </front>
785
- </article>"#;
786
-
787
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
788
- assert!(metadata.publication_date.is_some());
789
- assert_eq!(metadata.volume, Some("2".to_string()));
790
- assert_eq!(metadata.issue, Some("4".to_string()));
791
- assert!(metadata.pages.is_some());
792
- }
793
-
794
- #[test]
795
- fn test_extract_jats_abstract() {
796
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
797
- <article>
798
- <front>
799
- <article-meta>
800
- <abstract>
801
- <sec>
802
- <title>Background</title>
803
- <p>This is the background information of the study.</p>
804
- </sec>
805
- <sec>
806
- <title>Methods</title>
807
- <p>We used quantitative analysis to evaluate the hypothesis.</p>
808
- </sec>
809
- </abstract>
810
- </article-meta>
811
- </front>
812
- </article>"#;
813
-
814
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
815
- assert!(metadata.abstract_text.is_some());
816
- let abstract_text = metadata.abstract_text.unwrap();
817
- assert!(abstract_text.contains("background"));
818
- assert!(abstract_text.contains("quantitative"));
819
- }
820
-
821
- #[test]
822
- fn test_extract_jats_tables_basic() {
823
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
824
- <article>
825
- <body>
826
- <table-wrap id="tbl1">
827
- <table>
828
- <thead>
829
- <tr>
830
- <th>Study</th>
831
- <th>Year</th>
832
- </tr>
833
- </thead>
834
- <tbody>
835
- <tr>
836
- <td>Study A</td>
837
- <td>2003</td>
838
- </tr>
839
- <tr>
840
- <td>Study B</td>
841
- <td>2004</td>
842
- </tr>
843
- </tbody>
844
- </table>
845
- </table-wrap>
846
- </body>
847
- </article>"#;
848
-
849
- let (_metadata, _content, _title, tables) = extract_jats_all_in_one(jats).expect("Table extraction failed");
850
- assert_eq!(tables.len(), 1);
851
- assert_eq!(tables[0].cells.len(), 3);
852
- }
853
-
854
- #[test]
855
- fn test_extract_jats_corresponding_author() {
856
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
857
- <article>
858
- <front>
859
- <article-meta>
860
- <author-notes>
861
- <corresp id="cor1">To whom correspondence should be addressed. E-mail: rwilliams@yale.edu</corresp>
862
- </author-notes>
863
- </article-meta>
864
- </front>
865
- </article>"#;
866
-
867
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
868
- assert!(metadata.corresponding_author.is_some());
869
- let corresp = metadata.corresponding_author.unwrap();
870
- assert!(corresp.contains("rwilliams"));
871
- }
872
-
873
- #[test]
874
- fn test_extract_jats_section_hierarchy() {
875
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
876
- <article>
877
- <front>
878
- <article-meta>
879
- <article-title>Article Title</article-title>
880
- </article-meta>
881
- </front>
882
- <body>
883
- <sec id="s1">
884
- <title>Introduction</title>
885
- <p>Intro content.</p>
886
- </sec>
887
- <sec id="s2">
888
- <title>Methods</title>
889
- <p>Methods content.</p>
890
- </sec>
891
- <sec id="s3">
892
- <title>Results</title>
893
- <p>Results content.</p>
894
- </sec>
895
- </body>
896
- </article>"#;
897
-
898
- let (_metadata, content, _title, _tables) = extract_jats_all_in_one(jats).expect("Parse failed");
899
- assert!(content.contains("Introduction"));
900
- assert!(content.contains("Methods"));
901
- assert!(content.contains("Results"));
902
- }
903
-
904
- #[test]
905
- fn test_jats_extractor_full_metadata_extraction() {
906
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
907
- <article>
908
- <front>
909
- <article-meta>
910
- <article-title>Sample Article</article-title>
911
- <contrib-group>
912
- <contrib contrib-type="author">
913
- <name>
914
- <surname>Smith</surname>
915
- <given-names>John</given-names>
916
- </name>
917
- </contrib>
918
- </contrib-group>
919
- <article-id pub-id-type="doi">10.1234/test</article-id>
920
- <kwd-group>
921
- <kwd>test</kwd>
922
- </kwd-group>
923
- <abstract>
924
- <p>Test abstract.</p>
925
- </abstract>
926
- </article-meta>
927
- </front>
928
- <body>
929
- <p>Sample content.</p>
930
- </body>
931
- </article>"#;
932
-
933
- let (metadata_extracted, _content, _title, _tables) =
934
- extract_jats_all_in_one(jats).expect("Metadata extraction failed");
935
- assert_eq!(metadata_extracted.title, "Sample Article");
936
- assert_eq!(metadata_extracted.authors.len(), 1);
937
- assert_eq!(metadata_extracted.doi, Some("10.1234/test".to_string()));
938
- assert_eq!(metadata_extracted.keywords.len(), 1);
939
- assert!(metadata_extracted.abstract_text.is_some());
940
- }
941
-
942
- #[test]
943
- fn test_jats_extractor_empty_article() {
944
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
945
- <article>
946
- <front>
947
- <article-meta>
948
- </article-meta>
949
- </front>
950
- <body>
951
- </body>
952
- </article>"#;
953
-
954
- let (metadata, content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
955
- assert!(metadata.title.is_empty());
956
- assert!(content.is_empty() || content.trim().is_empty());
957
- }
958
-
959
- #[test]
960
- fn test_extract_jats_journal_title() {
961
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
962
- <article>
963
- <front>
964
- <article-meta>
965
- <article-title>Test Article</article-title>
966
- <journal-title>Nature Medicine</journal-title>
967
- </article-meta>
968
- </front>
969
- </article>"#;
970
-
971
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
972
- assert_eq!(metadata.journal_title, Some("Nature Medicine".to_string()));
973
- }
974
-
975
- #[test]
976
- fn test_extract_jats_article_type() {
977
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
978
- <article article-type="research-article">
979
- <front>
980
- <article-meta>
981
- <article-title>Test Article</article-title>
982
- </article-meta>
983
- </front>
984
- </article>"#;
985
-
986
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
987
- assert_eq!(metadata.article_type, Some("research-article".to_string()));
988
- }
989
-
990
- #[test]
991
- fn test_extract_all_13_metadata_fields() {
992
- let jats = r#"<?xml version="1.0" encoding="UTF-8"?>
993
- <article article-type="research-article">
994
- <front>
995
- <article-meta>
996
- <article-title>Full Metadata Test</article-title>
997
- <subtitle>A Complete Example</subtitle>
998
- <contrib-group>
999
- <contrib contrib-type="author">
1000
- <name>
1001
- <surname>Author</surname>
1002
- <given-names>First</given-names>
1003
- </name>
1004
- </contrib>
1005
- </contrib-group>
1006
- <aff>Department of Testing, Test University</aff>
1007
- <article-id pub-id-type="doi">10.1234/full-test</article-id>
1008
- <article-id pub-id-type="pii">TEST-001</article-id>
1009
- <kwd-group>
1010
- <kwd>testing</kwd>
1011
- <kwd>metadata</kwd>
1012
- </kwd-group>
1013
- <pub-date pub-type="epub">
1014
- <year>2024</year>
1015
- </pub-date>
1016
- <volume>5</volume>
1017
- <issue>3</issue>
1018
- <fpage>100</fpage>
1019
- <lpage>110</lpage>
1020
- <journal-title>Test Journal</journal-title>
1021
- <abstract>
1022
- <p>This is a test abstract for all metadata fields.</p>
1023
- </abstract>
1024
- <author-notes>
1025
- <corresp>Correspondence: test@example.com</corresp>
1026
- </author-notes>
1027
- </article-meta>
1028
- </front>
1029
- <body>
1030
- <p>Test content.</p>
1031
- </body>
1032
- </article>"#;
1033
-
1034
- let (metadata, _content, _title, _tables) = extract_jats_all_in_one(jats).expect("Metadata extraction failed");
1035
-
1036
- assert_eq!(metadata.title, "Full Metadata Test");
1037
- assert_eq!(metadata.subtitle, Some("A Complete Example".to_string()));
1038
- assert_eq!(metadata.authors.len(), 1);
1039
- assert!(metadata.authors[0].contains("Author"));
1040
- assert_eq!(metadata.affiliations.len(), 1);
1041
- assert!(metadata.affiliations[0].contains("Testing"));
1042
- assert_eq!(metadata.doi, Some("10.1234/full-test".to_string()));
1043
- assert_eq!(metadata.pii, Some("TEST-001".to_string()));
1044
- assert_eq!(metadata.keywords.len(), 2);
1045
- assert!(metadata.publication_date.is_some());
1046
- assert_eq!(metadata.volume, Some("5".to_string()));
1047
- assert_eq!(metadata.issue, Some("3".to_string()));
1048
- assert!(metadata.pages.is_some());
1049
- assert_eq!(metadata.journal_title, Some("Test Journal".to_string()));
1050
- assert_eq!(metadata.article_type, Some("research-article".to_string()));
1051
- assert!(metadata.abstract_text.is_some());
1052
- assert!(metadata.corresponding_author.is_some());
1053
- }
1054
- }