Class: ActiveStorage::Ocr::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/activestorage/ocr/result.rb

Overview

Represents the result of an OCR operation.

Contains the extracted text, confidence score, and processing metadata.

Example

result = client.extract_text(blob)
if result.success?
puts result.text
puts "Confidence: #{result.confidence}"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, confidence:, processing_time_ms:, warnings: [], engine: nil, preprocessing: nil) ⇒ Result

Creates a new Result.

Parameters

  • text - The extracted text
  • confidence - Confidence score (0.0 to 1.0)
  • processing_time_ms - Processing time in milliseconds
  • warnings - Array of warning messages (optional)
  • engine - The OCR engine used (optional)
  • preprocessing - Preprocessing stats hash (optional)


47
48
49
50
51
52
53
54
# File 'lib/activestorage/ocr/result.rb', line 47

def initialize(text:, confidence:, processing_time_ms:, warnings: [], engine: nil, preprocessing: nil)
  @text = text
  @confidence = confidence
  @processing_time_ms = processing_time_ms
  @warnings = warnings
  @engine = engine
  @preprocessing = preprocessing
end

Instance Attribute Details

#confidence ⇒ Object (readonly)

Confidence score from 0.0 to 1.0.



22
23
24
# File 'lib/activestorage/ocr/result.rb', line 22

def confidence
  @confidence
end

#engine ⇒ Object (readonly)

The OCR engine that processed this result (e.g., "ocrs" or "leptess").



31
32
33
# File 'lib/activestorage/ocr/result.rb', line 31

def engine
  @engine
end

#preprocessing ⇒ Object (readonly)

Preprocessing statistics (Hash with :preset, :total_time_ms, :steps). nil if preprocessing was skipped.



35
36
37
# File 'lib/activestorage/ocr/result.rb', line 35

def preprocessing
  @preprocessing
end

#processing_time_ms ⇒ Object (readonly)

Time taken to process the file in milliseconds.



25
26
27
# File 'lib/activestorage/ocr/result.rb', line 25

def processing_time_ms
  @processing_time_ms
end

#text ⇒ Object (readonly)

The extracted text content.



19
20
21
# File 'lib/activestorage/ocr/result.rb', line 19

def text
  @text
end

#warnings ⇒ Object (readonly)

Array of warning messages from the OCR server.



28
29
30
# File 'lib/activestorage/ocr/result.rb', line 28

def warnings
  @warnings
end

Instance Method Details

#preprocessing_preset ⇒ Object

Returns the preprocessing preset used, or nil if not preprocessed.



71
72
73
# File 'lib/activestorage/ocr/result.rb', line 71

def preprocessing_preset
  preprocessing&.dig(:preset)
end

#preprocessing_time_ms ⇒ Object

Returns the preprocessing time in milliseconds, or 0 if not preprocessed.



66
67
68
# File 'lib/activestorage/ocr/result.rb', line 66

def preprocessing_time_ms
  preprocessing&.dig(:total_time_ms) || 0
end

#success? ⇒ Boolean

Returns whether OCR successfully extracted text.

Returns

true if text was extracted, false if text is nil or empty.

Returns:

  • (Boolean)


61
62
63
# File 'lib/activestorage/ocr/result.rb', line 61

def success?
  !text.nil? && !text.empty?
end

#to_h ⇒ Object

Converts the result to a Hash.

Returns

A Hash with all result attributes.



80
81
82
83
84
85
86
87
88
89
# File 'lib/activestorage/ocr/result.rb', line 80

def to_h
  {
    text: text,
    confidence: confidence,
    processing_time_ms: processing_time_ms,
    warnings: warnings,
    engine: engine,
    preprocessing: preprocessing
  }
end

#to_metadata ⇒ Object

Converts the result to Active Storage metadata format.

This format is suitable for storing in blob metadata.

Returns

A Hash with :ocr_text, :ocr_confidence, :ocr_engine, and :ocr_processed_at.



98
99
100
101
102
103
104
105
# File 'lib/activestorage/ocr/result.rb', line 98

def 
  {
    ocr_text: text,
    ocr_confidence: confidence,
    ocr_engine: engine,
    ocr_processed_at: Time.now.utc.iso8601
  }
end