浏览代码

~/.bin/entropy: Calculate entropy per input line

Inspired by [this comment][comment] and very shortly described [on my
blog][blog].

[comment]: https://news.ycombinator.com/item?id=12819675
[blog]: https://papill0n.org/blog.html#git-ls-files-xargs-cat-entropy-rb-sort-tail-n20
Lucas Stadler 9 年之前
父节点
当前提交
0dc31cbe3b
共有 1 个文件被更改,包括 24 次插入0 次删除
  1. 24 0
      .bin/entropy.rb

+ 24 - 0
.bin/entropy.rb

@ -0,0 +1,24 @@
1
#!/usr/bin/env ruby
2
3
def shannon_entropy(s)
4
  d = {}
5
  s.each_char do |c|
6
    d[c] ||= 0.0
7
    d[c] += 1
8
  end
9
10
  res = 0.0
11
  d.each_value do |v|
12
    freq = v / s.length
13
    res -= freq * (Math.log(freq) / Math.log(2))
14
  end
15
16
  res
17
end
18
19
if __FILE__ == $0
20
  $stdin.each_line do |line|
21
    e = shannon_entropy(line)
22
    puts format("%.4f\t%s", e, line)
23
  end
24
end