Class: Replacement::Score

Inherits:
Object show all
Defined in:
lib/replacement/score.rb

Class Method Summary collapse

Class Method Details

.best_score(pattern, to_update, left_context, right_context, pattern_index) ⇒ Object



3
4
5
6
# File 'lib/replacement/score.rb', line 3

def self.best_score(pattern, to_update, left_context, right_context, pattern_index)
  [right_score(pattern, to_update, right_context, pattern_index),
    left_score(to_update, left_context, pattern_index)].max
end

.left_score(to_update, left_context, pattern_index) ⇒ Object



14
15
16
# File 'lib/replacement/score.rb', line 14

def self.left_score(to_update, left_context, pattern_index)
  string_dot_product(to_update[0...pattern_index].reverse, left_context.reverse).count
end

.right_score(pattern, to_update, right_context, pattern_index) ⇒ Object



8
9
10
11
12
# File 'lib/replacement/score.rb', line 8

def self.right_score(pattern, to_update, right_context, pattern_index)
  pattern_length = pattern.length
  start_index = pattern_index + pattern_length
  string_dot_product(to_update[start_index..-1], right_context).count
end

.string_dot_product(first, second) ⇒ Object



19
20
21
22
23
# File 'lib/replacement/score.rb', line 19

def self.string_dot_product(first, second)
  first.split("").zip(second.split("")).take_while do |(a,b)|
    a == b
  end
end