-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathratio.py
48 lines (39 loc) · 1.27 KB
/
ratio.py
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
47
48
import optparse
import sys
import itertools
from chatstatlib import counterToRank
from chatstatlib.util import rankToFile
options = optparse.OptionParser()
options.add_option("-o", "--outfile", dest="outfile", default="-",
help="Output file (defaults to stdin)")
def main():
opts, args = options.parse_args()
denominatorFile = args[0]
numeratorFile = args[1]
outFile = opts.outfile
denominators = {}
numerators = {}
ratios = {}
with open(denominatorFile) as df:
df.next() # Get rid of header
for line in df:
line = line.rstrip("\r\n")
rank, name, count = line.split(' ')
denominators[name] = int(count)
with open(numeratorFile) as nf:
nf.next() # Get rid of header
for line in nf:
line = line.rstrip("\r\n")
rank, name, count = line.split(' ')
numerators[name] = float(count)
for name, count in denominators.items():
if name in numerators:
ratios[name] = numerators[name] / count
rankings = counterToRank(ratios)
if outFile == "-":
rankToFile(sys.stdout, rankings)
else:
with open(outFile) as out:
rankToFile(out, rankings)
if __name__ == '__main__':
main()