Module: PdfExtract::Spatial

Defined in:
lib/spatial.rb

Constant Summary collapse

@@default_options =
{
  :separator => '',
  :lines => false,
  :write_mode => :left_to_right
}
@@spatial_attribs =
[:x, :y, :width, :height, :page_width, :page_height, :page]

Class Method Summary collapse

Class Method Details

.as_line(obj) ⇒ Object



94
95
96
# File 'lib/spatial.rb', line 94

def self.as_line obj
  get_dimensions(obj).merge({:content => obj[:content]})
end

.collapse(objs, options = {}) ⇒ Object

Collapse a list of objects into one. Will merge objects in the correct write order, specified by write_mode.



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/spatial.rb', line 116

def self.collapse objs, options={}
  options = @@default_options.merge options

  sorted = case write_mode
           when :left_to_right
             objs.sort_by { |obj| -(obj[:y].floor * 100) + (obj[:x] / 100.0) }
           end

  if sorted.count == 1
    sorted.first.dup
  else
    o = sorted.delete_at(0).dup
    while not sorted.count.zero?
      merge o, sorted.delete_at(0)
    end
    o
  end
end

.concat_lines(top, bottom) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/spatial.rb', line 13

def self.concat_lines top, bottom
  if top =~ /\-\Z/
    top[0..-2] + bottom
  else
    top + ' ' + bottom
  end
end

.contains?(a, b, padding = 0) ⇒ Boolean

Returns:

  • (Boolean)


135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/spatial.rb', line 135

def self.contains? a, b, padding=0
  a_x1 = a[:x] - padding
  a_x2 = a[:x] + a[:width] + (padding * 2)
  a_y1 = a[:y] - padding
  a_y2 = a[:y] + a[:height] + (padding * 2)

  b_x1 = b[:x]
  b_x2 = b[:x] + b[:width]
  b_y1 = b[:y]
  b_y2 = b[:y] + b[:height]

  b_x1 >= a_x1 && b_x2 <= a_x2 && b_y1 >= a_y1 && b_y2 <= a_y2
end

.drop_spatial(obj) ⇒ Object



21
22
23
# File 'lib/spatial.rb', line 21

def self.drop_spatial obj
  obj.dup.delete_if { |k, v| @@spatial_attribs.include? k }
end

.get_dimensions(obj) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/spatial.rb', line 82

def self.get_dimensions obj
  {
    :x => obj[:x],
    :y => obj[:y],
    :width => obj[:width],
    :height => obj[:height],
    :page => obj[:page],
    :page_width => obj[:page_width],
    :page_height => obj[:page_height]
  }
end

.get_text_content(obj) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/spatial.rb', line 98

def self.get_text_content obj
  if obj[:lines]
    obj[:lines].map do |line|
      if line[:content] =~ /\-\Z/
        line[:content][0..-2]
      else
        line[:content] + " "
      end
    end.join("").strip
  elsif obj[:content]
    obj[:content]
  else
    ""
  end
end

.line_count(obj) ⇒ Object



75
76
77
78
79
80
# File 'lib/spatial.rb', line 75

def self.line_count obj
  line_count = 0
  line_count += obj[:content].count("\n") + 1 if obj[:content]
  line_count += obj[:lines].length if obj[:lines]
  line_count
end

.merge(a, b, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/spatial.rb', line 43

def self.merge a, b, options={}
  options = @@default_options.merge options

  bottom_left = [ [a[:x], b[:x]].min, [a[:y], b[:y]].min ]
  top_right = [ [a[:x] + a[:width], b[:x] + b[:width]].max,
                [a[:y] + a[:height], b[:y] + b[:height]].max ]

  so = a.merge(b).merge({
    :x => bottom_left[0],
    :y => bottom_left[1],
    :width => top_right[0] - bottom_left[0],
    :height => top_right[1] - bottom_left[1]
  })

  if options[:lines]
    merge_lines a, b, so
  else
    so[:content] = (a[:content] + options[:separator] + b[:content])
    so[:content] = so[:content].gsub /\s+/, " "
  end

  if get_text_content(a).length > get_text_content(b).length
    so[:font] = a[:font]
    so[:line_height] = a[:line_height]
  else
    so[:font] = b[:font]
    so[:line_height] = b[:line_height]
  end

  so
end

.merge_lines(a, b, so) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/spatial.rb', line 25

def self.merge_lines a, b, so
  so[:lines] = []

  if a.key? :lines
    so[:lines] += a[:lines]
  else
    so[:lines] << as_line(a)
  end

  if b.key? :lines
    so[:lines] += b[:lines]
  else
    so[:lines] << as_line(b)
  end

  so
end

.overlap?(from, by, a, b) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
152
153
154
# File 'lib/spatial.rb', line 149

def self.overlap? from, by, a, b
  a_top = a[from] + a[by]
  b_top = b[rom] + b[by]

  (b_top <= a_top && b_top >= a[from]) || (b[from] >= a[from] && b[from] <= b_top)
end

.score(items, ideals, name) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/spatial.rb', line 156

def self.score items, ideals, name
  ideals.keys.each do |f|
    diffs = items.map {|item| (item[f] - ideals[f][0]).abs}
    diffs.map! {|d| d.nan? ? 1 : d}
    max_diff = diffs.max

    scores = diffs.map do |d|
      if d == 0
        ideals[f][1]
      else
        (1 - (d / max_diff)) * ideals[f][1]
      end
    end

    items.each_index do |i|
      items[i][name] ||= 0
      items[i][name] = items[i][name] + scores[i]
    end
  end
end