Class: ValkeyObjects::KNN

Inherits:
Object
  • Object
show all
Defined in:
lib/valkey/objects.rb

Overview

generic KNN processor

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ KNN

Initialize with a collection of strings



79
80
81
# File 'lib/valkey/objects.rb', line 79

def initialize(collection)
  @collection = collection.flatten.compact
end

Instance Method Details

#[](q) ⇒ Object



106
107
108
# File 'lib/valkey/objects.rb', line 106

def [] q
  rank(q)[0][:string]
end

#hood(query) ⇒ Object

Find k nearest neighbors to the query string Returns array of hashes with :string and :distance keys



85
86
87
88
89
90
91
92
93
# File 'lib/valkey/objects.rb', line 85

def hood(query)
  # Calculate distances for all strings
  distances = @collection.map do |str|
    { string: str, distance: levenshtein_distance(query, str) }
  end
  
  # Sort by distance (ascending) and take top k
  distances.sort_by { |item| item[:distance] }.take([10, @collection.size].min)
end

#rank(query) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/valkey/objects.rb', line 95

def rank query
  neighbors = hood(query)
  
  neighbors.map do |item|
    max_len = [query.length, item[:string].length].max
    similarity = max_len.zero? ? 1.0 : 1.0 - (item[:distance].to_f / max_len)
    dist = levenshtein_distance(item[:string], query)
    { string: item[:string], similarity: similarity.round(4), distance: dist.round(4) }
  end.sort_by { |h| h[:distance] && -h[:similarity] }
end