Class: Simhilarity::Candidate
- Inherits:
-
Object
- Object
- Simhilarity::Candidate
- Defined in:
- lib/simhilarity/candidate.rb
Overview
A potential match between two Elements
. It can calculate it’s own score.
Instance Attribute Summary collapse
-
#a ⇒ Object
readonly
first half of the candidate pair - the needle.
-
#b ⇒ Object
readonly
first half of the candidate pair - the haystack.
-
#matcher ⇒ Object
readonly
matcher that owns this guy.
Instance Method Summary collapse
-
#initialize(matcher, a, b) ⇒ Candidate
constructor
:nodoc:.
-
#score ⇒ Object
Calculate the score for this
Candidate
. -
#to_s ⇒ Object
:nodoc:.
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
#a ⇒ Object (readonly)
first half of the candidate pair - the needle.
8 9 10 |
# File 'lib/simhilarity/candidate.rb', line 8 def a @a end |
#b ⇒ Object (readonly)
first half of the candidate pair - the haystack.
11 12 13 |
# File 'lib/simhilarity/candidate.rb', line 11 def b @b end |
#matcher ⇒ Object (readonly)
matcher that owns this guy
5 6 7 |
# File 'lib/simhilarity/candidate.rb', line 5 def matcher @matcher end |
Instance Method Details
#score ⇒ Object
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_s ⇒ Object
:nodoc:
42 43 44 |
# File 'lib/simhilarity/candidate.rb', line 42 def to_s #:nodoc: "Candidate #{score}: #{a.inspect}..#{b.inspect}" end |