5

我正在用 ruby​​ 脚本解析一个大型 CSV 文件,需要从一些搜索键中找到最接近的标题匹配项。搜索键可能是一个或多个值,并且这些值可能不完全匹配,如下所示(应该接近)

search_keys = ["big", "bear"]

一个包含我需要搜索的数据的大数组,只想在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"]
        ]

在这种情况下,我希望它返回该行["3", "three big bears", "2626", "a heart warmer"],因为这是与我的搜索键最接近的匹配。

我希望它从给定的搜索键中返回最接近的匹配项。

我可以使用任何助手/库/宝石吗?以前有人做过吗??

4

5 回答 5

2

我很担心,这项任务应该由数据库级别或类似级别的任何搜索引擎处理,没有必要在应用程序中获取数据并跨列/行进行搜索等,应该很昂贵。但现在这里是简单的方法:)

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 :)
于 2012-05-30T08:30:02.570 回答
1

我认为您可以自己完成,无需使用任何宝石!这可能接近您的需要;在数组中搜索键并为每个找到的元素设置一个排名。

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

这段代码可以比上面写得更好,但我想向你展示细节。

于 2012-05-30T08:19:37.407 回答
1

这行得通。将查找并返回匹配*行的数组作为result.

*matched rows = id、title、code 或 description 匹配任何提供的 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!
于 2012-05-30T09:29:10.377 回答
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"]
于 2012-05-30T09:43:44.217 回答
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

任何人都知道如何组合最后一个解决方案,以便删除不包含任何键的行并保持简洁?

于 2012-05-30T17:20:06.257 回答