我需要在倒排索引中搜索部分匹配,以下代码适用于完全匹配但不适用于部分匹配。从http://rosettacode.org/wiki/Inverted_Index的示例中重新设计了这个(在 Ruby1.9.3 中不再适用)
请问如何做到这一点最有效?请不要建议使用 Lucene、Sphinx 等,除非您知道一个轻量级、简单和纯 Ruby 解决方案,并且想自己做。
@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"]]