Class: Llmemory::Retrieval::Bm25Scorer

Inherits:
Object
  • Object
show all
Defined in:
lib/llmemory/retrieval/bm25_scorer.rb

Constant Summary collapse

K1 =
1.5
B =
0.75

Instance Method Summary collapse

Constructor Details

#initialize(k1: K1, b: B) ⇒ Bm25Scorer

Returns a new instance of Bm25Scorer.



9
10
11
12
# File 'lib/llmemory/retrieval/bm25_scorer.rb', line 9

def initialize(k1: K1, b: B)
  @k1 = k1
  @b = b
end

Instance Method Details

#score_candidates(query, candidates) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/llmemory/retrieval/bm25_scorer.rb', line 14

def score_candidates(query, candidates)
  return [] if candidates.empty?

  query_tokens = tokenize(query)
  return candidates.map { |c| c.merge(bm25_score: 0.0, normalized_bm25: 0.0) } if query_tokens.empty?

  doc_tokens_list = candidates.map { |c| tokenize((c[:text] || c["text"]).to_s) }
  avg_doc_len = doc_tokens_list.map(&:size).sum.to_f / [doc_tokens_list.size, 1].max
  n_docs = candidates.size

  doc_freq = Hash.new(0)
  doc_tokens_list.each do |tokens|
    tokens.uniq.each { |t| doc_freq[t] += 1 }
  end

  candidates.each_with_index.map do |c, i|
    doc_tokens = doc_tokens_list[i]
    doc_len = doc_tokens.size
    bm25 = 0.0

    query_tokens.uniq.each do |term|
      tf = doc_tokens.count(term)
      next if tf.zero?

      n_qi = doc_freq[term]
      idf = Math.log((n_docs - n_qi + 0.5) / (n_qi + 0.5) + 1.0)
      numerator = tf * (@k1 + 1)
      denom = tf + @k1 * (1 - @b + @b * doc_len.to_f / [avg_doc_len, 1].max)
      bm25 += idf * numerator / denom
    end

    c.merge(bm25_score: bm25)
  end.tap do |scored|
    max_bm25 = scored.map { |s| s[:bm25_score] }.max.to_f
    max_bm25 = 1.0 if max_bm25.zero?
    scored.each { |s| s[:normalized_bm25] = s[:bm25_score] / max_bm25 }
  end
end