ruby-on-rails - 如何在哈希栏中搜索单词

标签 ruby-on-rails ruby hash

您好,我正在使用 ruby​​-2.5.0 和 rails 5 开发一个 Ruby on Rails 项目。我有一个可以是不同格式的散列。如果散列中存在任何单词,我将在此散列中搜索某些单词,并返回 true 。单词可以是键或值。

Json Format Example:
{
    "data": {

        "Lines": [{
                "Words": [{
                    "WordText": "Walmart",
                    "Left": 63,
                    "Top": 33,
                    "Height": 72,
                    "Width": 364
                }],
                "MaxHeight": 72,
                "MinTop": 33
            },
            {
                "Words": [{
                        "WordText": "Save",
                        "Left": 70,
                        "Top": 116,
                        "Height": 26,
                        "Width": 68
                    },
                    {
                        "WordText": "money.",
                        "Left": 148,
                        "Top": 123,
                        "Height": 25,
                        "Width": 108
                    },
                    {
                        "WordText": "Live",
                        "Left": 267,
                        "Top": 118,
                        "Height": 25,
                        "Width": 59
                    },
                    {
                        "WordText": "better.",
                        "Left": 335,
                        "Top": 117,
                        "Height": 26,
                        "Width": 100
                    }
                ],
                "MaxHeight": 26,
                "MinTop": 116
            },
            {
                "Words": [{
                        "WordText": "srg",
                        "Left": 53,
                        "Top": 176,
                        "Height": 20,
                        "Width": 36
                    },
                    {
                        "WordText": "2006",
                        "Left": 105,
                        "Top": 176,
                        "Height": 21,
                        "Width": 49
                    },
                    {
                        "WordText": "00006167",
                        "Left": 222,
                        "Top": 177,
                        "Height": 21,
                        "Width": 100
                    },
                    {
                        "WordText": "03",
                        "Left": 390,
                        "Top": 178,
                        "Height": 22,
                        "Width": 23
                    },
                    {
                        "WordText": "04247",
                        "Left": 480,
                        "Top": 179,
                        "Height": 21,
                        "Width": 62
                    }
                ],
                "MaxHeight": 22,
                "MinTop": 176
            },
            {
                "Words": [{
                        "WordText": "MOTOR",
                        "Left": 53,
                        "Top": 201,
                        "Height": 22,
                        "Width": 63
                    },
                    {
                        "WordText": "OIL",
                        "Left": 132,
                        "Top": 201,
                        "Height": 22,
                        "Width": 36
                    }
                ],
                "MaxHeight": 22,
                "MinTop": 201
            },
            {
                "Words": [{
                    "WordText": "007310200289",
                    "Left": 230,
                    "Top": 202,
                    "Height": 22,
                    "Width": 152
                }],
                "MaxHeight": 22,
                "MinTop": 202
            },
            {
                "Words": [{
                    "WordText": "SUBTOTAL",
                    "Left": 297,
                    "Top": 227,
                    "Height": 22,
                    "Width": 101
                }],
                "MaxHeight": 22,
                "MinTop": 227
            },
            {
                "Words": [{
                    "WordText": "21.97",
                    "Left": 474,
                    "Top": 228,
                    "Height": 21,
                    "Width": 61
                }],
                "MaxHeight": 21,
                "MinTop": 228
            },
            {
                "Words": [{
                        "WordText": "TAX",
                        "Left": 175,
                        "Top": 250,
                        "Height": 22,
                        "Width": 36
                    },
                    {
                        "WordText": "1",
                        "Left": 228,
                        "Top": 251,
                        "Height": 21,
                        "Width": 7
                    }
                ],
                "MaxHeight": 22,
                "MinTop": 250
            },
            {
                "Words": [{
                    "WordText": "7.700",
                    "Left": 283,
                    "Top": 251,
                    "Height": 21,
                    "Width": 62
                }],
                "MaxHeight": 21,
                "MinTop": 251
            },
            {
                "Words": [{
                    "WordText": "1.69",
                    "Left": 488,
                    "Top": 252,
                    "Height": 21,
                    "Width": 47
                }],
                "MaxHeight": 21,
                "MinTop": 252
            },
            {
                "Words": [{
                    "WordText": "rorAL",
                    "Left": 337,
                    "Top": 275,
                    "Height": 22,
                    "Width": 61
                }],
                "MaxHeight": 22,
                "MinTop": 275
            },
            {
                "Words": [{
                    "WordText": "23.66",
                    "Left": 474,
                    "Top": 276,
                    "Height": 22,
                    "Width": 61
                }],
                "MaxHeight": 22,
                "MinTop": 276
            },
            {
                "Words": [{
                        "WordText": "SHOPPING",
                        "Left": 160,
                        "Top": 299,
                        "Height": 21,
                        "Width": 101
                    },
                    {
                        "WordText": "CARD",
                        "Left": 277,
                        "Top": 299,
                        "Height": 22,
                        "Width": 48
                    },
                    {
                        "WordText": "TEND",
                        "Left": 341,
                        "Top": 300,
                        "Height": 21,
                        "Width": 49
                    }
                ],
                "MaxHeight": 22,
                "MinTop": 299
            },
            {
                "Words": [{
                    "WordText": "23.66",
                    "Left": 474,
                    "Top": 300,
                    "Height": 22,
                    "Width": 61
                }],
                "MaxHeight": 22,
                "MinTop": 300
            },
            {
                "Words": [{
                        "WordText": "CHANGE",
                        "Left": 269,
                        "Top": 324,
                        "Height": 21,
                        "Width": 74
                    },
                    {
                        "WordText": "DUE",
                        "Left": 359,
                        "Top": 324,
                        "Height": 21,
                        "Width": 36
                    }
                ],
                "MaxHeight": 21,
                "MinTop": 324
            },
            {
                "Words": [{
                    "WordText": "07/21/15",
                    "Left": 61,
                    "Top": 386,
                    "Height": 22,
                    "Width": 101
                }],
                "MaxHeight": 22,
                "MinTop": 386
            },
            {
                "Words": [{
                        "WordText": "ITEMS",
                        "Left": 173,
                        "Top": 455,
                        "Height": 42,
                        "Width": 120
                    },
                    {
                        "WordText": "SOLD",
                        "Left": 326,
                        "Top": 456,
                        "Height": 41,
                        "Width": 96
                    },
                    {
                        "WordText": "I",
                        "Left": 458,
                        "Top": 457,
                        "Height": 40,
                        "Width": 11
                    }
                ],
                "MaxHeight": 42,
                "MinTop": 455
            },
            {
                "Words": [{
                        "WordText": "Washington",
                        "Left": 88,
                        "Top": 539,
                        "Height": 23,
                        "Width": 129
                    },
                    {
                        "WordText": "E-Cycles:",
                        "Left": 226,
                        "Top": 539,
                        "Height": 23,
                        "Width": 106
                    },
                    {
                        "WordText": "Free",
                        "Left": 341,
                        "Top": 540,
                        "Height": 18,
                        "Width": 50
                    },
                    {
                        "WordText": "Recycling",
                        "Left": 400,
                        "Top": 540,
                        "Height": 24,
                        "Width": 107
                    }
                ],
                "MaxHeight": 24,
                "MinTop": 539
            },
            {
                "Words": [{
                        "WordText": "For",
                        "Left": 103,
                        "Top": 568,
                        "Height": 18,
                        "Width": 37
                    },
                    {
                        "WordText": "Computers,",
                        "Left": 148,
                        "Top": 568,
                        "Height": 23,
                        "Width": 126
                    },
                    {
                        "WordText": "Monitors,",
                        "Left": 283,
                        "Top": 569,
                        "Height": 21,
                        "Width": 100
                    },
                    {
                        "WordText": "and",
                        "Left": 392,
                        "Top": 570,
                        "Height": 18,
                        "Width": 40
                    },
                    {
                        "WordText": "TV's",
                        "Left": 441,
                        "Top": 570,
                        "Height": 18,
                        "Width": 48
                    }
                ],
                "MaxHeight": 23,
                "MinTop": 568
            },
            {
                "Words": [{
                        "WordText": "www.ecyclewashington.org",
                        "Left": 52,
                        "Top": 597,
                        "Height": 24,
                        "Width": 301
                    },
                    {
                        "WordText": "1-800-RECYCLE",
                        "Left": 363,
                        "Top": 598,
                        "Height": 19,
                        "Width": 185
                    }
                ],
                "MaxHeight": 24,
                "MinTop": 597
            },
            {
                "Words": [{
                        "WordText": "Lou",
                        "Left": 49,
                        "Top": 643,
                        "Height": 20,
                        "Width": 36
                    },
                    {
                        "WordText": "Prices",
                        "Left": 101,
                        "Top": 643,
                        "Height": 21,
                        "Width": 75
                    },
                    {
                        "WordText": "You",
                        "Left": 192,
                        "Top": 643,
                        "Height": 21,
                        "Width": 36
                    },
                    {
                        "WordText": "Can",
                        "Left": 244,
                        "Top": 644,
                        "Height": 21,
                        "Width": 35
                    },
                    {
                        "WordText": "Trust.",
                        "Left": 295,
                        "Top": 644,
                        "Height": 21,
                        "Width": 72
                    },
                    {
                        "WordText": "Every",
                        "Left": 386,
                        "Top": 644,
                        "Height": 23,
                        "Width": 62
                    },
                    {
                        "WordText": "Day.",
                        "Left": 463,
                        "Top": 645,
                        "Height": 21,
                        "Width": 47
                    }
                ],
                "MaxHeight": 23,
                "MinTop": 643
            },
            {
                "Words": [{
                    "WordText": "07/21/15",
                    "Left": 145,
                    "Top": 668,
                    "Height": 22,
                    "Width": 100
                }],
                "MaxHeight": 22,
                "MinTop": 668
            }
        ],
        "HasOverlay": true,
        "Message": "Total lines: 22",
        "ParsedText": "Walmart"
    }

}

假设我有一个单词数组 ["Walmart","Optical","Anker"]。我想检查哈希中是否存在数组中的任何单词,然后返回 true。 Json 格式可以不同。请帮助我如何在哈希中搜索某些单词。提前致谢。

最佳答案

我想您只是想查找数据是否包含任何单词列表?如果是的话

h = #your data set as posted so I won't put it here again

["Walmart","Optical","Anker"].select{|w| h.to_s.include?(w) } # shows which words were found. 


["Walmart","Optical","Anker"].any?{|w| h.to_s.include?(w) } # returns bool
# => true

非常简单,我认为您需要更复杂的东西。在这种情况下,将数据散列转换为字符串,并查看该词是否包含在字符串中。

更新:

也许这在 Ruby 版本 >= 2.3 中更为惯用你可以挖掘但你仍然需要知道结构。 @mudasobwa 如果结构不可靠,您有什么建议?

def deep_find_words(hash, words_to_match)
  words =
  hash.dig(:data, :Lines).map do |line|
   line.dig(:Words).map do |word|
      word.dig(:WordText)
    end
  end.flatten
  words_to_match.select{|w| words.include? w}
end

deep_find_words(h, ["Walmart","Optical","Anker"])

关于ruby-on-rails - 如何在哈希栏中搜索单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49588208/

相关文章:

javascript - 为什么我的 AJAX 请求在 dev 中是 PUT 请求,但在运行测试时是 GET 请求?

ruby-on-rails - 获取任意(类型未知)模型实例的 ActiveModel::Serializer 实例

ruby-on-rails - heroku 打开 - 未指定应用程序

Ruby - 从键数组中设置嵌套散列的值

javascript - 如何从字符串中获取哈希值,然后在 javascript 中从哈希值中获取字符串

ruby-on-rails - Rails 控制台的 YAML 输出

ruby-on-rails - RoR 的 "magic"命名空间解析是如何工作的?

c# - 将 N 个 Excel 工作簿中的第 X 个工作表复制到新工作簿中 - 使用哪种语言?

ruby - "rvm use"上的 RVM 1.25.22(稳定)错误

perl:洗牌值排序哈希?