我是 Ruby 的新手,正在尝试编写一个方法来返回字符串中最常见单词的数组。如果有一个词的计数很高,则应返回该词。如果有两个词与高计数并列,则应在数组中返回这两个词。
问题是当我通过第二个字符串时,代码只计算“单词”两次而不是三次。当传递第三个字符串时,它返回计数为 2 的“it”,这是没有意义的,因为“it”的计数应该为 1。
def most_common(string)
counts = {}
words = string.downcase.tr(",.?!",'').split(' ')
words.uniq.each do |word|
counts[word] = 0
end
words.each do |word|
counts[word] = string.scan(word).count
end
max_quantity = counts.values.max
max_words = counts.select { |k, v| v == max_quantity }.keys
puts max_words
end
most_common('a short list of words with some words') #['words']
most_common('Words in a short, short words, lists of words!') #['words']
most_common('a short list of words with some short words in it') #['words', 'short']
最佳答案
您计算单词实例的方法是您的问题。 它
在with
中,所以它被重复计算了。
[1] pry(main)> 'with some words in it'.scan('it')
=> ["it", "it"]
虽然可以更简单地完成,您可以使用 each_with_object
调用按值的实例数对数组的内容进行分组,如下所示:
counts = words.each_with_object(Hash.new(0)) { |e, h| h[e] += 1 }
这会遍历数组中的每个条目,并将散列中每个单词条目的值加 1。
因此以下内容应该适合您:
def most_common(string)
words = string.downcase.tr(",.?!",'').split(' ')
counts = words.each_with_object(Hash.new(0)) { |e, h| h[e] += 1 }
max_quantity = counts.values.max
counts.select { |k, v| v == max_quantity }.keys
end
p most_common('a short list of words with some words') #['words']
p most_common('Words in a short, short words, lists of words!') #['words']
p most_common('a short list of words with some short words in it') #['words', 'short']
关于ruby - 字符串中最常见的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26388864/