加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-17 03:56:18 所属栏目:百科 来源:网络整理
导读:我是 Ruby的新手,并尝试编写一个方法,该方法将返回字符串中最常见的单词数组.如果有一个具有高计数的单词,则应返回该单词.如果高计数绑定了两个单词,则两者都应以数组形式返回. 问题是,当我通过第二个字符串时,代码只计算“单词”两次而不是三次.当第三个字
我是 Ruby的新手,并尝试编写一个方法,该方法将返回字符串中最常见的单词数组.如果有一个具有高计数的单词,则应返回该单词.如果高计数绑定了两个单词,则两者都应以数组形式返回.

问题是,当我通过第二个字符串时,代码只计算“单词”两次而不是三次.当第三个字符串通过时,它返回“it”,计数为2,这没有任何意义,因为“它”的计数应为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']

解决方法

计算单词实例的方法是你的问题.它是在,所以它是双重计算.

[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,lists of words!') #['words']
p most_common('a short list of words with some short words in it') #['words','short']

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读