Class: RSpec::Covers::Tracer::DynamicCorpus

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/covers/tracer/dynamic_corpus.rb

Instance Method Summary collapse

Constructor Details

#initializeDynamicCorpus

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