Sha256: 37e6b3fc558d8523a53b6987b581ebac08852c3f541ee4cfbd6cc731711e2efa
Contents?: true
Size: 1.06 KB
Versions: 240
Compression:
Stored size: 1.06 KB
Contents
class OcrNumbers DIGITS = { ' _ | ||_| ' => '0', ' | | ' => '1', ' _ _||_ ' => '2', ' _ _| _| ' => '3', ' |_| | ' => '4', ' _ |_ _| ' => '5', ' _ |_ |_| ' => '6', ' _ | | ' => '7', ' _ |_||_| ' => '8', ' _ |_| _| ' => '9' } DIGITS.default = '?' def self.convert(text) OcrNumbers.new(text).convert end def initialize(text) @text = text end def convert raise ArgumentError unless valid? numbers.map {|lines| decode(lines) }.join(',') end private attr_reader :text def valid? (rows.size % 4).zero? && (first_row_width % 3).zero? && rows[1..-1].all? {|row| row.size == first_row_width } end def decode(lines) lines.map {|line| line.scan(/.../) }. transpose. map {|char_rows| DIGITS[char_rows.join] }. join end def numbers text.split("\n").each_slice(4) end def rows @rows ||= text.split("\n") end def first_row_width @first_row_width ||= rows.first.size end end module BookKeeping VERSION = 1 end
Version data entries
240 entries across 240 versions & 1 rubygems