Я новичок в ruby, и у меня возникли проблемы с выполнением этой задачи, он должен вычислить наибольшее количество слов в строках, где находится мой код. я пытался решить эту проблему в течение 2 месяцев, и я смотрел ваши видео-ролики о ruby и успокаивался в срок 4 раза, пожалуйста, помогите
class LineAnalyzer
attr_reader :highest_wf_count, :highest_wf_words, :content, :line_number
def initialize(line, line_number)
@content = line
@line_number = line_number
self.calculate_word_frequency(line)
end
def calculate_word_frequency(line)
word_frequency = Hash.new(0)
line.split.each do |word|
word_frequency[word.downcase] += 1
end
@highest_wf_count = word_frequency.values.max
if @highest_wf_count >1
@highest_wf_words = word_frequency.select {|key,value| value == word_frequency.values.max}.keys
end
end
end
class Solution
attr_reader :analyzers, :highest_count_across_lines, :highest_count_words_across_lines, :highest_count_across_lines
def initialize
@analyzers = []
@highest_count_words_across_lines =nil
end
def analyze_file
text = File.open("./test.txt", "r")
text.each_line do |line|
@analyzers << line
end
end
def calculate_line_with_highest_frequency
c=[]
@arr = {}
@analyzers.each_with_index do |line, index|
lll = LineAnalyzer.new(line,index)
@arr[lll.highest_wf_words] = lll.highest_wf_count
if lll.highest_wf_words != nil
c.push(lll.highest_wf_words)#кол-во встречаний
end
end
x=c.join(" ")
x=x.split
z =x[1]
b =x[2]
d =x[0]
x[0] =z
x[1] =b
x[2] =d
@highest_count_words_across_lines =x
@highest_count_across_lines = @arr.values.max#4
end
def print_highest_word_frequency_across_lines
puts @highest_count_words_across_lines
end
end
есть решение_spe c
r
equire_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
и есть line_analyzer_spe c
require_relative "../module2_assignment"
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
и есть ошибка
/shared/submission/anything.zip: 0 tests run
["\n", "LineAnalyzer\n", " has accessor for highest_wf_count\n", " has
["\accessor for highest_wf_words\n", " has accessor for content\n", " has
["\accessor for line_number\n", " has method calculate_word_frequency\n", "
["\calls calculate_word_frequency when created\n", " attributes and
["\values\n", " has attributes content and line_number\n", " content
["\attribute\n", " line_number attribute\n", "
["\#calculate_word_frequency\n", " highest_wf_count value is 3\n", "
["\highest_wf_words check\n", " content attribute will have expected
["\value\n", " line_number attribute\n", "\n", "Solution\n", " should
["\respond to #analyze_file\n", " should respond to
["\#calculate_line_with_highest_frequency\n", " should respond to
["\#print_highest_word_frequency_across_lines\n", " #analyze_file\n", "
["\creates 3 line analyzers\n", " #calculate_line_with_highest_frequency\n",
["\" calculates highest count across lines to be 4\n", " calculates
["\highest count words across lines to be will, it, really (FAILED - 1)\n",
["\"\n", "Failures:\n", "\n", " 1)
["\Solution#calculate_line_with_highest_frequency calculates highest count
["\words across lines to be will, it, really\n", " Failure/Error:
["\words_found =
["\solution.highest_count_words_across_lines.map(&:highest_wf_words).flatten\n",
["\"\n", " NoMethodError:\n", " undefined method `highest_wf_words'
["\for \"will\":String\n", " # ./spec/solution_spec.rb:39:in `map'\n", "
["\ # ./spec/solution_spec.rb:39:in `block (3 levels) in <top
["\(required)>'\n", "\n", "Finished in 0.01251 seconds (files took 0.09799
["\seconds to load)\n", "19 examples, 1 failure\n", "\n", "Failed
["\examples:\n", "\n", "rspec ./spec/solution_spec.rb:32 #
["\Solution#calculate_line_with_highest_frequency calculates highest count
["\words across lines to be will, it, really\n", "\n"][]