class Bismas::CLI::Chardiff
Public Class Methods
defaults()
click to toggle source
Calls superclass method
# File lib/bismas/cli/chardiff.rb 35 def self.defaults 36 super.merge( 37 file1_encoding: DEFAULT_ENCODING, 38 file2_encoding: DEFAULT_ENCODING, 39 output_encoding: Encoding.default_external 40 ) 41 end
Public Instance Methods
run(arguments)
click to toggle source
# File lib/bismas/cli/chardiff.rb 43 def run(arguments) 44 quit unless arguments.empty? 45 46 output_encoding, frequencies = options[:output_encoding], 47 Hash.new { |h, k| h[k] = Hash.new { |i, j| i[j] = [0, 0] } } 48 49 2.times { |index| 50 file = "file#{index + 1}" 51 52 reader_options = { 53 encoding: "#{options[:"#{file}_encoding"]}:#{output_encoding}", 54 key: key = options[:"#{file}_key"] 55 } 56 57 Reader.parse_file(options[file.to_sym], reader_options) { |id, record| 58 frequency = Hash.new(0); record.delete(key) 59 60 record.each_value { |array| array.each { |value| 61 value.each_char { |char| frequency[char] += 1 } } } 62 63 frequencies.values_at(0, key ? id.to_i : $.).each { |hash| 64 frequency.each { |char, count| hash[char][index] += count } } 65 } 66 } 67 68 File.open_file(options[:output], {}, 'w') { |io| 69 begin 70 csv = CSV.new(io) << %w[id char count1 count2 diff] 71 72 frequencies.sort_by { |id,| id }.each { |id, hash| 73 hash.sort_by { |char,| char }.each { |char, (count1, count2)| 74 unless count1 == count2 75 csv << [id, char, count1, count2, count2 - count1] 76 end 77 } 78 } 79 ensure 80 csv.close if csv 81 end 82 } 83 end
Private Instance Methods
opts(opts)
click to toggle source
# File lib/bismas/cli/chardiff.rb 87 def opts(opts) 88 opts.option(:file1__FILE, :i, 'Path to input file 1 [Required]') 89 opts.option(:file2__FILE, :j, 'Path to input file 2 [Required]') 90 91 opts.separator 92 93 opts.option(:output__FILE, 'Path to output file [Default: STDOUT]') 94 95 opts.separator 96 opts.separator 'Input options:' 97 98 opts.option(:file1_encoding__ENCODING, :N, "File 1 encoding [Default: #{DEFAULT_ENCODING}]") 99 opts.option(:file2_encoding__ENCODING, :O, "File 2 encoding [Default: #{DEFAULT_ENCODING}]") 100 101 opts.separator 102 103 opts.option(:file1_key__KEY, :K, 'ID key of file 1') 104 opts.option(:file2_key__KEY, :L, 'ID key of file 2') 105 106 opts.separator 107 opts.separator 'Output options:' 108 109 opts.option(:output_encoding__ENCODING, :n, "Output encoding [Default: #{defaults[:output_encoding]}]") 110 end