data = Array.new(256, 0)
all = 0.0
File.open(ARGV[0], "r") do |io|
io.binmode # バイナリモードにする
while text = io.read(1) do
t = text.unpack("C")[0].to_i # 1バイトずつ変換
data[t] += 1
all += 1
end
end
entropy = 0 # エントロピー
for i in 0..255 do
d = data[i]
pr = d/all
entropy += -1*pr*(Math.log(pr)/Math.log(2)) unless d == 0
print i, " ", d, "\n"
end
print "entropy is ", entropy, "\n"