mindee 2.2.0 → 3.0.0

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 (152) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/.rubocop.yml +2 -0
  4. data/.yardopts +1 -0
  5. data/CHANGELOG.md +36 -0
  6. data/README.md +29 -16
  7. data/bin/mindee.rb +23 -26
  8. data/docs/code_samples/bank_account_details_v1.txt +10 -5
  9. data/docs/code_samples/bank_account_details_v2.txt +19 -0
  10. data/docs/code_samples/bank_check_v1.txt +10 -5
  11. data/docs/code_samples/carte_vitale_v1.txt +10 -5
  12. data/docs/code_samples/custom_v1.txt +19 -10
  13. data/docs/code_samples/default.txt +10 -2
  14. data/docs/code_samples/expense_receipts_v4.txt +10 -5
  15. data/docs/code_samples/expense_receipts_v5.txt +11 -6
  16. data/docs/code_samples/financial_document_v1.txt +10 -5
  17. data/docs/code_samples/idcard_fr_v1.txt +10 -5
  18. data/docs/code_samples/invoice_splitter_v1_async.txt +66 -0
  19. data/docs/code_samples/invoices_v4.txt +10 -5
  20. data/docs/code_samples/license_plates_v1.txt +10 -5
  21. data/docs/code_samples/passport_v1.txt +10 -5
  22. data/docs/code_samples/proof_of_address_v1.txt +10 -5
  23. data/docs/ruby-api-builder.md +30 -31
  24. data/docs/ruby-getting-started.md +64 -23
  25. data/docs/ruby-invoice-ocr.md +70 -59
  26. data/docs/ruby-passport-ocr.md +49 -40
  27. data/docs/ruby-receipt-ocr.md +45 -32
  28. data/lib/mindee/client.rb +150 -148
  29. data/lib/mindee/geometry/min_max.rb +23 -0
  30. data/lib/mindee/geometry/point.rb +35 -0
  31. data/lib/mindee/geometry/polygon.rb +23 -0
  32. data/lib/mindee/geometry/quadrilateral.rb +45 -0
  33. data/lib/mindee/geometry/utils.rb +81 -0
  34. data/lib/mindee/geometry.rb +5 -116
  35. data/lib/mindee/http/endpoint.rb +123 -16
  36. data/lib/mindee/http.rb +3 -0
  37. data/lib/mindee/input/sources.rb +87 -73
  38. data/lib/mindee/parsing/common/api_response.rb +109 -0
  39. data/lib/mindee/parsing/common/document.rb +48 -0
  40. data/lib/mindee/parsing/common/error.rb +24 -0
  41. data/lib/mindee/parsing/common/inference.rb +43 -0
  42. data/lib/mindee/parsing/common/ocr/mvision_v1.rb +34 -0
  43. data/lib/mindee/parsing/common/ocr/ocr.rb +169 -0
  44. data/lib/mindee/parsing/common/ocr.rb +3 -0
  45. data/lib/mindee/parsing/common/orientation.rb +26 -0
  46. data/lib/mindee/parsing/common/page.rb +40 -0
  47. data/lib/mindee/parsing/common/prediction.rb +15 -0
  48. data/lib/mindee/parsing/common/product.rb +19 -0
  49. data/lib/mindee/parsing/common.rb +10 -0
  50. data/lib/mindee/parsing/custom/classification_field.rb +28 -0
  51. data/lib/mindee/parsing/custom/list_field.rb +76 -0
  52. data/lib/mindee/parsing/custom.rb +4 -0
  53. data/lib/mindee/parsing/standard/amount_field.rb +26 -0
  54. data/lib/mindee/parsing/standard/base_field.rb +104 -0
  55. data/lib/mindee/parsing/standard/classification_field.rb +16 -0
  56. data/lib/mindee/parsing/standard/company_registration_field.rb +21 -0
  57. data/lib/mindee/parsing/standard/date_field.rb +34 -0
  58. data/lib/mindee/parsing/standard/locale_field.rb +50 -0
  59. data/lib/mindee/parsing/standard/payment_details_field.rb +42 -0
  60. data/lib/mindee/parsing/standard/position_field.rb +44 -0
  61. data/lib/mindee/parsing/standard/tax_field.rb +108 -0
  62. data/lib/mindee/parsing/standard/text_field.rb +16 -0
  63. data/lib/mindee/parsing/standard.rb +12 -0
  64. data/lib/mindee/parsing.rb +3 -2
  65. data/lib/mindee/{input → pdf}/pdf_processing.rb +4 -32
  66. data/lib/mindee/pdf/pdf_tools.rb +34 -0
  67. data/lib/mindee/pdf.rb +3 -0
  68. data/lib/mindee/product/.rubocop.yml +5 -0
  69. data/lib/mindee/product/custom/custom_v1.rb +35 -0
  70. data/lib/mindee/product/custom/custom_v1_document.rb +60 -0
  71. data/lib/mindee/product/custom/custom_v1_page.rb +32 -0
  72. data/lib/mindee/product/eu/license_plate/license_plate_v1.rb +38 -0
  73. data/lib/mindee/product/eu/license_plate/license_plate_v1_document.rb +37 -0
  74. data/lib/mindee/product/eu/license_plate/license_plate_v1_page.rb +34 -0
  75. data/lib/mindee/product/financial_document/financial_document_v1.rb +36 -0
  76. data/lib/mindee/product/financial_document/financial_document_v1_document.rb +188 -0
  77. data/lib/mindee/product/financial_document/financial_document_v1_line_item.rb +90 -0
  78. data/lib/mindee/product/financial_document/financial_document_v1_page.rb +32 -0
  79. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v1.rb +38 -0
  80. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v1_document.rb +43 -0
  81. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v1_page.rb +34 -0
  82. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v2.rb +38 -0
  83. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v2_bban.rb +71 -0
  84. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v2_document.rb +58 -0
  85. data/lib/mindee/product/fr/bank_account_details/bank_account_details_v2_page.rb +34 -0
  86. data/lib/mindee/product/fr/carte_vitale/carte_vitale_v1.rb +38 -0
  87. data/lib/mindee/product/fr/carte_vitale/carte_vitale_v1_document.rb +52 -0
  88. data/lib/mindee/product/fr/carte_vitale/carte_vitale_v1_page.rb +34 -0
  89. data/lib/mindee/product/fr/id_card/id_card_v1.rb +38 -0
  90. data/lib/mindee/product/fr/id_card/id_card_v1_document.rb +82 -0
  91. data/lib/mindee/product/fr/id_card/id_card_v1_page.rb +48 -0
  92. data/lib/mindee/product/invoice/invoice_v4.rb +37 -0
  93. data/lib/mindee/product/invoice/invoice_v4_document.rb +212 -0
  94. data/lib/mindee/product/invoice/invoice_v4_line_item.rb +66 -0
  95. data/lib/mindee/product/invoice/invoice_v4_page.rb +32 -0
  96. data/lib/mindee/product/invoice_splitter/invoice_splitter_v1.rb +36 -0
  97. data/lib/mindee/product/invoice_splitter/invoice_splitter_v1_document.rb +65 -0
  98. data/lib/mindee/product/invoice_splitter/invoice_splitter_v1_page.rb +32 -0
  99. data/lib/mindee/product/passport/passport_v1.rb +36 -0
  100. data/lib/mindee/{parsing/prediction/fr/id_card/id_card_v1.rb → product/passport/passport_v1_document.rb} +45 -45
  101. data/lib/mindee/product/passport/passport_v1_page.rb +32 -0
  102. data/lib/mindee/product/proof_of_address/proof_of_address_v1.rb +36 -0
  103. data/lib/mindee/product/proof_of_address/proof_of_address_v1_document.rb +83 -0
  104. data/lib/mindee/product/proof_of_address/proof_of_address_v1_page.rb +32 -0
  105. data/lib/mindee/product/receipt/receipt_v4.rb +36 -0
  106. data/lib/mindee/product/receipt/receipt_v4_document.rb +86 -0
  107. data/lib/mindee/product/receipt/receipt_v4_page.rb +32 -0
  108. data/lib/mindee/product/receipt/receipt_v5.rb +36 -0
  109. data/lib/mindee/product/receipt/receipt_v5_document.rb +138 -0
  110. data/lib/mindee/product/receipt/receipt_v5_line_item.rb +69 -0
  111. data/lib/mindee/product/receipt/receipt_v5_page.rb +32 -0
  112. data/lib/mindee/product/us/bank_check/bank_check_v1.rb +38 -0
  113. data/lib/mindee/product/us/bank_check/bank_check_v1_document.rb +73 -0
  114. data/lib/mindee/product/us/bank_check/bank_check_v1_page.rb +34 -0
  115. data/lib/mindee/product.rb +16 -0
  116. data/lib/mindee/version.rb +2 -1
  117. data/lib/mindee.rb +3 -1
  118. metadata +87 -38
  119. data/docs/code_samples/shipping_containers_v1.txt +0 -14
  120. data/lib/mindee/document_config.rb +0 -60
  121. data/lib/mindee/parsing/document.rb +0 -31
  122. data/lib/mindee/parsing/error.rb +0 -22
  123. data/lib/mindee/parsing/inference.rb +0 -53
  124. data/lib/mindee/parsing/page.rb +0 -46
  125. data/lib/mindee/parsing/prediction/base.rb +0 -30
  126. data/lib/mindee/parsing/prediction/common_fields/amount.rb +0 -21
  127. data/lib/mindee/parsing/prediction/common_fields/base.rb +0 -72
  128. data/lib/mindee/parsing/prediction/common_fields/company_registration.rb +0 -17
  129. data/lib/mindee/parsing/prediction/common_fields/date.rb +0 -30
  130. data/lib/mindee/parsing/prediction/common_fields/locale.rb +0 -45
  131. data/lib/mindee/parsing/prediction/common_fields/payment_details.rb +0 -33
  132. data/lib/mindee/parsing/prediction/common_fields/position.rb +0 -39
  133. data/lib/mindee/parsing/prediction/common_fields/tax.rb +0 -40
  134. data/lib/mindee/parsing/prediction/common_fields/text.rb +0 -12
  135. data/lib/mindee/parsing/prediction/common_fields.rb +0 -11
  136. data/lib/mindee/parsing/prediction/custom/custom_v1.rb +0 -58
  137. data/lib/mindee/parsing/prediction/custom/fields.rb +0 -91
  138. data/lib/mindee/parsing/prediction/eu/license_plate/license_plate_v1.rb +0 -34
  139. data/lib/mindee/parsing/prediction/financial_document/financial_document_v1.rb +0 -237
  140. data/lib/mindee/parsing/prediction/financial_document/financial_document_v1_line_item.rb +0 -58
  141. data/lib/mindee/parsing/prediction/fr/bank_account_details/bank_account_details_v1.rb +0 -40
  142. data/lib/mindee/parsing/prediction/fr/carte_vitale/carte_vitale_v1.rb +0 -49
  143. data/lib/mindee/parsing/prediction/invoice/invoice_v4.rb +0 -212
  144. data/lib/mindee/parsing/prediction/invoice/invoice_v4_line_item.rb +0 -58
  145. data/lib/mindee/parsing/prediction/passport/passport_v1.rb +0 -121
  146. data/lib/mindee/parsing/prediction/proof_of_address/proof_of_address_v1.rb +0 -80
  147. data/lib/mindee/parsing/prediction/receipt/receipt_v4.rb +0 -87
  148. data/lib/mindee/parsing/prediction/receipt/receipt_v5.rb +0 -136
  149. data/lib/mindee/parsing/prediction/receipt/receipt_v5_line_item.rb +0 -37
  150. data/lib/mindee/parsing/prediction/shipping_container/shipping_container_v1.rb +0 -38
  151. data/lib/mindee/parsing/prediction/us/bank_check/bank_check_v1.rb +0 -70
  152. data/lib/mindee/parsing/prediction.rb +0 -15
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
-
5
- require_relative 'http/endpoint'
6
- require_relative 'parsing/document'
7
- require_relative 'parsing/error'
8
- require_relative 'parsing/prediction'
9
-
10
- module Mindee
11
- # Specific client for sending a document to the API.
12
- class DocumentConfig
13
- # Array of possible Mindee::Endpoint to be used.
14
- # @return [Mindee::HTTP::Endpoint]
15
- attr_reader :endpoint
16
-
17
- # @param prediction_class [Class<Mindee::Prediction::Prediction>]
18
- # @param endpoint [Mindee::HTTP::Endpoint]
19
- def initialize(prediction_class, endpoint)
20
- @prediction_class = prediction_class
21
- @endpoint = endpoint
22
- end
23
-
24
- # Call the prediction API.
25
- # @param input_doc [Mindee::InputDocument]
26
- # @param include_words [Boolean]
27
- # @param close_file [Boolean]
28
- # @param cropper [Boolean]
29
- # @return [Mindee::DocumentResponse]
30
- def predict(input_doc, include_words, close_file, cropper)
31
- check_api_key
32
- response = predict_request(input_doc, include_words, close_file, cropper)
33
- hashed_response = JSON.parse(response.body, object_class: Hash)
34
- return Document.new(@prediction_class, hashed_response['document']) if (200..299).include?(response.code.to_i)
35
-
36
- error = Parsing::Error.new(hashed_response['api_request']['error'])
37
- raise error
38
- end
39
-
40
- private
41
-
42
- # @param input_doc [Mindee::InputDocument]
43
- # @param include_words [Boolean]
44
- # @param close_file [Boolean]
45
- # # @param cropper [Boolean]
46
- # @return [Net::HTTPResponse]
47
- def predict_request(input_doc, include_words, close_file, cropper)
48
- @endpoint.predict_req_post(input_doc, include_words: include_words, close_file: close_file, cropper: cropper)
49
- end
50
-
51
- def check_api_key
52
- return unless @endpoint.api_key.nil? || @endpoint.api_key.empty?
53
-
54
- raise "Missing API key for '#{@document_type}', " \
55
- "check your Client Configuration.\n" \
56
- 'You can set this using the ' \
57
- "'#{HTTP::API_KEY_ENV_NAME}' environment variable."
58
- end
59
- end
60
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'inference'
4
-
5
- module Mindee
6
- # Stores all response attributes.
7
- class Document
8
- # @return [Mindee::Inference]
9
- attr_reader :inference
10
- # @return [String] Filename sent to the API
11
- attr_reader :name
12
- # @return [String] Mindee ID of the document
13
- attr_reader :id
14
-
15
- # @param prediction_class [Class<Mindee::Prediction::Prediction>]
16
- # @param http_response [Hash]
17
- def initialize(prediction_class, http_response)
18
- @id = http_response['id']
19
- @name = http_response['name']
20
- @inference = Mindee::Inference.new(prediction_class, http_response['inference'])
21
- end
22
-
23
- def to_s
24
- out_str = String.new
25
- out_str << "########\nDocument\n########"
26
- out_str << "\n:Mindee ID: #{@id}"
27
- out_str << "\n:Filename: #{@name}"
28
- out_str << "\n\n#{@inference}"
29
- end
30
- end
31
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mindee
4
- module Parsing
5
- # API Error
6
- class Error < StandardError
7
- # @return [String]
8
- attr_reader :api_code
9
- # @return [String]
10
- attr_reader :api_details
11
- # @return [String]
12
- attr_reader :api_message
13
-
14
- def initialize(error)
15
- @api_code = error['code']
16
- @api_details = error['details']
17
- @api_message = error['message']
18
- super("#{@api_code}: #{@api_details} - #{@api_message}")
19
- end
20
- end
21
- end
22
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'page'
4
-
5
- module Mindee
6
- # Product information
7
- class Product
8
- attr_reader :name, :type, :version
9
-
10
- # @param http_response [Hash]
11
- def initialize(http_response)
12
- @name = http_response['name']
13
- @type = http_response['type']
14
- @version = http_response['version']
15
- end
16
- end
17
-
18
- # Inference holds all predictions
19
- class Inference
20
- # @return [Boolean]
21
- attr_reader :is_rotation_applied
22
- # @return [Array<Mindee::Page>]
23
- attr_reader :pages
24
- # @return [Mindee::Prediction]
25
- attr_reader :prediction
26
- # @return [Mindee::Product]
27
- attr_reader :product
28
-
29
- # @param prediction_class [Class<Mindee::Prediction::Prediction>]
30
- # @param http_response [Hash]
31
- def initialize(prediction_class, http_response)
32
- @is_rotation_applied = http_response['is_rotation_applied']
33
- @prediction = prediction_class.new(http_response['prediction'], nil)
34
- @product = Product.new(http_response['product'])
35
- @pages = []
36
- http_response['pages'].each do |page|
37
- @pages.push(Page.new(prediction_class, page))
38
- end
39
- end
40
-
41
- def to_s
42
- is_rotation_applied = @is_rotation_applied ? 'Yes' : 'No'
43
- out_str = String.new
44
- out_str << "Inference\n#########"
45
- out_str << "\n:Product: #{@product.name} v#{@product.version}"
46
- out_str << "\n:Rotation applied: #{is_rotation_applied}"
47
- out_str << "\n\nPrediction\n=========="
48
- out_str << "\n#{@prediction}"
49
- out_str << "\n\nPage Predictions\n================\n\n"
50
- out_str << @pages.map(&:to_s).join("\n\n")
51
- end
52
- end
53
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mindee
4
- # Page orientation
5
- class Orientation
6
- # @return [Integer]
7
- attr_reader :page_id
8
- # A prediction among these 3 possible outputs:
9
- # * 0 degrees: the page is already upright
10
- # * 90 degrees: the page must be rotated clockwise to be upright
11
- # * 270 degrees: the page must be rotated counterclockwise to be upright
12
- # @return [Integer, nil]
13
- attr_reader :value
14
-
15
- # @param prediction [Hash]
16
- # @param page_id [Integer]
17
- def initialize(prediction, page_id)
18
- @value = prediction['value']
19
- @page_id = page_id
20
- end
21
- end
22
-
23
- # Page object
24
- class Page
25
- # @return [Integer]
26
- attr_reader :id
27
- # @return [Mindee::Prediction]
28
- attr_reader :prediction
29
- # @return [Mindee::Orientation]
30
- attr_reader :orientation
31
-
32
- def initialize(prediction_class, page)
33
- @id = page['id'].to_i
34
- @prediction = prediction_class.new(page['prediction'], @id)
35
- @orientation = Orientation.new(page['orientation'], @id)
36
- end
37
-
38
- def to_s
39
- out_str = String.new
40
- title = "Page #{@id}"
41
- out_str << "#{title}\n"
42
- out_str << ('-' * title.size)
43
- out_str << "\n#{@prediction}"
44
- end
45
- end
46
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mindee
4
- module Prediction
5
- # Base document object.
6
- class Prediction
7
- # document type
8
- # @return [String]
9
- attr_reader :document_type
10
- # Validation checks for the document
11
- # @return [Hash<Symbol, Boolean>]
12
- attr_reader :checklist
13
- # Original filename of the document
14
- # @return [String, nil]
15
- attr_reader :filename
16
- # Detected MIME type of the document
17
- # @return [String, nil]
18
- attr_reader :file_mimetype
19
-
20
- def initialize(_prediction, _page_id)
21
- @checklist = {}
22
- end
23
-
24
- # @return [Boolean]
25
- def all_checks
26
- @checklist.all? { |_, value| value == true }
27
- end
28
- end
29
- end
30
- end
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base'
4
-
5
- module Mindee
6
- # Represents tax information.
7
- class AmountField < Field
8
- # Amount value as 3 decimal float
9
- # @return [Float, nil]
10
- attr_reader :value
11
-
12
- def initialize(prediction, page_id, reconstructed: false)
13
- super
14
- @value = @value.round(3) unless @value.nil?
15
- end
16
-
17
- def to_s
18
- Field.float_to_string(@value)
19
- end
20
- end
21
- end
@@ -1,72 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative '../../../geometry'
4
-
5
- module Mindee
6
- # Base field object.
7
- class Field
8
- # @return [String, Float, Integer, Boolean]
9
- attr_reader :value
10
- # @return [Array<Array<Float>>]
11
- attr_reader :bounding_box
12
- # @return [Mindee::Geometry::Polygon]
13
- attr_reader :polygon
14
- # @return [Integer, nil]
15
- attr_reader :page_id
16
- # true if the field was reconstructed or computed using other fields.
17
- # @return [Boolean]
18
- attr_reader :reconstructed
19
- # The confidence score, value will be between 0.0 and 1.0
20
- # @return [Float]
21
- attr_accessor :confidence
22
-
23
- # @param prediction [Hash]
24
- # @param page_id [Integer, nil]
25
- # @param reconstructed [Boolean]
26
- def initialize(prediction, page_id, reconstructed: false)
27
- @value = prediction['value']
28
- @confidence = prediction['confidence']
29
- @polygon = Geometry.polygon_from_prediction(prediction['polygon'])
30
- @bounding_box = Geometry.get_bounding_box(@polygon) unless @polygon.nil? || @polygon.empty?
31
- @page_id = page_id || prediction['page_id']
32
- @reconstructed = reconstructed
33
- end
34
-
35
- def to_s
36
- @value ? @value.to_s : ''
37
- end
38
-
39
- # Multiply all the Mindee::Field confidences in the array.
40
- def self.array_confidence(field_array)
41
- product = 1
42
- field_array.each do |field|
43
- return 0.0 if field.confidence.nil?
44
-
45
- product *= field.confidence
46
- end
47
- product.to_f
48
- end
49
-
50
- # Add all the Mindee::Field values in the array.
51
- def self.array_sum(field_array)
52
- arr_sum = 0
53
- field_array.each do |field|
54
- return 0.0 if field.value.nil?
55
-
56
- arr_sum += field.value
57
- end
58
- arr_sum.to_f
59
- end
60
-
61
- # @param value [Float]
62
- # @param min_precision [Integer]
63
- def self.float_to_string(value, min_precision = 2)
64
- return String.new if value.nil?
65
-
66
- precision = value.to_f.to_s.split('.')[1].size
67
- precision = [precision, min_precision].max
68
- format_string = "%.#{precision}f"
69
- format(format_string, value)
70
- end
71
- end
72
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mindee
4
- # Company registration number or code, and its type.
5
- class CompanyRegistration < Field
6
- # @return [String]
7
- attr_reader :type
8
-
9
- # @param prediction [Hash]
10
- # @param page_id [Integer, nil]
11
- # @param reconstructed [Boolean]
12
- def initialize(prediction, page_id, reconstructed: false)
13
- super
14
- @type = prediction['type']
15
- end
16
- end
17
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'date'
4
-
5
- require_relative 'base'
6
-
7
- module Mindee
8
- # Represents a date.
9
- class DateField < Field
10
- # The date as a standard Ruby `Date` object.
11
- # @return [Date, nil]
12
- attr_reader :date_object
13
- # The ISO 8601 representation of the date, regardless of the `raw` contents.
14
- # @return [String, nil]
15
- attr_reader :value
16
- # The textual representation of the date as found on the document.
17
- # @return [String, nil]
18
- attr_reader :raw
19
-
20
- # @param prediction [Hash]
21
- # @param page_id [Integer, nil]
22
- def initialize(prediction, page_id)
23
- super
24
- return unless @value
25
-
26
- @date_object = Date.parse(@value)
27
- @raw = prediction['raw']
28
- end
29
- end
30
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mindee
4
- # Represents locale information
5
- class Locale
6
- # The confidence score, value will be between 0.0 and 1.0
7
- # @return [Float]
8
- attr_reader :confidence
9
- # Language code in ISO 639-1 format.
10
- # @return [String]
11
- attr_reader :language
12
- # Country code in ISO 3166-1 alpha-2 format.
13
- # @return [String, nil]
14
- attr_reader :country
15
- # Currency code in ISO 4217 format.
16
- # @return [String]
17
- attr_reader :currency
18
- # Language code, with country code when available.
19
- # @return [String]
20
- attr_reader :value
21
-
22
- # @param prediction [Hash]
23
- def initialize(prediction, _page_id = nil)
24
- value_key = if prediction.include? 'value'
25
- 'value'
26
- else
27
- 'language'
28
- end
29
- @confidence = prediction['confidence']
30
- @value = prediction[value_key]
31
- @language = prediction['language']
32
- @country = prediction['country']
33
- @currency = prediction['currency']
34
- end
35
-
36
- def to_s
37
- out_str = String.new
38
- out_str << "#{@value}; " if @value
39
- out_str << "#{@language}; " if @language
40
- out_str << "#{@country}; " if @country
41
- out_str << "#{@currency}; " if @currency
42
- out_str.strip
43
- end
44
- end
45
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base'
4
-
5
- module Mindee
6
- # Represents payment details for invoices and receipts
7
- class PaymentDetails < Field
8
- attr_reader :account_number,
9
- :iban,
10
- :routing_number,
11
- :swift
12
-
13
- # @param prediction [Hash]
14
- # @param page_id [Integer, nil]
15
- # @param reconstructed [Boolean]
16
- def initialize(prediction, page_id, reconstructed: false)
17
- super
18
- @account_number = prediction['account_number']
19
- @iban = prediction['iban']
20
- @routing_number = prediction['routing_number']
21
- @swift = prediction['swift']
22
- end
23
-
24
- def to_s
25
- out_str = String.new
26
- out_str << "#{@account_number}; " if @account_number
27
- out_str << "#{@iban}; " if @iban
28
- out_str << "#{@routing_number}; " if @routing_number
29
- out_str << "#{@swift}; " if @swift
30
- out_str.strip
31
- end
32
- end
33
- end
@@ -1,39 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Mindee
4
- # An element's position on the image
5
- class PositionField
6
- # @return [Mindee::Geometry::Polygon]
7
- attr_reader :polygon
8
- # @return [Mindee::Geometry::Polygon]
9
- attr_reader :value
10
- # @return [Mindee::Geometry::Quadrilateral]
11
- attr_reader :quadrangle
12
- # @return [Mindee::Geometry::Quadrilateral]
13
- attr_reader :rectangle
14
- # @return [Mindee::Geometry::Quadrilateral]
15
- attr_reader :bounding_box
16
-
17
- # @param prediction [Hash]
18
- # @param page_id [Integer, nil]
19
- def initialize(prediction, page_id)
20
- @polygon = Geometry.polygon_from_prediction(prediction['polygon']) unless prediction['polygon'].empty?
21
- @quadrangle = to_quadrilateral(prediction, 'quadrangle')
22
- @rectangle = to_quadrilateral(prediction, 'rectangle')
23
- @bounding_box = to_quadrilateral(prediction, 'bounding_box')
24
- @page_id = page_id || prediction['page_id']
25
- @value = @polygon
26
- end
27
-
28
- def to_s
29
- out_str = String.new
30
- out_str << "Polygon with #{@polygon.size} points."
31
- end
32
-
33
- private
34
-
35
- def to_quadrilateral(prediction, key)
36
- Geometry.quadrilateral_from_prediction(prediction[key]) unless prediction[key].empty?
37
- end
38
- end
39
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base'
4
-
5
- module Mindee
6
- # Represents tax information.
7
- class TaxField < Field
8
- # Tax value as 3 decimal float
9
- # @return [Float]
10
- attr_reader :value
11
- # Tax rate percentage
12
- # @return [Float]
13
- attr_reader :rate
14
- # Tax code
15
- # @return [String]
16
- attr_reader :code
17
-
18
- # @param prediction [Hash]
19
- # @param page_id [Integer, nil]
20
- def initialize(prediction, page_id)
21
- super
22
- @value = @value.round(3) unless @value.nil?
23
- @rate = prediction['rate'].to_f unless prediction['rate'].nil?
24
- @code = prediction['code'] unless prediction['code'] == 'None'
25
- end
26
-
27
- # @param value [Float]
28
- def print_float(value)
29
- format('%.2f', value)
30
- end
31
-
32
- def to_s
33
- out_str = String.new
34
- out_str << "#{print_float(@value)} " if @value
35
- out_str << "#{print_float(@rate)}% " if @rate
36
- out_str << "#{@code} " if @code
37
- out_str.strip
38
- end
39
- end
40
- end
@@ -1,12 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'base'
4
-
5
- module Mindee
6
- # Represents tax information.
7
- class TextField < Field
8
- # Amount value as 3 decimal float
9
- # @return [String, nil]
10
- attr_reader :value
11
- end
12
- end
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'common_fields/amount'
4
- require_relative 'common_fields/base'
5
- require_relative 'common_fields/company_registration'
6
- require_relative 'common_fields/date'
7
- require_relative 'common_fields/locale'
8
- require_relative 'common_fields/payment_details'
9
- require_relative 'common_fields/position'
10
- require_relative 'common_fields/tax'
11
- require_relative 'common_fields/text'
@@ -1,58 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative 'fields'
4
- require_relative '../base'
5
-
6
- module Mindee
7
- module Prediction
8
- # Custom document object.
9
- class CustomV1 < Prediction
10
- # All value fields in the document
11
- # @return [Hash<Symbol, Mindee::ListField>]
12
- attr_reader :fields
13
- # All classifications in the document
14
- # @return [Hash<Symbol, Mindee::ClassificationField>]
15
- attr_reader :classifications
16
-
17
- # @param prediction [Hash]
18
- # @param page_id [Integer, nil]
19
- def initialize(prediction, page_id)
20
- super
21
- @fields = {}
22
- @classifications = {}
23
- prediction.each do |field_name, field_prediction|
24
- field_sym = field_name.to_sym
25
- set_field(field_sym, field_prediction, page_id)
26
- end
27
- end
28
-
29
- def to_s
30
- out_str = String.new
31
- @classifications.each do |name, info|
32
- out_str << "\n:#{name}: #{info}".rstrip
33
- end
34
- @fields.each do |name, info|
35
- out_str << "\n:#{name}: #{info}".rstrip
36
- end
37
- out_str[1..].to_s
38
- end
39
-
40
- private
41
-
42
- # @param field_prediction [Hash]
43
- def set_field(field_sym, field_prediction, page_id)
44
- # Currently two types of fields possible in a custom API response:
45
- # fields having a list of values, and classification fields.
46
- # Here we use the fact that only value lists have the 'values' attribute.
47
-
48
- if field_prediction.key? 'values'
49
- @fields[field_sym] = ListField.new(field_prediction, page_id)
50
- elsif field_prediction.key? 'value'
51
- @classifications[field_sym] = ClassificationField.new(field_prediction)
52
- else
53
- throw 'Unknown API field type'
54
- end
55
- end
56
- end
57
- end
58
- end