Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ruby关键字搜索数组_Ruby_Arrays_Search - Fatal编程技术网

ruby关键字搜索数组

ruby关键字搜索数组,ruby,arrays,search,Ruby,Arrays,Search,我正在解析ruby脚本中的一个大型CSV文件,需要从一些搜索键中找到与标题最接近的匹配项。搜索键可能有一个或多个值,这些值可能不完全匹配,如下所示(应接近) 一个包含我需要搜索的数据的大数组,我只想在title列中搜索: array = [ ["id", "title", "code", "description"], ["1", "once upon a time", "3241", "a classic story"],

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

一个包含我需要搜索的数据的大数组,我只想在
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”,“三只大熊”,“2626”,“一个心脏加热器”]
,因为这是与我的搜索键最接近的匹配项

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


有我可以使用的助手/库/宝石吗?以前有人这样做过吗???

我想你可以自己做,不需要使用任何宝石! 这可能接近你所需要的;在数组中搜索键并为每个找到的元素设置一个等级

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
result=[]
array.each do|ar|
排名=0
搜索|键。每个do |键|
如果ar[1]。包括?(键)
秩+=1
结束
结束
如果排名>0

结果我很担心,这个任务应该交给任何数据库级别或类似级别的搜索引擎处理,在应用程序中毫无意义地获取数据和跨列/行搜索等,应该很昂贵。但现在,这里有一个简单的方法:)

数组=[
[“id”、“标题”、“代码”、“说明”],
[“1”、“从前”、“3241”、“经典故事”],
[“2”,“大灰狼”,“4235”,“有点吓人”],
[“3”,“三只大熊”,“2626”,“一颗温暖的心”]
]
h={}
搜索键=[“大”,“熊”]
数组[1..-1]。每个do | rec|
rec\u id=rec[0]。到\u i
搜索|键。每个do |键|
如果记录[1],是否包括?钥匙
h[rec_id]=h[rec_id]?(h[rec_id]+1):1
结束
结束
结束
最近的=h键优先
h、 每个人都做记录,计数
如果h[最近]所需输出:)

这很有效。将查找并返回匹配的*行数组作为
result

*匹配行=id、标题、代码或描述与提供的任何搜索关键字匹配的行。包括部分搜索,如“熊”中的“熊”

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!
result=[]
array.each do | a|
a、 每个人都有|
搜索关键字。每个都可以|

结果你可以用一种更简洁的方式来写

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"]

这是我的一条线

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

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

确定匹配的度量是什么?我在想标题字符串
。包括?
关键字,递归地搜索所有关键字,然后得到最高命中率的行或类似的东西。这个解决方案看起来很酷。我不能让第一行工作,但我可以让第二行删除所有结果,不点击,这将是非常有用的。很高兴听到,但让我惊讶的是,其中一行不工作,你使用Ruby193,那么它们应该都工作,第一行提供了多维数组的过滤版本,第二个是一个排序的版本,没有标题,这一个很好用,非常简洁。如果我能得到结果。uniq!对它们进行排序,使最高的重复数在第一位,这将是完美的。这类似于下面同位素提供的答案,但有一个排名系统,我喜欢这个,我想我可能会使用它。谢谢。我在你的末尾添加了以下代码以按等级排序<代码>结果.排序!{| a,b | b[1]a[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