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
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/bismas/cli/chardiff.rb', line 43
def run(arguments)
quit unless arguments.empty?
output_encoding, frequencies = options[:output_encoding],
Hash.new { |h, k| h[k] = Hash.new { |i, j| i[j] = [0, 0] } }
2.times { |index|
file = "file#{index + 1}"
reader_options = {
encoding: "#{options[:"#{file}_encoding"]}:#{output_encoding}",
key: key = options[:"#{file}_key"]
}
Reader.parse_file(options[file.to_sym], reader_options) { |id, record|
frequency = Hash.new(0); record.delete(key)
record.each_value { |array| array.each { |value|
value.each_char { |char| frequency[char] += 1 } } }
frequencies.values_at(0, key ? id.to_i : $.).each { |hash|
frequency.each { |char, count| hash[char][index] += count } }
}
}
File.open_file(options[:output], {}, 'w') { |io|
begin
csv = CSV.new(io) << %w[id char count1 count2 diff]
frequencies.sort_by { |id,| id }.each { |id, hash|
hash.sort_by { |char,| char }.each { |char, (count1, count2)|
unless count1 == count2
csv << [id, char, count1, count2, count2 - count1]
end
}
}
ensure
csv.close if csv
end
}
end
|