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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D8598ce8f08e821c8496b8e81645319a?d=identicon&s=25 kashia.buch (Guest)
on 2005-11-15 12:23
(Received via mailing list)
Hi,

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
:D

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

have a nice day,
Kash

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

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

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

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

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