Skip to content

Commit a2aafe6

Browse files
authored
🎨 update and improve rST output of documents (#22)
1 parent 8036ad2 commit a2aafe6

File tree

13 files changed

+134
-149
lines changed

13 files changed

+134
-149
lines changed

lib/mindee/parsing/prediction/common_fields/locale.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Locale
2020
attr_reader :value
2121

2222
# @param prediction [Hash]
23-
def initialize(prediction)
23+
def initialize(prediction, _page_id = nil)
2424
value_key = if prediction.include? 'value'
2525
'value'
2626
else

lib/mindee/parsing/prediction/eu/license_plate/license_plate_v1.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
module Mindee
77
module Prediction
88
module EU
9-
# License plate prediction.
9+
# License Plate v1 prediction results.
1010
class LicensePlateV1 < Prediction
11-
# A list of license plates.
11+
# List of all license plates found in the image.
1212
# @return [Array<Mindee::TextField>]
1313
attr_reader :license_plates
1414

@@ -23,9 +23,9 @@ def initialize(prediction, page_id)
2323
end
2424

2525
def to_s
26-
license_plates = @license_plates.map(&:value).join(', ')
26+
license_plates = @license_plates.join("\n #{' ' * 16}")
2727
out_str = String.new
28-
out_str << "\n:License plates: #{license_plates}".rstrip
28+
out_str << "\n:License Plates: #{license_plates}".rstrip
2929
out_str[1..].to_s
3030
end
3131
end

lib/mindee/parsing/prediction/financial_document/financial_document_v1.rb

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require_relative '../common_fields'
44
require_relative '../base'
5-
require_relative 'invoice_line_item'
5+
require_relative 'line_item'
66

77
module Mindee
88
module Prediction
@@ -60,7 +60,7 @@ class FinancialDocumentV1 < Prediction
6060
# @return [Array<Mindee::CompanyRegistration>]
6161
attr_reader :supplier_company_registrations
6262
# Line items details.
63-
# @return [Array<Mindee::InvoiceLineItem>]
63+
# @return [Array<Mindee::FinancialDocumentLineItem>]
6464
attr_reader :line_items
6565
# Time as seen on the receipt in HH:MM format.
6666
# @return [Mindee::TextField]
@@ -83,7 +83,6 @@ class FinancialDocumentV1 < Prediction
8383
# @param page_id [Integer, nil]
8484
def initialize(prediction, page_id) # rubocop:todo Metrics/AbcSize
8585
super
86-
8786
@time = TextField.new(prediction['time'], page_id)
8887
@category = TextField.new(prediction['category'], page_id)
8988
@subcategory = TextField.new(prediction['subcategory'], page_id)
@@ -100,7 +99,6 @@ def initialize(prediction, page_id) # rubocop:todo Metrics/AbcSize
10099
@invoice_number = TextField.new(prediction['invoice_number'], page_id)
101100
@supplier_name = TextField.new(prediction['supplier_name'], page_id)
102101
@supplier_address = TextField.new(prediction['supplier_address'], page_id)
103-
104102
@reference_numbers = []
105103
prediction['reference_numbers'].each do |item|
106104
@reference_numbers.push(TextField.new(item, page_id))
@@ -121,14 +119,12 @@ def initialize(prediction, page_id) # rubocop:todo Metrics/AbcSize
121119
prediction['supplier_company_registrations'].each do |item|
122120
@supplier_company_registrations.push(CompanyRegistration.new(item, page_id))
123121
end
124-
125122
@total_tax = AmountField.new(
126123
{ value: nil, confidence: 0.0 }, page_id
127124
)
128-
129125
@line_items = []
130126
prediction['line_items'].each do |item|
131-
@line_items.push(InvoiceLineItem.new(item, page_id))
127+
@line_items.push(FinancialDocumentLineItem.new(item, page_id))
132128
end
133129
reconstruct(page_id)
134130
end
@@ -153,39 +149,35 @@ def to_s
153149
out_str << "\n:Supplier address: #{@supplier_address}".rstrip
154150
out_str << "\n:Supplier company registrations: #{supplier_company_registrations}".rstrip
155151
out_str << "\n:Supplier payment details: #{supplier_payment_details}".rstrip
156-
157152
out_str << "\n:Customer name: #{@customer_name}".rstrip
158153
out_str << "\n:Customer address: #{@customer_address}".rstrip
159154
out_str << "\n:Customer company registrations: #{customer_company_registrations}".rstrip
160-
161155
out_str << "\n:Tip: #{@tip}".rstrip
162-
163156
out_str << "\n:Taxes: #{taxes}".rstrip
164157
out_str << "\n:Total taxes: #{@total_tax}".rstrip
165158
out_str << "\n:Total net: #{@total_net}".rstrip
166159
out_str << "\n:Total amount: #{@total_amount}".rstrip
167-
160+
out_str << "\n:Line Items:"
168161
out_str << line_items_to_s
169-
170162
out_str[1..].to_s
171163
end
172164

173165
private
174166

167+
def line_item_separator(char)
168+
" +#{char * 22}+#{char * 9}+#{char * 9}+#{char * 10}+#{char * 18}+#{char * 38}+"
169+
end
170+
175171
def line_items_to_s
176-
line_item_separator = "#{'=' * 22} #{'=' * 8} #{'=' * 9} #{'=' * 10} #{'=' * 18} #{'=' * 36}"
177-
line_items = @line_items.map(&:to_s).join("\n")
172+
return '' if @line_items.empty?
178173

174+
line_items = @line_items.map(&:to_s).join("\n#{line_item_separator('-')}\n ")
179175
out_str = String.new
180-
out_str << "\n\n:Line Items:"
181-
182-
return out_str if line_items.empty?
183-
184-
out_str << "\n#{line_item_separator}"
185-
out_str << "\nCode QTY Price Amount Tax (Rate) Description"
186-
out_str << "\n#{line_item_separator}"
187-
out_str << "\n#{line_items}"
188-
out_str << "\n#{line_item_separator}"
176+
out_str << "\n#{line_item_separator('-')}"
177+
out_str << "\n | Code#{' ' * 17}| QTY | Price | Amount | Tax (Rate) | Description#{' ' * 26}|"
178+
out_str << "\n#{line_item_separator('=')}"
179+
out_str << "\n #{line_items}"
180+
out_str << "\n#{line_item_separator('-')}"
189181
end
190182

191183
def reconstruct(page_id)

lib/mindee/parsing/prediction/invoice/invoice_line_item.rb renamed to lib/mindee/parsing/prediction/financial_document/line_item.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module Mindee
66
# Line items for invoices
7-
class InvoiceLineItem
7+
class FinancialDocumentLineItem
88
# @return [String] The product code referring to the item.
99
attr_reader :product_code
1010
# @return [String]
@@ -42,17 +42,15 @@ def initialize(prediction, page_id)
4242
def to_s
4343
tax = Field.float_to_string(@tax_amount)
4444
tax << " (#{Field.float_to_string(@tax_rate)}%)" unless @tax_rate.nil?
45-
4645
description = @description.nil? ? '' : @description
4746
description = "#{description[0..32]}..." if description.size > 35
48-
4947
out_str = String.new
50-
out_str << format('%- 22s', @product_code)
51-
out_str << " #{format('%- 8s', Field.float_to_string(@quantity))}"
52-
out_str << " #{format('%- 9s', Field.float_to_string(@unit_price))}"
53-
out_str << " #{format('%- 10s', Field.float_to_string(@total_amount))}"
54-
out_str << " #{format('%- 18s', tax)}"
55-
out_str << " #{description}"
48+
out_str << format('| %- 20s', @product_code)
49+
out_str << " #{format('| %- 7s', Field.float_to_string(@quantity))}"
50+
out_str << " #{format('| %- 7s', Field.float_to_string(@unit_price))}"
51+
out_str << " #{format('| %- 8s', Field.float_to_string(@total_amount))}"
52+
out_str << " #{format('| %- 16s', tax)}"
53+
out_str << " #{format('| %- 37s|', description)}"
5654
end
5755
end
5856
end

lib/mindee/parsing/prediction/fr/bank_account_details/bank_account_details_v1.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
module Mindee
77
module Prediction
88
module FR
9-
# French bank account information (RIB)
9+
# Bank Account Details v1 prediction results.
1010
class BankAccountDetailsV1 < Prediction
11-
# The account's IBAN.
11+
# The International Bank Account Number (IBAN).
1212
# @return [Mindee::TextField]
1313
attr_reader :iban
14-
# The account holder's name.
14+
# The name of the account holder as seen on the document.
1515
# @return [Mindee::TextField]
1616
attr_reader :account_holder_name
17-
# The bank's SWIFT code.
17+
# The bank's SWIFT Business Identifier Code (BIC).
1818
# @return [Mindee::TextField]
1919
attr_reader :swift
2020

@@ -30,8 +30,8 @@ def initialize(prediction, page_id)
3030
def to_s
3131
out_str = String.new
3232
out_str << "\n:IBAN: #{@iban}".rstrip
33-
out_str << "\n:Account holder name: #{@account_holder_name}".rstrip
34-
out_str << "\n:SWIFT: #{@swift}".rstrip
33+
out_str << "\n:Account Holder's Name: #{@account_holder_name}".rstrip
34+
out_str << "\n:SWIFT Code: #{@swift}".rstrip
3535
out_str[1..].to_s
3636
end
3737
end

lib/mindee/parsing/prediction/fr/carte_vitale/carte_vitale_v1.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
module Mindee
77
module Prediction
88
module FR
9-
# French Carte Vitale
9+
# Carte Vitale v1 prediction results.
1010
class CarteVitaleV1 < Prediction
11-
# List of given (first) names of the cardholder.
11+
# The given name(s) of the card holder.
1212
# @return [Array<Mindee::TextField>]
1313
attr_reader :given_names
14-
# The surname (last name) of the cardholder.
14+
# The surname of the card holder.
1515
# @return [Mindee::TextField]
1616
attr_reader :surname
17-
# The social security number of the cardholder.
17+
# The Social Security Number (Numéro de Sécurité Sociale) of the card holder
1818
# @return [Mindee::TextField]
1919
attr_reader :social_security
20-
# The issuance date of the card.
20+
# The date the card was issued.
2121
# @return [Mindee::DateField]
2222
attr_reader :issuance_date
2323

@@ -35,12 +35,12 @@ def initialize(prediction, page_id)
3535
end
3636

3737
def to_s
38-
given_names = @given_names.map(&:value).join(', ')
38+
given_names = @given_names.join("\n #{' ' * 15}")
3939
out_str = String.new
40-
out_str << "\n:Given names: #{given_names}".rstrip
40+
out_str << "\n:Given Name(s): #{given_names}".rstrip
4141
out_str << "\n:Surname: #{@surname}".rstrip
4242
out_str << "\n:Social Security Number: #{@social_security}".rstrip
43-
out_str << "\n:Issuance date: #{@issuance_date}".rstrip
43+
out_str << "\n:Issuance Date: #{@issuance_date}".rstrip
4444
out_str[1..].to_s
4545
end
4646
end

lib/mindee/parsing/prediction/fr/id_card/id_card_v1.rb

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,39 @@
66
module Mindee
77
module Prediction
88
module FR
9-
# French national ID card
9+
# Carte Nationale d'Identité v1 prediction results.
1010
class IdCardV1 < Prediction
11-
# The authority which has issued the card.
12-
# @return [Array<Mindee::TextField>]
13-
attr_reader :authority
14-
# Indicates if it is the recto side, the verso side or both.
11+
# The side of the document which is visible.
1512
# @return [Mindee::TextField]
1613
attr_reader :document_side
17-
# The card number.
14+
# The identification card number.
1815
# @return [Mindee::TextField]
1916
attr_reader :id_number
20-
# The expiration date of the card.
21-
# @return [Mindee::DateField]
22-
attr_reader :expiry_date
23-
# The surname (last name) of the cardholder.
17+
# The given name(s) of the card holder.
18+
# @return [Array<Mindee::TextField>]
19+
attr_reader :given_names
20+
# The surname of the card holder.
2421
# @return [Mindee::TextField]
2522
attr_reader :surname
26-
# List of first (given) names of the cardholder.
27-
# @return [Mindee::TextField]
28-
attr_reader :given_names
29-
# The date of birth of the cardholder.
23+
# The date of birth of the card holder.
3024
# @return [Mindee::DateField]
3125
attr_reader :birth_date
32-
# The place of birth of the cardholder.
26+
# The place of birth of the card holder.
3327
# @return [Mindee::TextField]
3428
attr_reader :birth_place
35-
# The sex or gender of the cardholder.
29+
# The expiry date of the identification card.
30+
# @return [Mindee::DateField]
31+
attr_reader :expiry_date
32+
# The name of the issuing authority.
33+
# @return [Mindee::TextField]
34+
attr_reader :authority
35+
# The gender of the card holder.
3636
# @return [Mindee::TextField]
3737
attr_reader :gender
38-
# The value of the first MRZ line.
38+
# Machine Readable Zone, first line
3939
# @return [Mindee::TextField]
4040
attr_reader :mrz1
41-
# The value of the second MRZ line.
41+
# Machine Readable Zone, second line
4242
# @return [Mindee::TextField]
4343
attr_reader :mrz2
4444

@@ -49,33 +49,34 @@ def initialize(prediction, page_id)
4949
@document_side = TextField.new(prediction['document_side'], page_id) if page_id
5050
@authority = TextField.new(prediction['authority'], page_id)
5151
@id_number = TextField.new(prediction['id_number'], page_id)
52+
@given_names = []
53+
prediction['given_names'].each do |item|
54+
@given_names.push(TextField.new(item, page_id))
55+
end
56+
@surname = TextField.new(prediction['surname'], page_id)
5257
@birth_date = DateField.new(prediction['birth_date'], page_id)
53-
@expiry_date = DateField.new(prediction['expiry_date'], page_id)
5458
@birth_place = TextField.new(prediction['birth_place'], page_id)
59+
@expiry_date = DateField.new(prediction['expiry_date'], page_id)
60+
@authority = TextField.new(prediction['authority'], page_id)
5561
@gender = TextField.new(prediction['gender'], page_id)
56-
@surname = TextField.new(prediction['surname'], page_id)
5762
@mrz1 = TextField.new(prediction['mrz1'], page_id)
5863
@mrz2 = TextField.new(prediction['mrz2'], page_id)
59-
@given_names = []
60-
prediction['given_names'].each do |item|
61-
@given_names.push(TextField.new(item, page_id))
62-
end
6364
end
6465

6566
def to_s
66-
given_names = @given_names.map(&:value).join(', ')
67+
given_names = @given_names.join("\n #{' ' * 15}")
6768
out_str = String.new
68-
out_str << "\n:Document side: #{@document_side}".rstrip if @document_side
69-
out_str << "\n:Authority: #{@authority}".rstrip
70-
out_str << "\n:Given names: #{given_names}".rstrip
69+
out_str << "\n:Document Side: #{@document_side}".rstrip if @document_side
70+
out_str << "\n:Identity Number: #{@id_number}".rstrip
71+
out_str << "\n:Given Name(s): #{given_names}".rstrip
7172
out_str << "\n:Surname: #{@surname}".rstrip
73+
out_str << "\n:Date of Birth: #{@birth_date}".rstrip
74+
out_str << "\n:Place of Birth: #{@birth_place}".rstrip
75+
out_str << "\n:Expiry Date: #{@expiry_date}".rstrip
76+
out_str << "\n:Issuing Authority: #{@authority}".rstrip
7277
out_str << "\n:Gender: #{@gender}".rstrip
73-
out_str << "\n:ID Number: #{@id_number}".rstrip
74-
out_str << "\n:Birth date: #{@birth_date}".rstrip
75-
out_str << "\n:Birth place: #{@birth_place}".rstrip
76-
out_str << "\n:Expiry date: #{@expiry_date}".rstrip
77-
out_str << "\n:MRZ 1: #{@mrz1}".rstrip
78-
out_str << "\n:MRZ 2: #{@mrz2}".rstrip
78+
out_str << "\n:MRZ Line 1: #{@mrz1}".rstrip
79+
out_str << "\n:MRZ Line 2: #{@mrz2}".rstrip
7980
out_str[1..].to_s
8081
end
8182
end

0 commit comments

Comments
 (0)