Forum: Ruby Re: Index and Query (#54)

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
kashia.buch (Guest)
on 2005-11-15 13:23
(Received via mailing list)

looking at all those long subscriptions.. couldn't resist to make my
own. probably not as complete/fast as those, but small and neat looking

should be pretty much self explenators. it's using bignums for the index

have a nice day,

docs = {
  :doc1 => "The quick brown fox",
  :doc2 => "Jumped over the brown dog",
  :doc3 => "Cut him to the quick"

# building word list
lst = {|k,v| v.split(/[^\w']+/) }.flatten.uniq

# building index
index = docs.inject( do |hash, (k, v)|
  lst.each { |x| hash[k] |= 2**lst.index(x) if v.index(x) }

# query the string
q = ARGV[0] || "Cut"

res = { |k,v| k if (v & 2**lst.index(q)) > 0 }.compact
p res
This topic is locked and can not be replied to.