added code to output master.csv

batch
Jeff Yates 2020-10-26 15:44:16 -04:00
parent a60395dd5b
commit 4e1b862586
1 changed files with 40 additions and 2 deletions

View File

@ -54,6 +54,7 @@ end
def write_output_csv (output, filename) def write_output_csv (output, filename)
CSV.open(filename, 'wb') do |csv| CSV.open(filename, 'wb') do |csv|
csv << ["bin", "words", "total"] csv << ["bin", "words", "total"]
output.delete(:filename)
output.each_key do |key| output.each_key do |key|
line = [] line = []
line.push(key) line.push(key)
@ -127,6 +128,7 @@ def process_file (file_name, binfile, type)
outfile = file_name outfile = file_name
outfile.slice!('.txt') outfile.slice!('.txt')
#puts outfile #puts outfile
output[:filename] = 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
bins.each_key do |bin_number| bins.each_key do |bin_number|
key = bin_number.to_sym key = bin_number.to_sym
@ -146,11 +148,46 @@ end
#This method will process all .txt files in the supplied directory #This method will process all .txt files in the supplied directory
def process_dir(dir_name, binfile, type) def process_dir(dir_name, binfile, type)
Dir.glob(dir_name + '*.txt') do |file_name| Dir.glob(dir_name + '*.txt') do |file_name|
puts file_name puts "Processing" + file_name
process_file(file_name, binfile, type) process_file(file_name, binfile, type)
end end
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 options = Hash.new
OptionParser.new do |opts| OptionParser.new do |opts|
opts.banner = 'sorter.rb --options' opts.banner = 'sorter.rb --options'
@ -174,5 +211,6 @@ elsif options[:file]
process_file(options[:file], options[:binfile], options[:type]) process_file(options[:file], options[:binfile], options[:type])
elsif options[:dir] elsif options[:dir]
process_dir(options[:dir], options[:binfile], options[:type]) process_dir(options[:dir], options[:binfile], options[:type])
generate_master_output(options[:dir], options[:binfile])
end end