Skip to content

Instantly share code, notes, and snippets.

@nandajavarma
Created January 28, 2014 16:06
Show Gist options
  • Save nandajavarma/8670548 to your computer and use it in GitHub Desktop.
Save nandajavarma/8670548 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
pages = Array.new
queries = Array.new
puts "Enter the input. Hit ctrl + D when done!"
ARGF.each do |line|
if line[0].downcase == 'p'
pages << line.downcase
elsif line[0].downcase == 'q'
queries << line.downcase
else next
end
end
for query in queries
search_terms = query.split
search_terms.shift
result_array = Array.new
print "Q" + (queries.index(query) + 1).to_s + " : "
for page in pages
weight = 0
keywords = page.split
keywords.shift
list = search_terms & keywords
for a_key in list
weight = weight + ((8 - keywords.index(a_key)) * (8 - search_terms.index(a_key)))
end
result_array << weight
hash = Hash.new
i = 1
result_array.each do |value|
hash["P" + i.to_s] = value
i += 1
end
end
result_hash = Hash[hash.sort_by{|k,v| [-v, k]}]
i = 0
result_hash.each do |key, value|
if value != 0 and i < 5
print key + " "
i += 1
end
end
print "\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment