Class: Linkage::ScoreSet Abstract
- Inherits:
-
Object
- Object
- Linkage::ScoreSet
- Defined in:
- lib/linkage/score_set.rb
Overview
A ScoreSet is responsible for keeping track of scores. During the record linkage process, one or more Comparators generate scores. These scores are handled by a ScoreRecorder, which uses a ScoreSet to actually save the scores. ScoreSet is also used to fetch the linkage scores so that a Matcher can create matches.
ScoreSet is the superclass of implementations for different formats. Currently there are two formats for storing scores:
- CSV (Linkage::ScoreSets::CSV)
- Database (Linkage::ScoreSets::Database)
See the documentation for score set you're interested in for more information.
If you want to implement a custom ScoreSet, create a class that inherits ScoreSet and defines at least #add_score and #each_pair. You can then register that class via ScoreSet.register.
Direct Known Subclasses
Class Method Summary collapse
-
.klass_for(name) ⇒ Class?
(also: [])
Return a registered ScoreSet subclass or
nil
if it doesn't exist. -
.register(name, klass) ⇒ Object
Register a new score set.
Instance Method Summary collapse
-
#add_score(comparator_id, id_1, id_2, value) ⇒ Object
abstract
Add a score to the ScoreSet.
-
#close ⇒ Object
This is called by Linkage::ScoreRecorder#stop, after all scores have been added.
-
#each_pair(&block) ⇒ Object
abstract
Yield scores for each pair of records.
-
#open_for_reading ⇒ Object
This is called by Matcher#run, before any scores are read via #each_pair.
-
#open_for_writing ⇒ Object
This is called by Linkage::ScoreRecorder#start, before any scores are added via #add_score.
Class Method Details
.klass_for(name) ⇒ Class? Also known as: []
Return a registered ScoreSet subclass or nil
if it doesn't exist.
51 52 53 |
# File 'lib/linkage/score_set.rb', line 51 def klass_for(name) @score_sets ? @score_sets[name] : nil end |
.register(name, klass) ⇒ Object
Register a new score set. Subclasses must define at least #add_score
and #each_pair. Otherwise, an ArgumentError
will be raised when you
try to call register.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/linkage/score_set.rb', line 30 def register(name, klass) methods = klass.instance_methods(false) missing = [] unless methods.include?(:add_score) missing.push("#add_score") end unless methods.include?(:each_pair) missing.push("#each_pair") end unless missing.empty? raise ArgumentError, "class must define #{missing.join(" and ")}" end @score_sets ||= {} @score_sets[name] = klass end |
Instance Method Details
#add_score(comparator_id, id_1, id_2, value) ⇒ Object
Add a score to the ScoreSet. Subclasses must redefine this.
76 77 78 |
# File 'lib/linkage/score_set.rb', line 76 def add_score(comparator_id, id_1, id_2, value) raise NotImplementedError end |
#close ⇒ Object
This is called by Linkage::ScoreRecorder#stop, after all scores have been added. Subclasses can redefine this to perform any teardown needed.
101 102 |
# File 'lib/linkage/score_set.rb', line 101 def close end |
#each_pair(&block) ⇒ Object
Yield scores for each pair of records. Subclasses must redefine this. This method is called by Matcher#run with a block with three parameters:
score_set.each_pair do |id_1, id_2, scores|
end
scores
should be a Hash where comparator ids are keys and scores are
values. For example: { 1 => 0.5, 2 => 0.75, 3 => 1 }
. Note that not all
comparators (including Comparators::Compare) create scores for each
pair. A missing score means that pair was given a score of 0.
95 96 97 |
# File 'lib/linkage/score_set.rb', line 95 def each_pair(&block) raise NotImplementedError end |
#open_for_reading ⇒ Object
This is called by Matcher#run, before any scores are read via #each_pair. Subclasses can redefine this to perform any setup needed for reading scores.
60 61 |
# File 'lib/linkage/score_set.rb', line 60 def open_for_reading end |
#open_for_writing ⇒ Object
This is called by Linkage::ScoreRecorder#start, before any scores are added via #add_score. Subclasses can redefine this to perform any setup needed for saving scores.
66 67 |
# File 'lib/linkage/score_set.rb', line 66 def open_for_writing end |