Class: Mindee::Parsing::Standard::AbstractField

Inherits:
Object
  • Object
show all
Defined in:
lib/mindee/parsing/standard/base_field.rb

Overview

Base Field object, upon which fields and feature fields are built

Direct Known Subclasses

FeatureField, Field

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prediction, page_id) ⇒ AbstractField

Returns a new instance of AbstractField.

Parameters:

  • prediction (Hash)
  • page_id (Integer, nil)


22
23
24
25
26
27
# File 'lib/mindee/parsing/standard/base_field.rb', line 22

def initialize(prediction, page_id)
  @confidence = prediction['confidence'] if prediction.key?('confidence')
  @polygon = Geometry.polygon_from_prediction(prediction['polygon']) if prediction.key?('polygon')
  @bounding_box = Geometry.get_bounding_box(@polygon) unless @polygon.nil? || @polygon.empty?
  @page_id = page_id || prediction['page_id']
end

Instance Attribute Details

#bounding_boxMindee::Geometry::Quadrilateral? (readonly)



11
12
13
# File 'lib/mindee/parsing/standard/base_field.rb', line 11

def bounding_box
  @bounding_box
end

#confidenceFloat?

The confidence score, value will be between 0.0 and 1.0

Returns:

  • (Float, nil)


18
19
20
# File 'lib/mindee/parsing/standard/base_field.rb', line 18

def confidence
  @confidence
end

#page_idInteger? (readonly)

Returns:

  • (Integer, nil)


15
16
17
# File 'lib/mindee/parsing/standard/base_field.rb', line 15

def page_id
  @page_id
end

#polygonMindee::Geometry::Polygon? (readonly)

Returns:



13
14
15
# File 'lib/mindee/parsing/standard/base_field.rb', line 13

def polygon
  @polygon
end

Class Method Details

.array_confidence(field_array) ⇒ Float

Multiply all the Mindee::Parsing::Standard::Field confidences in the array.

Returns:

  • (Float)


36
37
38
39
40
41
42
43
44
# File 'lib/mindee/parsing/standard/base_field.rb', line 36

def self.array_confidence(field_array)
  product = 1
  field_array.each do |field|
    return 0.0 if field.confidence.nil?

    product *= field.confidence
  end
  product.to_f
end

.array_sum(field_array) ⇒ Float

Add all the Mindee::Parsing::Standard::Field values in the array.

Returns:

  • (Float)


48
49
50
51
52
53
54
55
56
# File 'lib/mindee/parsing/standard/base_field.rb', line 48

def self.array_sum(field_array)
  arr_sum = 0
  field_array.each do |field|
    return 0.0 if field.value.nil?

    arr_sum += field.value
  end
  arr_sum.to_f
end

.float_to_string(value, min_precision = 2) ⇒ String

Parameters:

  • value (Float)
  • min_precision (Integer) (defaults to: 2)

Returns:

  • (String)


61
62
63
64
65
66
67
68
# File 'lib/mindee/parsing/standard/base_field.rb', line 61

def self.float_to_string(value, min_precision = 2)
  return String.new if value.nil?

  precision = value.to_f.to_s.split('.')[1].size
  precision = [precision, min_precision].max
  format_string = "%.#{precision}f"
  format(format_string, value)
end

Instance Method Details

#to_sString

Returns:

  • (String)


30
31
32
# File 'lib/mindee/parsing/standard/base_field.rb', line 30

def to_s
  @value ? @value.to_s : ''
end