Module: LangExtract::Core::SimilarityUpperBound

Defined in:
lib/langextract/core/resolver.rb

Class Method Summary collapse

Class Method Details

.passes?(target, candidate, target_counts, threshold) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
# File 'lib/langextract/core/resolver.rb', line 11

def passes?(target, candidate, target_counts, threshold)
  denominator = target.length + candidate.length
  length_matches = [target.length, candidate.length].min
  return false if (2.0 * length_matches / denominator) < threshold

  candidate_counts = candidate.each_char.tally
  character_matches = target_counts.sum { |character, count| [count, candidate_counts[character].to_i].min }
  (2.0 * character_matches / denominator) >= threshold
end