Class: Mindee::V1::Product::Universal::UniversalPrediction
- Inherits:
-
Mindee::V1::Parsing::Common::Prediction
- Object
- Mindee::V1::Parsing::Common::Prediction
- Mindee::V1::Product::Universal::UniversalPrediction
- Includes:
- Mindee::V1::Parsing::Common, Mindee::V1::Parsing::Standard, Mindee::V1::Parsing::Universal
- Defined in:
- lib/mindee/v1/product/universal/universal_prediction.rb,
sig/mindee/v1/product/universal/universal_prediction.rbs
Overview
Universal Document V1 page.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fields ⇒ Hash<Symbol, Mindee::V1::Parsing::Universal::UniversalListField>
readonly
All value fields in the document.
Instance Method Summary collapse
- #generate_field_string(field_name, field_value, pattern) ⇒ String
- #generate_list_field_string(field_name, field_value, pattern) ⇒ String
- #generate_sub_value_string(field_name, sub_value, pattern) ⇒ String
-
#initialize(_ = nil) ⇒ UniversalPrediction
constructor
A new instance of UniversalPrediction.
-
#list_field_names ⇒ Array<String>
Lists names of all top-level field keys.
-
#list_fields ⇒ Hash<String, UniversalListField>
Returns a hash of all list-like fields.
-
#object_fields ⇒ Hash<String, UniversalObjectField>
Returns a hash of all object-like fields.
-
#single_fields ⇒ Hash<String, StringField>
Returns a hash of all fields that aren't a collection.
-
#to_s ⇒ String
String representation.
Methods included from Mindee::V1::Parsing::Universal
Constructor Details
#initialize(_ = nil) ⇒ UniversalPrediction
Returns a new instance of UniversalPrediction.
19 20 21 22 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 19 def initialize(_ = nil) super @fields = {} # : Hash[String | Symbol, untyped] end |
Instance Attribute Details
#fields ⇒ Hash<Symbol, Mindee::V1::Parsing::Universal::UniversalListField> (readonly)
All value fields in the document
17 18 19 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 17 def fields @fields end |
Instance Method Details
#generate_field_string(field_name, field_value, pattern) ⇒ String
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 45 def generate_field_string(field_name, field_value, pattern) values = field_value.values return '' if values.nil? || values.empty? str_value = '' first_value = values[0] str_value += if first_value.is_a?(Parsing::Universal::UniversalObjectField) first_value.str_level(1).sub(pattern, '\\1* :') else "#{first_value.to_s.sub(pattern, '\\1* :')}\n" end Array(values[1..]).each do |sub_value| str_value += if sub_value.is_a?(Parsing::Universal::UniversalObjectField) sub_value.str_level(1).sub(pattern, '\\1* :') else "#{' ' * (field_name.length + 2)} #{sub_value}\n" end end str_value.rstrip end |
#generate_list_field_string(field_name, field_value, pattern) ⇒ String
67 68 69 70 71 72 73 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 67 def generate_list_field_string(field_name, field_value, pattern) str_value = '' field_value.each_value do |sub_value| str_value += generate_sub_value_string(field_name, sub_value, pattern) end str_value.rstrip end |
#generate_sub_value_string(field_name, sub_value, pattern) ⇒ String
75 76 77 78 79 80 81 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 75 def generate_sub_value_string(field_name, sub_value, pattern) if sub_value.is_a?(Mindee::V1::Parsing::Universal::UniversalObjectField) sub_value.str_level(1).gsub(pattern, '\1* :') else (' ' * (field_name.length + 2)) + "#{sub_value}\n" end end |
#list_field_names ⇒ Array<String>
Lists names of all top-level field keys
121 122 123 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 121 def list_field_names @fields.keys.map(&:to_s) end |
#list_fields ⇒ Hash<String, UniversalListField>
Returns a hash of all list-like fields
95 96 97 98 99 100 101 102 103 104 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 95 def list_fields list_fields = {} # : Hash[String | Symbol, Mindee::V1::Parsing::Universal::UniversalListField] @fields.each do |field_name, field_value| if field_value.is_a?(Mindee::V1::Parsing::Universal::UniversalListField) list_fields[field_name] = field_value end end list_fields end |
#object_fields ⇒ Hash<String, UniversalObjectField>
Returns a hash of all object-like fields
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 108 def object_fields object_fields = {} # : Hash[String | Symbol, untyped] @fields.each do |field_name, field_value| if field_value.is_a?(Mindee::V1::Parsing::Universal::UniversalObjectField) object_fields[field_name] = field_value end end object_fields end |
#single_fields ⇒ Hash<String, StringField>
Returns a hash of all fields that aren't a collection
85 86 87 88 89 90 91 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 85 def single_fields single_fields = {} # : Hash[String | Symbol, untyped] @fields.each do |field_name, field_value| single_fields[field_name] = field_value if field_value.is_a?(Mindee::V1::Parsing::Standard::StringField) end single_fields end |
#to_s ⇒ String
String representation.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/mindee/v1/product/universal/universal_prediction.rb', line 25 def to_s out_str = '' pattern = %r{^(\n* *)( {2}):} @fields.each do |field_name, field_value| str_value = if field_value.is_a?( Mindee::V1::Parsing::Universal::UniversalListField ) && field_value.values.length.positive? generate_field_string(field_name, field_value, pattern) else field_value.to_s end out_str += "\n:#{field_name}:" out_str += " #{str_value}".sub(%r{^\s+\n}, "\n") if str_value.length.positive? end out_str.sub("\n", '') end |