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
18
# File 'lib/loose_tight_dictionary/similarity.rb', line 11

def <=>(other)
  by_score = best_score <=> other.best_score
  if by_score == 0
    original_weight <=> other.original_weight
  else
    by_score
  end
end

#best_scoreObject



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

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

#best_variantsObject



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

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



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

def best_wrapper1_variant
  best_variants[0]
end

#best_wrapper2_variantObject



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

def best_wrapper2_variant
  best_variants[1]
end

#inspectObject



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

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

#original_weightObject

Weight things towards short original strings



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

def original_weight
  @original_weight ||= (1.0 / (wrapper1.render.length * wrapper2.render.length))
end