ruby - 字符串中最常见的单词

标签 ruby arrays string hash

我是 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/

相关文章:

Ruby gem Mechanize 与 jRuby 1.6.0 (Ruby 1.9.2)

php - 在php中替换字符串的前两个字符

c++ - 如何从列表中找到以 'b'开头的单词以及带有4个符号的单词?

ruby-on-rails - 如何检查子链接文件是否存在

ruby-on-rails - 用于 ruby​​/ruby on rails 的 SOAP 客户端生成器

arrays - 如何使用 MongoDB 上的聚合从数组中删除字段?

arrays - 顺序遍历数组的最快算法

php - Codeigniter 关联数组 - 在 foreach 循环中更改数组名称

c# - 用一个字符替换字符串中的整个字符

sql - 与 Rails 中的 find_by_sql 相关的问题