Class: CommaSplice::OptionScorer

Inherits:
Object
  • Object
show all
Defined in:
lib/comma_splice/helpers/option_scorer.rb

Overview

scores options based on how likely they are to be correct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(option, separator: ',') ⇒ OptionScorer

Returns a new instance of OptionScorer.



6
7
8
9
10
# File 'lib/comma_splice/helpers/option_scorer.rb', line 6

def initialize(option, separator: ',')
  @option = option
  @start_score = 100
  @separator = separator
end

Instance Attribute Details

#optionObject (readonly)

Returns the value of attribute option.



4
5
6
# File 'lib/comma_splice/helpers/option_scorer.rb', line 4

def option
  @option
end

Instance Method Details

#breakdownObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/comma_splice/helpers/option_scorer.rb', line 12

def breakdown
  score     = @start_score
  breakdown = []

  rules.each do |rule|
    rule_score = send(rule.to_sym)
    score += rule_score
    breakdown << "#{rule_score.to_s.ljust(3)} #{rule.to_sym}" if rule_score != 0
  end

  breakdown.unshift("score: #{score}")
end

#options_that_are_blankObject



63
64
65
66
67
# File 'lib/comma_splice/helpers/option_scorer.rb', line 63

def options_that_are_blank
  option.select do |o|
    o.to_s.strip.blank?
  end.size * -5
end

#options_that_end_with_a_separatorObject



51
52
53
54
55
# File 'lib/comma_splice/helpers/option_scorer.rb', line 51

def options_that_end_with_a_separator
  option.select do |o|
    o.to_s.ends_with?(@separator)
  end.size * -5
end

#options_that_have_longest_comma_separated_numberObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/comma_splice/helpers/option_scorer.rb', line 69

def options_that_have_longest_comma_separated_number
  # return 0 unless @separator == ','

  # favor items that have a longer comma separated number
  # i.e in the following example, option 1 should win
  # (1)    artist    : Half Japanese
  #        title     : 1,000,000,000 Kisses
  #        albumtitle: Beautiful Songs: The Best of Jad Fair & Half Japanese
  #        label     : Stillwater/Fire
  #
  #
  # (2)    artist    : Half Japanese,1,000,000
  #        title     : 000 Kisses
  #        albumtitle: Beautiful Songs: The Best of Jad Fair & Half Japanese
  #        label     : Stillwater/Fire
  #
  #
  # (3)    artist    : Half Japanese,1
  #        title     : 000,000,000 Kisses
  #        albumtitle: Beautiful Songs: The Best of Jad Fair & Half Japanese
  #        label     : Stillwater/Fire
  #
  #
  # (4)    artist    : Half Japanese,1,000
  #        title     : 000,000 Kisses
  #        albumtitle: Beautiful Songs: The Best of Jad Fair & Half Japanese
  #        label     : Stillwater/Fire

  option.collect do |o|
    result = o.to_s.scan(/\d{1,3}(?:,\d{1,3})*(?:\.\d+)?/)
    if result.size.positive? && result.first.index(',')
      result.join(',').size
    else
      0
    end
  end.max.to_i
end

#options_that_have_words_joined_by_separatorsObject



57
58
59
60
61
# File 'lib/comma_splice/helpers/option_scorer.rb', line 57

def options_that_have_words_joined_by_separators
  option.select do |o|
    Regexp.new("[^0-9\\s]#{@separator}\\w").match(o.to_s) || Regexp.new("\\w#{@separator}[^0-9\\s]").match(o.to_s)
  end.compact.size * -5
end

#options_that_start_with_a_quote_followed_by_a_spaceObject



39
40
41
42
43
# File 'lib/comma_splice/helpers/option_scorer.rb', line 39

def options_that_start_with_a_quote_followed_by_a_space
  option.select do |o|
    o.to_s.starts_with?('" ')
  end.size * -1
end

#options_that_start_with_a_separatorObject



45
46
47
48
49
# File 'lib/comma_splice/helpers/option_scorer.rb', line 45

def options_that_start_with_a_separator
  option.select do |o|
    o.to_s.starts_with?(@separator)
  end.size * -5
end

#options_that_start_with_a_spaceObject



33
34
35
36
37
# File 'lib/comma_splice/helpers/option_scorer.rb', line 33

def options_that_start_with_a_space
  option.select do |o|
    o.to_s.starts_with?(' ')
  end.size * -10
end

#scoreObject



25
26
27
28
29
30
31
# File 'lib/comma_splice/helpers/option_scorer.rb', line 25

def score
  score = @start_score
  rules.each do |rule|
    score += send(rule.to_sym)
  end
  score
end