如何计算Ruby中最高的词频

问题描述 投票:-1回答:2

我一直在为Coursera Rails入门课程进行这项作业。我们受命编写一个程序来计算文本文件中的最大单词频率。我们被指示创建一种方法:

  1. 计算单个单词出现在给定内容中并存储在highest_wf_count中的最大次数。
  2. 确定使用次数最多的单词并将其存储在highest_wf_words中。

[当我运行提供给我们的rspec测试时,一个测试失败了。我打印了输出以查看问题出在哪里,但无法解决。

这是我的代码,rspec测试以及得到的内容:

class LineAnalyzer

  attr_accessor :highest_wf_count
  attr_accessor :highest_wf_words
  attr_accessor :content
  attr_accessor :line_number

  def initialize(content, line_number)
    @content = content
    @line_number = line_number
    @highest_wf_count = 0
    @highest_wf_words = highest_wf_words
    calculate_word_frequency
  end
  def calculate_word_frequency()
    @highest_wf_words = Hash.new(0)
    @content.split.each do |word|
      @highest_wf_words[word.downcase!] += 1
      if @highest_wf_words.has_key?(word)
        @highest_wf_words[word] += 1 
      else
        @highest_wf_words[word] = 1
      end
      @highest_wf_words.sort_by{|word, count| count}
      @highest_wf_count = @highest_wf_words.max_by {|word, count| count}
    end
  end
  def highest_wf_count()
    p @highest_wf_count
  end
end

这是rspec代码:

require 'rspec'

describe LineAnalyzer do
  subject(:lineAnalyzer) { LineAnalyzer.new("test", 1) }

  it "has accessor for highest_wf_count" do
    is_expected.to respond_to(:highest_wf_count) 
  end 
  it "has accessor for highest_wf_words" do
    is_expected.to respond_to(:highest_wf_words) 
  end
  it "has accessor for content" do
    is_expected.to respond_to(:content) 
  end
  it "has accessor for line_number" do
    is_expected.to respond_to(:line_number) 
  end
  it "has method calculate_word_frequency" do
    is_expected.to respond_to(:calculate_word_frequency) 
  end
  context "attributes and values" do
  it "has attributes content and line_number" do
    is_expected.to have_attributes(content: "test", line_number: 1) 
  end
  it "content attribute should have value \"test\"" do
    expect(lineAnalyzer.content).to eq("test")
  end
  it "line_number attribute should have value 1" do
    expect(lineAnalyzer.line_number).to eq(1)
  end
end

  it "calls calculate_word_frequency when created" do
    expect_any_instance_of(LineAnalyzer).to receive(:calculate_word_frequency)
    LineAnalyzer.new("", 1) 
  end

  context "#calculate_word_frequency" do
    subject(:lineAnalyzer) { LineAnalyzer.new("This is a really really really cool cool you you you", 2) }

    it "highest_wf_count value is 3" do
      expect(lineAnalyzer.highest_wf_count).to eq(3)
    end
    it "highest_wf_words will include \"really\" and \"you\"" do
      expect(lineAnalyzer.highest_wf_words).to include 'really', 'you'
    end
    it "content attribute will have value \"This is a really really really cool cool you you you\"" do
      expect(lineAnalyzer.content).to eq("This is a really really really cool cool you you you")
    end
    it "line_number attribute will have value 2" do
      expect(lineAnalyzer.line_number).to eq(2)
    end
  end
end

这是rspec输出:

13 examples, 1 failure

Failed examples:

rspec ./course01/module02/assignment-Calc-Max-Word-Freq/spec/line_analyzer_spec.rb:42 # LineAnalyzer#calculate_word_frequency highest_wf_count value is 3

我的输出:

#<LineAnalyzer:0x00007fc7f9018858 @content="This is a really really really cool cool you you you", @line_number=2, @highest_wf_count=[nil, 10], @highest_wf_words={"this"=>2, nil=>10, "is"=>1, "a"=>1, "really"=>3, "cool"=>2, "you"=>3}>
  1. 根据测试字符串,字数不正确。
  2. “ nil”被包含在哈希中。
  3. 没有像应该按值(计数)对哈希进行排序。

我尝试了几种方法来解决这些问题,但没有任何效果。我再次浏览了讲课材料,但是找不到任何有帮助的内容,而且讨论板也很少受到学生提问的监视。

ruby-on-rails ruby rspec rspec-rails
2个回答
1
投票

根据Ruby documentation

downcase!(* args)public

压缩str的内容,如果未进行任何更改,则返回nil。

由于.downcase!方法的这种意外行为,如果单词已经全部为小写,则您将在此行中增加nil的出现:

@highest_wf_words[word.downcase!] += 1

测试也失败了,因为@highest_wf_words.max_by {|word, count| count}返回一个包含计数和一个单词的数组,而我们只想获取计数。

通过测试的简化的calculate_word_frequency方法看起来像这样:

  def calculate_word_frequency()
    @highest_wf_words = Hash.new(0)

    @content.split.each do |word|
      # we don't have to check if the word existed before
      # because we set 0 as default value in @highest_wf_words hash

      # use .downcase instead of .downcase!
      @highest_wf_words[word.downcase] += 1

      # extract only the count, and then get the max
      @highest_wf_count = @highest_wf_words.map {|word, count| count}.max
    end
  end

1
投票

Nil

零是downcase!

这将修改字符串就位,如果没有任何更改,则返回nil。如果您说“这很奇怪”,那么您说得对(恕我直言)。

# just use the non destructive variant
word.downcase

排序

sort_by返回一个新对象(哈希,数组等),并且不修改该方法的接收者。您需要重新分配或使用sort_by!

unsorted = [3, 1, 2]
sorted = unsorted.sort

p unsorted # => [3, 1, 2]
p sorted # => [1, 2, 3]

unsorted.sort!
p unsorted # => [1, 2, 3]

错误字数

一旦纠正了这两个错误,它看起来就会更好。请注意,该方法不会返回单个整数,而是返回包含单词和count的两个元素的数组,因此它应如下所示:["really", 6]

简化事物:

如果可以使用ruby 2.7,那么有方便的Enumerable#tally方法!

%w(foo foo bar foo baz foo).tally
=> {"foo"=>4, "bar"=>1, "baz"=>1}

示例取自https://medium.com/@baweaver/ruby-2-7-enumerable-tally-a706a5fb11ea

© www.soinside.com 2019 - 2024. All rights reserved.