diff --git a/sorter.rb b/sorter.rb index 5f629bf..4cf8991 100755 --- a/sorter.rb +++ b/sorter.rb @@ -54,12 +54,13 @@ end def write_output_csv (output, filename) CSV.open(filename, 'wb') do |csv| csv << ["bin", "words", "total"] + output.delete(:filename) output.each_key do |key| line = [] line.push(key) output[key].each_key do |sub_key| line.push(output[key][sub_key]) - end + end csv << line end end @@ -127,6 +128,7 @@ def process_file (file_name, binfile, type) outfile = file_name outfile.slice!('.txt') #puts outfile + output[:filename] = outfile csv.each { |bin| bins[bin[0]] = bin[1..].compact } #turn the csv array into a hash, remove nils bins.each_key do |bin_number| key = bin_number.to_sym @@ -146,11 +148,46 @@ end #This method will process all .txt files in the supplied directory def process_dir(dir_name, binfile, type) Dir.glob(dir_name + '*.txt') do |file_name| - puts file_name + puts "Processing" + file_name process_file(file_name, binfile, type) end end +def generate_master_output(dir_name, binfile) + puts dir_name + file=File.open(binfile,"r") + bin_count = file.readlines.size + file.close + bin_header = [] + bin_count.times do |num| + num += 1 + words_head = "Bin " + num.to_s + " words" + total_head = "Bin " + num.to_s + " total" + bin_header.push(words_head) + bin_header.push(total_head) + end + CSV.open('master.csv', 'wb') do |csv| + header = ["File", "Total Words" ] + bin_header + csv << header + Dir.glob(dir_name + '*.json') do |file_name| + puts file_name + csv_row = [] + json_file = File.read(file_name) + data_hash = JSON.parse(json_file) + csv_row.push(data_hash["filename"]) + data_hash.delete("filename") + word_total = 0 + data_hash.each_key do |key| + csv_row.push(data_hash[key]["words"]) + csv_row.push(data_hash[key]["total"]) + word_total += data_hash[key]["total"] + end + csv_row = csv_row.insert(1, word_total) + csv << csv_row + end + end +end + options = Hash.new OptionParser.new do |opts| opts.banner = 'sorter.rb --options' @@ -174,5 +211,6 @@ elsif options[:file] process_file(options[:file], options[:binfile], options[:type]) elsif options[:dir] process_dir(options[:dir], options[:binfile], options[:type]) + generate_master_output(options[:dir], options[:binfile]) end