Class: ErrorToCommunicate::Levenshtein
- Inherits:
-
Object
- Object
- ErrorToCommunicate::Levenshtein
- Defined in:
- lib/error_to_communicate/levenshtein.rb
Instance Attribute Summary collapse
-
#actual ⇒ Object
Returns the value of attribute actual.
-
#memoized ⇒ Object
Returns the value of attribute memoized.
-
#target ⇒ Object
Returns the value of attribute target.
Class Method Summary collapse
Instance Method Summary collapse
- #call(target_length, actual_length) ⇒ Object
- #distance ⇒ Object
-
#initialize(target, actual) ⇒ Levenshtein
constructor
A new instance of Levenshtein.
Constructor Details
#initialize(target, actual) ⇒ Levenshtein
Returns a new instance of Levenshtein.
8 9 10 11 12 |
# File 'lib/error_to_communicate/levenshtein.rb', line 8 def initialize(target, actual) self.target = target self.actual = actual self.memoized = {} end |
Instance Attribute Details
#actual ⇒ Object
Returns the value of attribute actual.
7 8 9 |
# File 'lib/error_to_communicate/levenshtein.rb', line 7 def actual @actual end |
#memoized ⇒ Object
Returns the value of attribute memoized.
7 8 9 |
# File 'lib/error_to_communicate/levenshtein.rb', line 7 def memoized @memoized end |
#target ⇒ Object
Returns the value of attribute target.
7 8 9 |
# File 'lib/error_to_communicate/levenshtein.rb', line 7 def target @target end |
Class Method Details
.call(target, actual) ⇒ Object
3 4 5 |
# File 'lib/error_to_communicate/levenshtein.rb', line 3 def self.call(target, actual) new(target, actual).distance end |
Instance Method Details
#call(target_length, actual_length) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/error_to_communicate/levenshtein.rb', line 18 def call(target_length, actual_length) memoized[[target_length, actual_length]] ||= if target_length.zero? actual_length elsif actual_length.zero? target_length elsif target[target_length - 1] == actual[actual_length - 1] call(target_length - 1, actual_length - 1) else [ call(target_length , actual_length - 1), call(target_length - 1, actual_length ), call(target_length - 1, actual_length - 1), ].min + 1 end end |
#distance ⇒ Object
14 15 16 |
# File 'lib/error_to_communicate/levenshtein.rb', line 14 def distance @distance ||= call target.length, actual.length end |