From 4e3f0fb3910fea0ef1580b7bf8e232e99ac62063 Mon Sep 17 00:00:00 2001 From: Jeff Yates Date: Sat, 24 Oct 2020 13:56:09 -0400 Subject: [PATCH] added csv output --- sorter.rb | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/sorter.rb b/sorter.rb index 5d62673..51c627a 100755 --- a/sorter.rb +++ b/sorter.rb @@ -45,13 +45,27 @@ end #write_output expects: # output - a hash containing all of our output -#This method converts the output hash to JSON and writes it to output.json -def write_output (output, filename) +#This method converts the output hash to JSON and writes it to "output.json" +def write_output_json (output, filename) outfile = File.open(filename,'w') outfile.write(output.to_json) outfile.close end +def write_output_csv (output, filename) + CSV.open(filename, 'wb') do |csv| + csv << ["bin", "words", "total"] + output.each_key do |key| + line = [] + line.push(key) + output[key].each_key do |sub_key| + line.push(output[key][sub_key]) + end + csv << line + end + end +end + #strip_text expects: # text - the text we're working on # start - the starting string to search for @@ -119,8 +133,9 @@ text = strip_text(text, 'PLOVEINTAKE', 'PLOVECLOSING') if options[:type] == 'iat text = split_text(text, 'Narrative:', 'Signatures:') if options[:type] == 'pn' output = Hash.new #Creating the output storage object bins = Hash.new #This hash stores the bins -outfile = options[:file] + '-out.json' +outfile = options[:file] outfile.slice!('.txt') +puts outfile csv.each { |bin| bins[bin[0]] = bin[1..].compact } #turn the csv array into a hash, remove nils @@ -130,5 +145,6 @@ bins.each_key do |bin_number| output[key][:words] = bin_counter(bins[bin_number], text) output[key][:total] = count_total(output[key]) end -write_output(output,outfile) +write_output_json(output,outfile + '-out.json') +write_output_csv(output,outfile + '-out.csv')