class CommaSplice::CommaCalculator
provide an array of CSV headers and and array of CSV values and this will figure out the best correction and prompt you if it can't find out
Public Class Methods
new(headers, values)
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 8 def initialize(headers, values) raise StandardError, "Determining all the possibilities to fit #{values.size} values into the #{headers.size} headers #{headers.inspect} is computationally expensive. Please specify the columns where commas might be." if headers.size > 10 && values.size > 10 @headers = headers @values = values @longest_header = @headers.max_by(&:length) end
Public Instance Methods
best_options()
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 52 def best_options max_score = ranked_options.collect { |o| o.score }.max ranked_options.select { |o| o.score == max_score } end
correction()
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 16 def correction if @headers.size == @values.size @values elsif best_options.size == 1 best_options.first.option elsif best_options.size > 1 prompt_for_options(best_options) else prompt_for_options(ranked_options) end end
needs_correcting?()
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 61 def needs_correcting? @headers.size < @values.size end
needs_manual_input?()
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 57 def needs_manual_input? !best_options.one? end
print_all_options()
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 65 def print_all_options ranked_options.each_with_index do |option, index| print_option(option, index) end end
ranked_options()
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 28 def ranked_options @all_options ||= join_possibilities.collect do |joins| values = @values.dup joins.collect do |join_num| val = values.shift(join_num) if val.empty? nil elsif val.size == 1 val.first else val.join(',') end end end @ranked_options ||= @all_options.collect do |option| OptionScorer.new(option) end end
score_option(option)
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 48 def score_option(option) OptionScorer.new(option).score end
Protected Instance Methods
join_possibilities()
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 73 def join_possibilities JoinPossibilities.new(@values.size, @headers.size).possibilities end
print_option(option, index = nil)
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 97 def print_option(option, index = nil) score_breakdown = option.breakdown lines = [] @headers.each_with_index do |header, i| marker = if i.zero? && index "(#{index + 1})" else '' end line = marker.ljust(7) + header.ljust(@longest_header.size) + ': ' + option.option[i].to_s.ljust(75) if CommaSplice.debug line = line + "| " + (score_breakdown.shift || "") end lines << line end lines << "\n" puts lines.join("\n") end
prompt_for_options(options)
click to toggle source
# File lib/comma_splice/helpers/comma_calculator.rb, line 77 def prompt_for_options(options) options.each_with_index do |option, index| print_option(option, index) end puts "press 0 to see all options" if ranked_options.size != options.size selected_option = nil until selected_option && selected_option.to_i > -1 puts 'which one is correct?' selected_option = STDIN.gets end if selected_option.to_i == 0 prompt_for_options(ranked_options.sort_by { |s| s.score.to_i }.reverse) else options[selected_option.to_i - 1].option end end