I need to search for a partial match in an inverted index, following code works for exact matches but not for partial. Reworked this from the example at http://rosettacode.org/wiki/Inverted_Index (which no longer works in Ruby1.9.3)
How to do that the most efficient way please ?
Please no advise about using Lucene, Sphinx etc unless you know a lightweight, simple and pure Ruby solution, want to do it myself.
@data = {"contents"=>["1.txt", "2.txt"], "of"=>["1.txt", "2.txt"], "file"=>["1.txt", "2.txt"], "one"=>["1.txt"], "two"=>["2.txt"]}
def search words
result = []
words.each do |word|
result << @data[word] if @data[word] #should do a partial match
end
result
end
p search ['of'] #=> [["1.txt", "2.txt"]]
p search ['one'] #=> [["1.txt"]]
p search ['on'] #=> [] <<should become [["1.txt"]]
Define
searchas follows: