【发布时间】:2022-10-01 08:07:39
【问题描述】:
我有一本单词词典,我想检查给定的字符串是否包含这些单词。我希望它们存储在哈希中,键是重复的单词,值是它出现的次数。
目前,它只会存储完整的字符串匹配项(以下不计入包含单词low),实际上并不会增加重复计数。
指出我正确的方向? :)
dictionary = [\"below\",\"down\",\"go\",\"going\",\"horn\",\"how\",\"howdy\",\"it\",\"i\",\"low\",\"own\",\"part\",\"partner\",\"sit\"]
def substringer(string, dict)
string_array = string.split(/\\W+/)
final_hash = {}
count = 0
dict.each do |entry|
if string_array.include?(entry)
final_hash = {entry => +1}
p final_hash
end
end
end
substringer(\"below, below, how\'s it goin?\", dictionary)
结果
{\"below\"=>1}
{\"how\"=>1}
{\"it\"=>1}
标签: ruby