gpt4 book ai didi

ruby - 如何根据 Ruby 中的单词列表有效地检查文本?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:32:25 24 4
gpt4 key购买 nike

给定 1,000 个单词的文本,检查 10,000 个单词的字典的有效方法是什么?我想计算非唯一匹配项的数量。

一个想法是将字典存储为散列。但随后我将不得不根据散列检查每个单词,这将是 1,000 次操作。这似乎效率不高。

另一个想法是 Postgres 文本搜索。但是是否可以在一个查询中执行此检查?

另一个想法是将单词存储在 Memcache 或 Redis 数据库中,但这需要 1,000 次查询并且非常慢。

那么,有没有更高效的解决方案呢?

使用 Ruby 工作。

编辑:为 a 添加基准:

Cary 关于 dict_set 更快的断言是正确的:

aw.length
=> 250
dw.length
=> 1233
dict_set.length
=> 1223
t = Time.now; 1000.times{ aw & dw }; Time.now - t
=> 0.682465
t = Time.now; 1000.times{ aw.count{ |w| dict_set.include? w }}; Time.now - t
=> 0.063375

所以,Set#include? 似乎非常有效。

最佳答案

假设:

text = "The quick brown fox and the quick brown bear jumped over the lazy dog"

dictionary = ["dog", "lazy", "quick", "sloth", "the"]

让我们首先将 dictionary 转换为集合:

require 'set'
dict_set = dictionary.to_set
#=> #<Set: {"dog", "lazy", "quick", "sloth", "the"}>

并将 text 转换为小写单词数组:

words = text.downcase.split
#=> ["the", "quick", "brown", "fox", "the", "and", "quick",
# "brown", "bear", "jumped", "over", "the", "lazy", "dog"]

这里有几种计算 dictionarytext 中单词数量的方法。

#1:简单数数

words.count { |w| dict_set.include?(w) }
#=> 7

#2:将相同的词分组并计数

words.group_by(&:itself).reduce(0) { |tot,(k,v)|
tot + ((dict_set.include?(k)) ? v.size : 0) }
#=> 7

Object#itself在 v2.2 中引入。对于早期版本,替换:

group_by(&:itself)

group_by { |w| w }

步骤:

h = words.group_by(&:itself)
#=> {"the" =>["the", "the", "the"],
# "quick"=>["quick", "quick"],
# "brown"=>["brown", "brown"],
# "fox"=>["fox"],
# ...
# "dog"=>["dog"]}
h.reduce(0) { |tot,(k,v)| tot + ((dict_set.include?(k)) ? v.size : 0) }
#=> 7}

考虑到 Set#include?,我预计 #1 通常是最快的非常快。也就是说,我怀疑将相同单词分组的时间是否少于字典查找所节省的时间。

关于ruby - 如何根据 Ruby 中的单词列表有效地检查文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29237290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com