-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge-results.rb
executable file
·45 lines (36 loc) · 1.25 KB
/
merge-results.rb
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
#!/usr/bin/env ruby
Encoding.default_external = Encoding::UTF_8
Encoding.default_internal = Encoding::UTF_8
require 'csv'
filtered_csv, titles_csv, output_titles_csv, output_ids_csv = ARGV
output_titles = File.open(output_titles_csv, 'w')
output_ids = File.open(output_ids_csv, 'w')
filtered_matches = {}
$stderr.puts "Parsing filtered CSV..."
CSV.foreach(filtered_csv, :headers => false) do |row|
filtered_matches[row[0]] ||= {}
filtered_matches[row[0]][row[1]] = row[2]
end
$stderr.puts "Parsing titles CSV..."
CSV.foreach(titles_csv, :headers => false) do |row|
filtered_matches[row[0]] ||= {}
filtered_matches[row[0]][row[1]] ||= 0
if filtered_matches[row[0]][row[1]] == 0
output_titles.write [row[0], row[1], row[2]].join(',') + "\n"
else
output_ids.write [row[0], row[1], filtered_matches[row[0]][row[1]].to_i + row[2].to_i].join(',') + "\n"
end
filtered_matches[row[0]].delete(row[1])
end
$stderr.puts "Outputting remaining ID matches..."
filtered_matches.each_key do |ht|
unless filtered_matches[ht].nil?
filtered_matches[ht].each_key do |ia|
unless filtered_matches[ht][ia].nil?
output_ids.write [ht, ia, filtered_matches[ht][ia]].join(',') + "\n"
end
end
end
end
output_titles.close
output_ids.close