Class: RSpec::Covers::Tracer::DynamicCorpus
- Inherits:
-
Object
- Object
- RSpec::Covers::Tracer::DynamicCorpus
- Defined in:
- lib/rspec/covers/tracer/dynamic_corpus.rb
Instance Method Summary collapse
-
#initialize ⇒ DynamicCorpus
constructor
A new instance of DynamicCorpus.
- #normalize(example_id, labels) ⇒ Object
- #record(example_id, labels) ⇒ Object
- #score(example_id, label) ⇒ Object
Constructor Details
#initialize ⇒ DynamicCorpus
Returns a new instance of DynamicCorpus.
7 8 9 |
# File 'lib/rspec/covers/tracer/dynamic_corpus.rb', line 7 def initialize @documents = {} end |
Instance Method Details
#normalize(example_id, labels) ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/rspec/covers/tracer/dynamic_corpus.rb', line 24 def normalize(example_id, labels) raw_scores = labels.to_h { |label| [label, score(example_id, label)] } max_score = raw_scores.values.max.to_f return raw_scores.transform_values { 0.0 } unless max_score.positive? raw_scores.transform_values { |value| value / max_score } end |
#record(example_id, labels) ⇒ Object
11 12 13 |
# File 'lib/rspec/covers/tracer/dynamic_corpus.rb', line 11 def record(example_id, labels) @documents[example_id] = labels.tally end |
#score(example_id, label) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/rspec/covers/tracer/dynamic_corpus.rb', line 15 def score(example_id, label) counts = @documents.fetch(example_id, {}) max_count = counts.values.max.to_f return 0.0 unless max_count.positive? term_frequency = counts.fetch(label, 0) / max_count term_frequency * inverse_document_frequency(label) end |