summaryrefslogtreecommitdiff
path: root/avg_weights
blob: 2b7274758fbd4f8c1728315f9ab30d21324838a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/env ruby

require 'trollop'
require 'zlib'


STDIN.set_encoding 'utf-8'
STDOUT.set_encoding 'utf-8'

opts = Trollop::options do
  opt :filter, "Filter if key does not appear in every file.", :type => :bool, :default => false
end

def usage
  puts "avg_weights_filter [--filter] <filename>+"
  exit 1
end
usage if ARGV.size==0


h = {}
ARGV.each { |fn|
if File.extname(fn)=='.gz'
  f = Zlib::GzipReader.new(File.new(fn, 'rb'))
else
  f = File.new fn, 'r'
end
while line = f.gets
  k, v = line.split
  v = v.to_f
  if h.has_key? k
    h[k] << v
  else
    h[k] = [v]
  end
end
f.close
}

n = ARGV.size.to_f

h.each_pair { |k,a|
  next if opts[:filter] and a.size < n
  puts "#{k} #{a.inject(:+)/n}"
}