Class: LooseTightDictionary::Similarity

Inherits:
Object
  • Object
show all
Defined in:
lib/loose_tight_dictionary/similarity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(wrapper1, wrapper2) ⇒ Similarity

Returns a new instance of Similarity.



6
7
8
9
# File 'lib/loose_tight_dictionary/similarity.rb', line 6

def initialize(wrapper1, wrapper2)
  @wrapper1 = wrapper1
  @wrapper2 = wrapper2
end

Instance Attribute Details

#wrapper1Object (readonly)

Returns the value of attribute wrapper1.



3
4
5
# File 'lib/loose_tight_dictionary/similarity.rb', line 3

def wrapper1
  @wrapper1
end

#wrapper2Object (readonly)

Returns the value of attribute wrapper2.



4
5
6
# File 'lib/loose_tight_dictionary/similarity.rb', line 4

def wrapper2
  @wrapper2
end

Instance Method Details

#<=>(other) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/loose_tight_dictionary/similarity.rb', line 11

def <=>(other)
  if best_score != other.best_score
    best_score <=> other.best_score
  else
    weight <=> other.weight
  end
end

#best_scoreObject



24
25
26
# File 'lib/loose_tight_dictionary/similarity.rb', line 24

def best_score
  @best_score ||= Score.new best_wrapper1_variant, best_wrapper2_variant
end

#best_variantsObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/loose_tight_dictionary/similarity.rb', line 36

def best_variants
  @best_variants ||= wrapper1.variants.product(wrapper2.variants).sort do |tuple1, tuple2|
    wrapper1_variant1, wrapper2_variant1 = tuple1
    wrapper1_variant2, wrapper2_variant2 = tuple2

    score1 = Score.new wrapper1_variant1, wrapper2_variant1
    score2 = Score.new wrapper1_variant2, wrapper2_variant2
    
    score1 <=> score2
  end[-1]
end

#best_wrapper1_variantObject



28
29
30
# File 'lib/loose_tight_dictionary/similarity.rb', line 28

def best_wrapper1_variant
  best_variants[0]
end

#best_wrapper2_variantObject



32
33
34
# File 'lib/loose_tight_dictionary/similarity.rb', line 32

def best_wrapper2_variant
  best_variants[1]
end

#inspectObject



48
49
50
# File 'lib/loose_tight_dictionary/similarity.rb', line 48

def inspect
  %{#<Similarity "#{wrapper2.to_str}"=>"#{best_wrapper2_variant}" versus "#{wrapper1.to_str}"=>"#{best_wrapper1_variant}" weight=#{"%0.5f" % weight} best_score=#{"%0.5f" % best_score.to_f}>}
end

#weightObject

Weight things towards short original strings



20
21
22
# File 'lib/loose_tight_dictionary/similarity.rb', line 20

def weight
  @weight ||= (1.0 / (wrapper1.to_str.length * wrapper2.to_str.length))
end