Class: Simhilarity::Candidate

Inherits:
Object
  • Object
show all
Defined in:
lib/simhilarity/candidate.rb

Overview

A potential match between two Elements. It can calculate it’s own score.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matcher, a, b) ⇒ Candidate

:nodoc:



13
14
15
16
17
# File 'lib/simhilarity/candidate.rb', line 13

def initialize(matcher, a, b) #:nodoc:
  @matcher = matcher
  @a = a
  @b = b
end

Instance Attribute Details

#aObject (readonly)

first half of the candidate pair - the needle.



8
9
10
# File 'lib/simhilarity/candidate.rb', line 8

def a
  @a
end

#bObject (readonly)

first half of the candidate pair - the haystack.



11
12
13
# File 'lib/simhilarity/candidate.rb', line 11

def b
  @b
end

#matcherObject (readonly)

matcher that owns this guy



5
6
7
# File 'lib/simhilarity/candidate.rb', line 5

def matcher
  @matcher
end

Instance Method Details

#scoreObject

Calculate the score for this Candidate. The score is the dice coefficient, (2*c)/(a+b).

  • a: the frequency weighted sum of the ngrams in a

  • b: the frequency weighted sum of the ngrams in b

  • c: the frequency weighted sum of the ngrams in (a & b)

Lazily calculated and memoized.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/simhilarity/candidate.rb', line 28

def score
  @score ||= begin
    c = (self.a.ngrams & self.b.ngrams)
    if c.length > 0
      a = self.a.ngrams_sum
      b = self.b.ngrams_sum
      c = matcher.ngrams_sum(c)
      (2.0 * c) / (a + b)
    else
      0
    end
  end
end

#to_sObject

:nodoc:



42
43
44
# File 'lib/simhilarity/candidate.rb', line 42

def to_s #:nodoc:
  "Candidate #{score}: #{a.inspect}..#{b.inspect}"
end