Ruby:在倒排索引中搜索部分匹配

时间:2012-06-08 15:37:24

标签: ruby full-text-search

我需要在反向索引中搜索部分匹配,以下代码适用于完全匹配但不适用于部分匹配。从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"]]

1 个答案:

答案 0 :(得分:3)

如下定义search

def search words
  words.map do |word|
    matches = @data.keys.select {|key| key.include?(word)}
    matches.map {|match| @data[match] }
  end      
end

p search ['of'] #=> [[["1.txt", "2.txt"]]]
p search ['one'] #=> [[["1.txt"]]]
p search ['on']  #=> [[["1.txt", "2.txt"], ["1.txt"]]] - note that "contents" contains "on"