Module: DSR
- Defined in:
- lib/dsr.rb,
lib/dsr/recognizer.rb
Defined Under Namespace
Modules: Recognizer
Class Method Summary collapse
- .capybara2struct(nodes) ⇒ Object
- .google2struct(json) ⇒ Object
-
.link(headers, array, direction, alignment, *priority) ⇒ Object
TODO: refactor or deprecate?.
- .nodes2struct(nodes) ⇒ Object
- .pdf2struct(object) ⇒ Object
- .subgraphs(data) ⇒ Object
Class Method Details
.capybara2struct(nodes) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/dsr.rb', line 125 def self. nodes nodes.map do |node| rect = node.rect StructLinkable.new node, rect.x, rect.y + rect.height, rect.x + rect.width, rect.y end end |
.google2struct(json) ⇒ Object
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 53 54 |
# File 'lib/dsr.rb', line 18 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| StructWithText.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 |
.link(headers, array, direction, alignment, *priority) ⇒ Object
TODO: refactor or deprecate?
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dsr.rb', line 57 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 |
.nodes2struct(nodes) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/dsr.rb', line 110 def self.nodes2struct nodes nodes.map do |node| StructLinkable.new(node, *::JSON.load(node.page.evaluate(<<~HEREDOC, node))) ( function(node) { var x = scrollX, y = scrollY; var rect = JSON.parse(JSON.stringify(node.getBoundingClientRect())); rect.top += scrollY; rect.left += scrollX; var t = JSON.stringify( [rect.left, rect.bottom, rect.right, rect.top] ); return t; } )(arguments[0]) HEREDOC end end |
.pdf2struct(object) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dsr.rb', line 75 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 StructWithText.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
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/dsr.rb', line 97 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 |