Ruby Keyword Search Array

I am parsing a large CSV file in a ruby ​​script and you need to find the closest header match to some search keys. Search keys can be one or more values, and the values ​​may not match as below (should be close)

search_keys = ["big", "bear"]

A large array containing the data that I need to search, only to search in a column title:

array = [
          ["id", "title",            "code", "description"],
          ["1",  "once upon a time", "3241", "a classic story"],
          ["2",  "a big bad wolf",   "4235", "a little scary"],
          ["3",  "three big bears",  "2626", "a heart warmer"]
        ]

In this case, I would like it to return a string ["3", "three big bears", "2626", "a heart warmer"], as this is the closest match to my search keys.

I want it to return the closest match of the specified search keys.

Are there any helpers / libraries / gems that I can use? Has anyone done this before?

+5
source
5

, , - ! , ; .

result = []
array.each do |ar|
    rank = 0
    search_keys.each do |key|
        if ar[1].include?(key)
            rank += 1
        end
    end

    if rank > 0
        result << [rank, ar]
    end 
end

, , .

+1

, db , / .., . :)

array = [
          ["id", "title",            "code", "description"],
          ["1",  "once upon a time", "3241", "a classic story"],
          ["2",  "a big bad wolf",   "4235", "a little scary"],
          ["3",  "three big bears",  "2626", "a heart warmer"]
        ]


h = {}

search_keys = ["big", "bear"]

array[1..-1].each do |rec|
  rec_id = rec[0].to_i

  search_keys.each do |key|
    if rec[1].include? key
      h[rec_id] = h[rec_id] ? (h[rec_id]+1) : 1
    end
  end
end

closest = h.keys.first

h.each do |rec, count| 
  closest = rec if h[closest] < h[rec]
end

array[closest] # => desired output :)
+2

. * result.

* = , , , seach_keys. , "" ""

result = []
array.each do |a|
    a.each do |i|
        search_keys.each do |k|
            result << a if i.include?(k)
        end
    end
end
result.uniq!
+1

, ...

array = [
          ["id", "title",            "code", "description"],
          ["1",  "once upon a time", "3241", "a classic story"],
          ["2",  "a big bad wolf",   "4235", "a little scary"],
          ["3",  "three big bears",  "2626", "a heart warmer"]
        ]
search_keys = ["big", "bear"]


def sift(records, target_field, search_keys)
    # find target_field index
    target_field_index = nil
    records.first.each_with_index do |e, i|
        if e == target_field
            target_field_index = i
            break
        end
    end
    if target_field_index.nil?
        raise "Target field was not found"
    end

    # sums up which records have a match and how many keys they match
    # key => val = record => number of keys matched
    counter = Hash.new(0) # each new hash key is init'd with value of 0

    records.each do |record| # look at all our given records
        search_keys.each do |key| # check each search key on the field
            if record[target_field_index].include?(key)
                counter[record] += 1 # found a key, init to 0 if required and increment count
            end
        end
    end

    # find the result with the most search key matches
    top_result = counter.to_a.reduce do |top, record|
        if record[1] > top[1] # [0] = record, [1] = key hit count
            top = record # set to new top
        end
        top # continue with reduce
    end.first # only care about the record (not the key hit count)
end


puts "Top result: #{sift array, 'title', search_keys}"
# => Top result: ["3", "three big bears", "2626", "a heart warmer"]
+1

p array.find_all {|a|a.join.scan(/#{search_keys.join("|")}/).length==search_keys.length}
=>[["3", "three big bears", "2626", "a heart warmer"]]

p array.drop(1).sort_by {|a|a.join.scan(/#{search_keys.join("|")}/).length}.reverse

Does anyone know how to combine the last solution so that the lines that do not contain any of the keys are discarded and that they are short as they are?

+1
source

All Articles