added csv output

batch
Jeff Yates 2020-10-24 13:56:09 -04:00
parent a09a3a3be1
commit 4e3f0fb391
1 changed files with 20 additions and 4 deletions

View File

@ -45,13 +45,27 @@ end
#write_output expects: #write_output expects:
# output - a hash containing all of our output # output - a hash containing all of our output
#This method converts the output hash to JSON and writes it to output.json #This method converts the output hash to JSON and writes it to "output.json"
def write_output (output, filename) def write_output_json (output, filename)
outfile = File.open(filename,'w') outfile = File.open(filename,'w')
outfile.write(output.to_json) outfile.write(output.to_json)
outfile.close outfile.close
end 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: #strip_text expects:
# text - the text we're working on # text - the text we're working on
# start - the starting string to search for # 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' text = split_text(text, 'Narrative:', 'Signatures:') if options[:type] == 'pn'
output = Hash.new #Creating the output storage object output = Hash.new #Creating the output storage object
bins = Hash.new #This hash stores the bins bins = Hash.new #This hash stores the bins
outfile = options[:file] + '-out.json' outfile = options[:file]
outfile.slice!('.txt') outfile.slice!('.txt')
puts outfile
csv.each { |bin| bins[bin[0]] = bin[1..].compact } #turn the csv array into a hash, remove nils 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][:words] = bin_counter(bins[bin_number], text)
output[key][:total] = count_total(output[key]) output[key][:total] = count_total(output[key])
end end
write_output(output,outfile) write_output_json(output,outfile + '-out.json')
write_output_csv(output,outfile + '-out.csv')