iiif_print 3.2.2 → 3.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6273bfe83a864471ade7c0735cffc768d80d15054c34d479ae00f34993703329
4
- data.tar.gz: 17651696ee25b9bedad6c415f8ab804d99adf13bc943e08f1f0fa910c3cbb70d
3
+ metadata.gz: 63e30799cf45be6cb719486414c6ce6ffaec6742823fad504b37777891bec6a7
4
+ data.tar.gz: 5c33a0f762a1ee6d74b160704f244bc0447fdabce3670878a3973e802dc01446
5
5
  SHA512:
6
- metadata.gz: 269d3160d6d7dcade6c1455f336fd8d0d68401f2e895fd7c99f3b9f31ad662eb1ced1320265eb2b6c21d98c32f49c8e806695a29fe5042024826eed9d1f846d6
7
- data.tar.gz: a8a38e1bdd90ccb00a294548b4f9bb2c0cd1fd2b0c0e5f45a457c0a738e097c24f907e8349267ab1f3de4d4e50228001629e72ede031effb0de0792926b341c3
6
+ metadata.gz: 977f1e11d02f47a6ee5fd31bf42f4574179529c71e534ee175cf2d947a0f3d6684a553da7f2f68ea205f681ce8fc116b377a2de51f39c138218f6c7f268d423b
7
+ data.tar.gz: 87c6d0397b7eb0c96f678e1023ce22820b82f3c543b3126b18778f1027c80b27d6a0c065a64c3dfaa3a70490af039d4d760c1f3eb1b0ab673c2af7bd8afd0790
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /app/samvera/hyrax-webapp
3
3
  specs:
4
- iiif_print (3.2.2)
4
+ iiif_print (3.2.3)
5
5
  blacklight_iiif_search (>= 1.0, < 3.0)
6
6
  derivative-rodeo (~> 0.5)
7
7
  hyrax (>= 2.5, < 6)
@@ -181,6 +181,14 @@ module IiifPrint
181
181
  true
182
182
  end
183
183
 
184
+ ##
185
+ # does nothing for ActiveFedora; it keeps no records for extracted text.
186
+ #
187
+ # @return [TrueClass]
188
+ def self.remove_extracted_text_for(*)
189
+ true
190
+ end
191
+
184
192
  ##
185
193
  # Extract text from the derivatives
186
194
  #
@@ -207,6 +207,29 @@ module IiifPrint
207
207
  Hyrax::ValkyriePersistDerivatives.call(stream, directives)
208
208
  end
209
209
 
210
+ ##
211
+ # Remove the fileset's extracted text records, so that re-extracting
212
+ # text replaces them rather than adding to them. The stored files are
213
+ # kept: the new records are written to the same derivative paths.
214
+ #
215
+ # @param [Hyrax::FileSet] a Valkyrie fileset
216
+ # @return [TrueClass, nil] nil when there is nothing to remove
217
+ def self.remove_extracted_text_for(file_set:)
218
+ # re-read: earlier derivative plugins in the same job (e.g. the thumbnail)
219
+ # have saved the file set since the caller loaded it
220
+ file_set = Hyrax.query_service.find_by(id: file_set.id)
221
+ extracted = Hyrax.custom_queries.find_many_file_metadata_by_use(resource: file_set,
222
+ use: Hyrax::FileMetadata::Use.uri_for(use: :extracted_file)).to_a
223
+ return if extracted.empty?
224
+
225
+ # file_ids can hold Strings as well as Valkyrie::IDs, so compare as strings
226
+ extracted_ids = extracted.map { |file_metadata| file_metadata.id.to_s }
227
+ file_set.file_ids = file_set.file_ids.reject { |id| extracted_ids.include?(id.to_s) }
228
+ Hyrax.persister.save(resource: file_set)
229
+ extracted.each { |file_metadata| Hyrax.persister.delete(resource: file_metadata) }
230
+ true
231
+ end
232
+
210
233
  ##
211
234
  # Extract text from the derivatives
212
235
  #
@@ -117,6 +117,10 @@ module IiifPrint
117
117
  def self.pdf_path_for(file_set:)
118
118
  raise NotImplementedError, "#{self}.{__method__}"
119
119
  end
120
+
121
+ def self.remove_extracted_text_for(file_set:)
122
+ raise NotImplementedError, "#{self}.{__method__}"
123
+ end
120
124
  end
121
125
  end
122
126
  end
@@ -75,10 +75,11 @@ module IiifPrint
75
75
 
76
76
  private
77
77
 
78
- # transform the image into a one-bit TIFF for OCR
78
+ # transform the image into a one-bit TIFF for OCR; a one-bit JP2 is
79
+ # converted too, since Leptonica cannot read JP2 at one bit per sample
79
80
  def preprocess_image
80
81
  tool = IiifPrint::ImageTool.new(@path)
81
- return if tool.metadata[:color] == 'monochrome'
82
+ return if tool.metadata[:color] == 'monochrome' && tool.metadata[:content_type] != 'image/jp2'
82
83
  intermediate_path = File.join(Dir.mktmpdir, 'monochrome-interim.tif')
83
84
  tool.convert(intermediate_path, true)
84
85
  @path = intermediate_path
@@ -28,13 +28,16 @@ module IiifPrint
28
28
  # TODO: Do we need this source_path instance variable?
29
29
  @source_path = filename
30
30
  ocr = page_ocr_service_class.new(filename)
31
+ # run all OCR first, so a failed or empty run leaves existing text in place
32
+ contents = ocr_derivatives.transform_values { |method_name| ocr.public_send(method_name) }
33
+ return if contents[:txt].blank?
31
34
 
32
- ocr_derivatives.each do |extension, method_name|
33
- path = prepare_path(extension.to_s)
34
- content = ocr.public_send(method_name)
35
+ IiifPrint.remove_extracted_text_for(file_set: file_set)
36
+
37
+ contents.each do |extension, content|
35
38
  next if content.blank?
36
39
 
37
- write(content: content, path: path, extension: extension)
40
+ write(content: content, path: prepare_path(extension.to_s), extension: extension)
38
41
  end
39
42
  end
40
43
 
@@ -47,7 +50,7 @@ module IiifPrint
47
50
  end
48
51
 
49
52
  def cleanup_derivatives(*)
50
- ocr_derivatives.keys do |extension|
53
+ ocr_derivatives.each_key do |extension|
51
54
  super(extension.to_s)
52
55
  end
53
56
  end
@@ -18,7 +18,7 @@ module IiifPrint
18
18
  # Write data as UTF-8 encoded text
19
19
  File.open(save_path, "w:UTF-8") do |f|
20
20
  f.write(data)
21
- IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url: file_set.id.to_s, container: 'extracted_text', mime_type: mime_type })
21
+ IiifPrint.copy_derivatives_from_data_store(stream: data, directives: { url: save_path, container: 'extracted_text', mime_type: mime_type })
22
22
  end
23
23
  end
24
24
 
@@ -28,6 +28,10 @@ module IiifPrint
28
28
  !File.size(path).zero?
29
29
  end
30
30
 
31
+ def alto_with_words?(path)
32
+ nonempty_file?(path) && IiifPrint::TextExtraction::AltoReader.new(path).text.present?
33
+ end
34
+
31
35
  # if there was no derivative yet, there might be one in-transit from
32
36
  # an ingest, so check for that, and use its source if applicable:
33
37
  def incoming_alto_path
@@ -35,16 +39,17 @@ module IiifPrint
35
39
  fileset_id: @file_set.id,
36
40
  destination_name: 'xml'
37
41
  ).pluck(:path).uniq.first
38
- path if nonempty_file?(path)
42
+ path if alto_with_words?(path)
39
43
  end
40
44
 
41
45
  def alto_path
42
- # check first for existing, non-empty derivative data:
46
+ # check first for existing derivative data with words; an ALTO without
47
+ # words (e.g. from a failed OCR run) should not prevent OCR:
43
48
  path = derivative_path_factory.derivative_path_for_reference(
44
49
  @file_set,
45
50
  'xml'
46
51
  )
47
- return path if nonempty_file?(path)
52
+ return path if alto_with_words?(path)
48
53
  incoming_alto_path
49
54
  end
50
55
 
@@ -67,6 +72,8 @@ module IiifPrint
67
72
  width,
68
73
  height
69
74
  )
75
+ IiifPrint.remove_extracted_text_for(file_set: @file_set)
76
+ save_derivative('xml', source_file)
70
77
  save_derivative('json', reader.json)
71
78
  save_derivative('txt', reader.text)
72
79
  end
@@ -1,3 +1,3 @@
1
1
  module IiifPrint
2
- VERSION = '3.2.2'.freeze
2
+ VERSION = '3.2.3'.freeze
3
3
  end
data/lib/iiif_print.rb CHANGED
@@ -80,6 +80,7 @@ module IiifPrint
80
80
  :solr_name,
81
81
  :solr_query,
82
82
  :pdf_path_for,
83
+ :remove_extracted_text_for,
83
84
  to: :persistence_adapter
84
85
  )
85
86
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iiif_print
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sean Upton
@@ -11,10 +11,9 @@ authors:
11
11
  - Kirk Wang
12
12
  - LaRita Robinson
13
13
  - Jeremy Friesen
14
- autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
- date: 2026-10-21 00:00:00.000000000 Z
16
+ date: 1980-01-02 00:00:00.000000000 Z
18
17
  dependencies:
19
18
  - !ruby/object:Gem::Dependency
20
19
  name: blacklight_iiif_search
@@ -502,7 +501,6 @@ homepage: https://github.com/notch8/iiif_print/
502
501
  licenses:
503
502
  - Apache-2.0
504
503
  metadata: {}
505
- post_install_message:
506
504
  rdoc_options: []
507
505
  require_paths:
508
506
  - lib
@@ -517,8 +515,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
517
515
  - !ruby/object:Gem::Version
518
516
  version: '0'
519
517
  requirements: []
520
- rubygems_version: 3.3.20
521
- signing_key:
518
+ rubygems_version: 3.6.7
522
519
  specification_version: 4
523
520
  summary: IiifPrint is a gem (Rails "engine") for Hyrax-based digital repository applications
524
521
  to support displaying parent/child works in the same viewer (Universal Viewer) and