Module: DSR

Defined in:
lib/dsr.rb

Class Method Summary collapse

Class Method Details

.google2struct(json) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/dsr.rb', line 16

def self.google2struct json
  require "json"
  Texts.new( JSON.load(json).tap do |json|
    require "nakischema"
    Nakischema.validate json, {
      hash_req: {
      "cropHintsAnnotation" => Hash,
      "fullTextAnnotation" => Hash,
      "imagePropertiesAnnotation" => Hash,
      "labelAnnotations" => Array,
      "safeSearchAnnotation" => Hash,
      "textAnnotations" => { each: {
        hash_req: {
          "boundingPoly" => { hash: {
            "vertices" => { size: 4..4, each: { hash: { "x" => Integer, "y" => Integer } } },
          } },
          "description" => /\A\S(.*\S)?\z/m,
        },
        hash_opt: {
          "locale" => /\A\S(.*\S)?\z/m,
        },
      } },
      },
      hash_opt: {
        "localizedObjectAnnotations" => Array,
      }
    }
  end["textAnnotations"].map do |text|
    Struct.new text["description"],
               text["boundingPoly"]["vertices"].map{ |_| _["x"] }.min,
               text["boundingPoly"]["vertices"].map{ |_| _["y"] }.max,
               text["boundingPoly"]["vertices"].map{ |_| _["x"] }.max,
               text["boundingPoly"]["vertices"].map{ |_| _["y"] }.min,
               text["boundingPoly"]["vertices"].map{ |_| _["x"] }.max - text["boundingPoly"]["vertices"].map{ |_| _["x"] }.min,
               text["boundingPoly"]["vertices"].map{ |_| _["y"] }.max - text["boundingPoly"]["vertices"].map{ |_| _["y"] }.min
  end )
end


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/dsr.rb', line 54

def self.link headers, array, direction, alignment, *priority
  l, r = case direction
  when :horizontal ; %i{ left right }
  when :vertical ; %i{ top bottom }
  else ; fail "invalid direction"
  end
  headers = headers.sort_by(&l).map(&:dup)
  headers.each_cons(2){ |a, b| a[r], b[l] = [a[r], b[l]].max, [a[r], b[l]].min }
  headers.first[l] = -Float::INFINITY
  headers.last[r] = +Float::INFINITY
  headers.unshift headers.delete_at headers.index{ |_| priority.include? _.text } unless priority.empty?   # TODO: document/explain this
  array.sort_by(&l).each_with_object([]) do |cell, a|
    i = headers.public_send(alignment){ |_| (_[l].._[r]).include?((cell[l]+cell[r])/2) }
    a[i] ||= []
    a[i] << cell
  end
end

.pdf2struct(object) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dsr.rb', line 71

def self.pdf2struct object
  require "hexapdf"
  processor = Class.new HexaPDF::Content::Processor do
    attr_reader :texts
    def initialize _
      super
      @texts = Texts.new
    end
    def show_text str
      boxes = decode_text_with_positioning str
      @texts.push Struct.new boxes.string,
        boxes.lower_left[0], -boxes.lower_left[1],
        boxes.upper_right[0], -boxes.upper_right[1],
        boxes.upper_right[0] - boxes.lower_left[0],
        boxes.lower_left[1] - boxes.upper_right[1]
    end
  end
  HexaPDF::Document.new(io: object).pages.map do |page|
    processor.new(page).tap(&page.method(:process_contents)).texts
  end
end

.subgraphs(data) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/dsr.rb', line 93

def self.subgraphs data
  data.zip.tap do |array|
    (0...data.size).each do |i|
      (0...i).to_a.select do |j|
        array[i].product(array[j]).any?{ |i,j| yield i,j }
      end.each do |j|
        array[i].concat array[j]
        array[j].clear
      end
    end
  end.reject &:empty?
end