ルビーコースラの行間エラーで最大カウントの単語を計算します

アカメブライス

もうすぐ2週間で頭から外せなくなりました。私はRubyの初心者であり、Ruby onRailsのこの最後の部分でコースラの紹介コースにこだわっています。ここで何が悪いのかを理解するのは確かに簡単ですが、今回はこのコースを時間内に終えるのに助けが必要です。

#Implement all parts of this assignment within (this) module2_assignment2.rb file

#Implement a class called LineAnalyzer.
class LineAnalyzer
  #Implement the following read-only attributes in the LineAnalyzer class. 
  #* highest_wf_count - a number with maximum number of occurrences for a single word (calculated)
  #* highest_wf_words - an array of words with the maximum number of occurrences (calculated)
  #* content          - the string analyzed (provided)
  #* line_number      - the line number analyzed (provided)

  #Add the following methods in the LineAnalyzer class.
  #* initialize() - taking a line of text (content) and a line number
  #* calculate_word_frequency() - calculates result

  #Implement the initialize() method to:
  #* take in a line of text and line number
  #* initialize the content and line_number attributes
  #* call the calculate_word_frequency() method.

  #Implement the calculate_word_frequency() method to:
  #* calculate the maximum number of times a single word appears within
  #  provided content and store that in the highest_wf_count attribute.
  #* identify the words that were used the maximum number of times and
  #  store that in the highest_wf_words attribute.

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


    def initialize(content, line)
    @content = content
    @line_number = line
    @highest_wf_count=0
    @highest_wf_words = []
    calculate_word_frequency()
  end

  def calculate_word_frequency()
    @highest_wf_words = Hash.new
    words = @content.split
    words.each { |w|
      if @highest_wf_words.has_key?(w)
        @highest_wf_words[w] += 1
      else
        @highest_wf_words[w] = 1
      end
    }
    @highest_wf_words.sort_by { |word, count| count }
    @highest_wf_words.each do |key, value|
      if value > @highest_wf_count
        @highest_wf_count = value
      end
    end
  end

  def highest_wf_count= (number)
    @highest_wf_count = number
  end

end


#  Implement a class called Solution. 
class Solution

  # Implement the following read-only attributes in the Solution class.
  #* analyzers - an array of LineAnalyzer objects for each line in the file
  #* highest_count_across_lines - a number with the maximum value for highest_wf_words attribute in the analyzers array.
  #* highest_count_words_across_lines - a filtered array of LineAnalyzer objects with the highest_wf_words attribute 
  #  equal to the highest_count_across_lines determined previously.
  
  # Implement the following methods in the Solution class.
  #* analyze_file() - processes 'test.txt' into an array of LineAnalyzers and stores them in analyzers.
  #* calculate_line_with_highest_frequency() - determines the highest_count_across_lines and 
  #  highest_count_words_across_lines attribute values  
  #* print_highest_word_frequency_across_lines() - prints the values of LineAnalyzer objects in 
  #  highest_count_words_across_lines in the specified format

  
  # Implement the analyze_file() method() to:
  #* Read the 'test.txt' file in lines 
  #* Create an array of LineAnalyzers for each line in the file

  # Implement the calculate_line_with_highest_frequency() method to:
  #* calculate the maximum value for highest_wf_count contained by the LineAnalyzer objects in analyzers array
  #  and stores this result in the highest_count_across_lines attribute.
  #* identifies the LineAnalyzer objects in the analyzers array that have highest_wf_count equal to highest_count_across_lines 
  #  attribute value determined previously and stores them in highest_count_words_across_lines.

  #Implement the print_highest_word_frequency_across_lines() method to
  #* print the values of objects in highest_count_words_across_lines in the specified format

  attr_reader  :analyzers, :highest_count_across_lines, :highest_count_words_across_lines

  def initialize
    @highest_count_across_lines = nil
    @highest_count_words_across_lines = nil
    @analyzers = []
  end

  def analyze_file()
    File.foreach('test.txt') do |content, line|
      LineAnalyzer = LineAnalyzer.new(content, line)
      @analyzers << LineAnalyzer
    end
  end

  def calculate_line_with_highest_frequency()
    @highest_count_across_lines = 0
    @highest_count_words_across_lines = []
    @analyzers.each do |analyzers|
      if analyzers.highest_wf_words > @highest_count_across_lines
        @highest_count_across_lines = analyzers.highest_wf_count
      end
      analyzers.highest_wf_words.each do |key, value|
       if analyzers.highest_count_words_across_lines < value
          @highest_count_words_across_lines << key
       end
      end
    end
  end

  def print_highest_word_frequency_across_lines()
    @highest_count_words_across_lines = Array.new
    puts "The following words have the highest word frequency per line:"
  end
end

私は次のようにrspecで19のうち1つが失敗し続けます:

LineAnalyzer
  has accessor for highest_wf_count
  has accessor for highest_wf_words
  has accessor for content
  has accessor for line_number
  has method calculate_word_frequency
  calls calculate_word_frequency when created
  attributes and values
    has attributes content and line_number
    content attribute should have value "test"
    line_number attribute should have value 1
  #calculate_word_frequency
    highest_wf_count value is 3
    highest_wf_words will include "really" and "you"
    content attribute will have value "This is a really really really cool cool you you you"
    line_number attribute will have value 2

Solution
  should respond to #analyze_file
  should respond to #calculate_line_with_highest_frequency
  should respond to #print_highest_word_frequency_across_lines
  #analyze_file
    creates 3 line analyzers
  #calculate_line_with_highest_frequency
    calculates highest count across lines to be 4
    calculates highest count words across lines to be will, it, really (FAILED - 1)

Failures:

  1) Solution#calculate_line_with_highest_frequency calculates highest count words across lines to be will, it, really
     Failure/Error: words_found = solution.highest_count_words_across_lines.map(&:highest_wf_words).flatten

     NoMethodError:
       undefined method `highest_wf_words' for "This":String
     # ./spec/solution_spec.rb:38:in `map'
     # ./spec/solution_spec.rb:38:in `block (3 levels) in <top (required)>'

Finished in 0.10817 seconds (files took 0.37345 seconds to load)
19 examples, 1 failure

Failed examples:

rspec ./spec/solution_spec.rb:31 # Solution#calculate_line_with_highest_frequency calculates highest count words across lines to be will, it, really

そしてこれがsolution_specファイルです。どこに迷うの?助けてください、それは必死の電話です!

require_relative "../module2_assignment"
require 'rspec'

describe Solution do
  subject(:solution) { Solution.new }

  it { is_expected.to respond_to(:analyze_file) } 
  it { is_expected.to respond_to(:calculate_line_with_highest_frequency) } 
  it { is_expected.to respond_to(:print_highest_word_frequency_across_lines) } 

  context "#analyze_file" do
    it "creates 3 line analyzers" do
      expect(solution.analyzers.length).to eq 0 
      solution.analyze_file
      expect(solution.analyzers.length).to eq 3
    end  
  end
  
  context "#calculate_line_with_highest_frequency" do


    it "calculates highest count across lines to be 4" do
      solution.analyze_file

      expect(solution.highest_count_across_lines).to be nil
      
      solution.calculate_line_with_highest_frequency

      expect(solution.highest_count_across_lines).to be 4
    end  
    it "calculates highest count words across lines to be will, it, really" do
      solution.analyze_file

      expect(solution.highest_count_words_across_lines).to be nil
      
      solution.calculate_line_with_highest_frequency

      words_found = solution.highest_count_words_across_lines.map(&:highest_wf_words).flatten
      expect(words_found).to match_array ["will", "it", "really"] 
    end  
  end

end

ムーブソン

あなたは多くの問題を抱えています。

  1. Solution#analyze_fileメソッドではLineAnalyzer、変数として定数(を使用しようとしています:LineAnalyzer = LineAnalyzer.new(content, line)次のように変更する必要があります。

    line_analyzer = LineAnalyzer.new(content, line)
    @analyzers << line_analyzer
    
  2. あなたlineと同じ方法で変数が常にありますnilメソッドの最初の行をに置き換えることで、これを修正できますFile.foreach('test.txt').with_index(1) do |content, line|

  3. の計算@highest_count_across_linesはまったく機能しません。ゼロに設定して反復する代わりに、次のようにします。

    @highest_count_across_lines = analyzers.map(&:highest_wf_count).max
    

私は率直に言ってテストに混乱していて、テストに合格する方法を見つけることにそれほど興奮していません。私見このコースは、非慣用的なRubyを教えています。しかし、うまくいけば、上記があなたを正しい軌道に乗せるでしょう。

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

PandasDataframeコンストラクター内の2つのフィールドで計算/算術演算を実行します

分類Dev

Hadoopを使用してファイル内の単語をカウントするJavaプログラムのコンパイルエラー

分類Dev

パンダのエラーは秒単位で時差を計算します

分類Dev

ロータスノートは、ビューエントリの合計時間/日を計算します

分類Dev

ネストされたng-repeatの合計をカウントし、angularjsで複数のコントローラー/モデルを更新します

分類Dev

データフレームの行間の類似性を計算します(共通のカウント値)

分類Dev

リモートsshベースのLinuxマシンでAndroidエミュレーターを実行し、ローカルブラウザーに表示します

分類Dev

Rubyは、コンポーネント文字の代わりにカウントを使用して単語のインスタンスを集計します

分類Dev

行全体で最大カウントの単語を計算して、実際に(FAILED-1)ruby rspec

分類Dev

文字列の大きなベクトルで単語スコアの合計を計算するパフォーマンスを向上させますか?

分類Dev

Elasticsearchの単純なカウント集計で400エラー

分類Dev

テーブル内の発生をカウントし、rmarkdown、スケーラビリティで表示します

分類Dev

mysqlを使用して、最大スコアに基づいてユーザーのランクを計算します

分類Dev

パンダの連続した値のグループの最大ランレングスを計算します

分類Dev

特定の単語をカウントして出力するのはデータベースの行です

分類Dev

Tensorflow:ラベルの逆カウントを計算します

分類Dev

トランジションのフレームごとにカスタム計算を実行します

分類Dev

単一のグラフィカルアプリを実行するためだけに、デスクトップまたはウィンドウマネージャーなしで小さなディストリビューションが必要

分類Dev

CのUDPクライアントサーバーで毎秒スループットを計算します

分類Dev

Python Flask:リクエストにかかる時間を計算してレスポンスのヘッダーに追加したいフラスコミドルウェアがあります

分類Dev

EditBoxコントロールのビュースコープの変数名を計算します

分類Dev

Yii2-計算フィールドを使用するときのActiveDataProvider「カウント」エラー

分類Dev

PHPでプレーンテキストの単語をカウントするエラーを修正するにはどうすればよいですか?

分類Dev

OpenXml Excel:メールアドレスの後の任意の単語でエラーをスローします

分類Dev

ドキュメントのクラスターが与えられた場合、コーパスとクラスター間の類似性を計算します

分類Dev

Laravelのクエリビルダーとカウント集計を使用したエラー1140

分類Dev

最大値でモーダル ウィンドウの動的幅をブートストラップしますか?

分類Dev

ピクセルあたりのフレームレート、解像度、カラーエンコーディングを指定してビデオサイズを計算するにはどうすればよいですか?

分類Dev

単語以外のカラーコード(\ 033 [0; 31mなど)を使用せずに、すべての主要なディストリビューションで色付きのテキストを出力します

Related 関連記事

  1. 1

    PandasDataframeコンストラクター内の2つのフィールドで計算/算術演算を実行します

  2. 2

    Hadoopを使用してファイル内の単語をカウントするJavaプログラムのコンパイルエラー

  3. 3

    パンダのエラーは秒単位で時差を計算します

  4. 4

    ロータスノートは、ビューエントリの合計時間/日を計算します

  5. 5

    ネストされたng-repeatの合計をカウントし、angularjsで複数のコントローラー/モデルを更新します

  6. 6

    データフレームの行間の類似性を計算します(共通のカウント値)

  7. 7

    リモートsshベースのLinuxマシンでAndroidエミュレーターを実行し、ローカルブラウザーに表示します

  8. 8

    Rubyは、コンポーネント文字の代わりにカウントを使用して単語のインスタンスを集計します

  9. 9

    行全体で最大カウントの単語を計算して、実際に(FAILED-1)ruby rspec

  10. 10

    文字列の大きなベクトルで単語スコアの合計を計算するパフォーマンスを向上させますか?

  11. 11

    Elasticsearchの単純なカウント集計で400エラー

  12. 12

    テーブル内の発生をカウントし、rmarkdown、スケーラビリティで表示します

  13. 13

    mysqlを使用して、最大スコアに基づいてユーザーのランクを計算します

  14. 14

    パンダの連続した値のグループの最大ランレングスを計算します

  15. 15

    特定の単語をカウントして出力するのはデータベースの行です

  16. 16

    Tensorflow:ラベルの逆カウントを計算します

  17. 17

    トランジションのフレームごとにカスタム計算を実行します

  18. 18

    単一のグラフィカルアプリを実行するためだけに、デスクトップまたはウィンドウマネージャーなしで小さなディストリビューションが必要

  19. 19

    CのUDPクライアントサーバーで毎秒スループットを計算します

  20. 20

    Python Flask:リクエストにかかる時間を計算してレスポンスのヘッダーに追加したいフラスコミドルウェアがあります

  21. 21

    EditBoxコントロールのビュースコープの変数名を計算します

  22. 22

    Yii2-計算フィールドを使用するときのActiveDataProvider「カウント」エラー

  23. 23

    PHPでプレーンテキストの単語をカウントするエラーを修正するにはどうすればよいですか?

  24. 24

    OpenXml Excel:メールアドレスの後の任意の単語でエラーをスローします

  25. 25

    ドキュメントのクラスターが与えられた場合、コーパスとクラスター間の類似性を計算します

  26. 26

    Laravelのクエリビルダーとカウント集計を使用したエラー1140

  27. 27

    最大値でモーダル ウィンドウの動的幅をブートストラップしますか?

  28. 28

    ピクセルあたりのフレームレート、解像度、カラーエンコーディングを指定してビデオサイズを計算するにはどうすればよいですか?

  29. 29

    単語以外のカラーコード(\ 033 [0; 31mなど)を使用せずに、すべての主要なディストリビューションで色付きのテキストを出力します

ホットタグ

アーカイブ