Class: LooseTightDictionary::Score

Inherits:
Object
  • Object
show all
Extended by:
ActiveSupport::Memoizable
Defined in:
lib/loose_tight_dictionary/score.rb

Constant Summary collapse

SPACE =
' '

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str1, str2) ⇒ Score

Returns a new instance of Score.



11
12
13
14
# File 'lib/loose_tight_dictionary/score.rb', line 11

def initialize(str1, str2)
  @str1 = str1.downcase
  @str2 = str2.downcase
end

Instance Attribute Details

#str1Object (readonly)

Returns the value of attribute str1.



9
10
11
# File 'lib/loose_tight_dictionary/score.rb', line 9

def str1
  @str1
end

#str2Object (readonly)

Returns the value of attribute str2.



9
10
11
# File 'lib/loose_tight_dictionary/score.rb', line 9

def str2
  @str2
end

Instance Method Details

#<=>(other) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/loose_tight_dictionary/score.rb', line 20

def <=>(other)
  by_dices_coefficient = (dices_coefficient <=> other.dices_coefficient)
  if by_dices_coefficient == 0
    levenshtein <=> other.levenshtein
  else
    by_dices_coefficient
  end
end

#dices_coefficientObject



47
48
49
# File 'lib/loose_tight_dictionary/score.rb', line 47

def dices_coefficient
  str1.pair_distance_similar str2
end

#inspectObject



16
17
18
# File 'lib/loose_tight_dictionary/score.rb', line 16

def inspect
  %{#<Score: dices_coefficient=#{dices_coefficient} levenshtein=#{levenshtein}>}
end

#levenshteinObject

extracted/adapted from the text gem version 1.0.2 normalization added for utf-8 strings lib/text/levenshtein.rb



80
81
82
# File 'lib/loose_tight_dictionary/score.rb', line 80

def levenshtein
  str1.levenshtein_similar str2
end

#utf8?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/loose_tight_dictionary/score.rb', line 29

def utf8?
  (defined?(::Encoding) ? str1.encoding.to_s : $KCODE).downcase.start_with?('u')
end