Class: HocrReader::Part
- Inherits:
-
Object
- Object
- HocrReader::Part
- Defined in:
- lib/hocr_reader/part.rb
Overview
class Part
Instance Attribute Summary collapse
-
#attributes ⇒ Object
Returns the value of attribute attributes.
-
#language ⇒ Object
Returns the value of attribute language.
-
#part_name ⇒ Object
Returns the value of attribute part_name.
-
#text ⇒ Object
Returns the value of attribute text.
-
#x_end ⇒ Object
Returns the value of attribute x_end.
-
#x_start ⇒ Object
Returns the value of attribute x_start.
-
#y_end ⇒ Object
Returns the value of attribute y_end.
-
#y_start ⇒ Object
Returns the value of attribute y_start.
Instance Method Summary collapse
- #convert_to_parameters(attribute) ⇒ Object
-
#initialize(part_name, phrase, title_attributes, lang) ⇒ Part
constructor
A new instance of Part.
- #method_missing(name, *args, &block) ⇒ Object
- #numeric?(str) ⇒ Boolean
- #respond_to_missing?(name) ⇒ Boolean
- #split_the_attributes(title_attributes) ⇒ Object
- #to_numeric(anything) ⇒ Object
Constructor Details
#initialize(part_name, phrase, title_attributes, lang) ⇒ Part
Returns a new instance of Part.
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/hocr_reader/part.rb', line 11 def initialize(part_name, phrase, title_attributes, lang) @part_name = part_name[3..-2] @text = phrase.text @attributes = split_the_attributes title_attributes @x_start = bbox[0].to_i @y_start = bbox[1].to_i @x_end = bbox[2].to_i @y_end = bbox[3].to_i @language = lang end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
49 50 51 |
# File 'lib/hocr_reader/part.rb', line 49 def method_missing(name, *args, &block) @attributes[name] || super end |
Instance Attribute Details
#attributes ⇒ Object
Returns the value of attribute attributes.
8 9 10 |
# File 'lib/hocr_reader/part.rb', line 8 def attributes @attributes end |
#language ⇒ Object
Returns the value of attribute language.
8 9 10 |
# File 'lib/hocr_reader/part.rb', line 8 def language @language end |
#part_name ⇒ Object
Returns the value of attribute part_name.
8 9 10 |
# File 'lib/hocr_reader/part.rb', line 8 def part_name @part_name end |
#text ⇒ Object
Returns the value of attribute text.
8 9 10 |
# File 'lib/hocr_reader/part.rb', line 8 def text @text end |
#x_end ⇒ Object
Returns the value of attribute x_end.
8 9 10 |
# File 'lib/hocr_reader/part.rb', line 8 def x_end @x_end end |
#x_start ⇒ Object
Returns the value of attribute x_start.
8 9 10 |
# File 'lib/hocr_reader/part.rb', line 8 def x_start @x_start end |
#y_end ⇒ Object
Returns the value of attribute y_end.
8 9 10 |
# File 'lib/hocr_reader/part.rb', line 8 def y_end @y_end end |
#y_start ⇒ Object
Returns the value of attribute y_start.
8 9 10 |
# File 'lib/hocr_reader/part.rb', line 8 def y_start @y_start end |
Instance Method Details
#convert_to_parameters(attribute) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hocr_reader/part.rb', line 34 def convert_to_parameters(attribute) parameters = attribute.slice(1..-1) if parameters.length > 1 value = [] parameters.each do |parameter| value << to_numeric(parameter) end elsif numeric?(parameters[0]) value = to_numeric(parameters[0]) else value = parameters[0] end value end |
#numeric?(str) ⇒ Boolean
69 70 71 |
# File 'lib/hocr_reader/part.rb', line 69 def numeric?(str) Float(str) != nil rescue false end |
#respond_to_missing?(name) ⇒ Boolean
53 54 55 56 57 58 |
# File 'lib/hocr_reader/part.rb', line 53 def respond_to_missing?(name, *) if @attributes[name] else super end end |
#split_the_attributes(title_attributes) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/hocr_reader/part.rb', line 22 def split_the_attributes(title_attributes) attributes = {} individual_attributes = [] title_attributes.each { |attrs| individual_attributes << attrs.split(' ') } # store the attibutes as keys in a hash that point to the value(s) individual_attributes.each do |attribute| key = attribute[0].to_sym attributes[key] = convert_to_parameters attribute end attributes end |
#to_numeric(anything) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/hocr_reader/part.rb', line 60 def to_numeric(anything) num = BigDecimal(anything) if num.frac.zero? num.to_i else num.to_f end end |