Class: HocrReader::Part

Inherits:
Object
  • Object
show all
Defined in:
lib/hocr_reader/part.rb

Overview

class Part

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/hocr_reader/part.rb', line 8

def attributes
  @attributes
end

#languageObject

Returns the value of attribute language.



8
9
10
# File 'lib/hocr_reader/part.rb', line 8

def language
  @language
end

#part_nameObject

Returns the value of attribute part_name.



8
9
10
# File 'lib/hocr_reader/part.rb', line 8

def part_name
  @part_name
end

#textObject

Returns the value of attribute text.



8
9
10
# File 'lib/hocr_reader/part.rb', line 8

def text
  @text
end

#x_endObject

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_startObject

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_endObject

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_startObject

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

Returns:

  • (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

Returns:

  • (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