Ruby: search for a partial match in an inverted index -


i need search partial match in inverted index, following code works exact matches not partial. reworked example @ http://rosettacode.org/wiki/inverted_index (which no longer works in ruby1.9.3)

how efficient way please ? please no advise using lucene, sphinx etc unless know lightweight, simple , pure ruby solution, want 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 |word|     result << @data[word] if @data[word] #should 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 search follows:

def search words   words.map |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 "contents" contains "on"  

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -