Class: Utcp::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/utcp/search.rb

Instance Method Summary collapse

Constructor Details

#initialize(repo) ⇒ Search

Returns a new instance of Search.



4
5
6
# File 'lib/utcp/search.rb', line 4

def initialize(repo)
  @repo = repo
end

Instance Method Details

#search(query, limit: 5) ⇒ Object

returns [ [score, tool_full_name], … ] sorted desc



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/utcp/search.rb', line 9

def search(query, limit: 5)
  q = query.to_s.downcase
  scores = []
  @repo.all_tools.each do |t|
    text = [t.name, t.description, (t.tags || []).join(" ")].join(" ").downcase
    score = 0
    q.split.each { |w| score += 3 if text.include?(w) }
    scores << [score, t]
  end
  scores.select { |s, _| s > 0 }.sort_by { |s, _| -s }[0, limit].map { |s, t| [s, t] }
end