diff --git a/2355/2/find_obscenity.rb b/2355/2/find_obscenity.rb index cad74b7c3..8368981c8 100644 --- a/2355/2/find_obscenity.rb +++ b/2355/2/find_obscenity.rb @@ -1,58 +1,51 @@ require 'russian_obscenity' -# rubocop:disable Metrics/MethodLength -# rubocop:disable Metrics/AbcSize -# rubocop:disable Style/IfUnlessModifier -# rubocop:disable Lint/ImplicitStringConcatenation -# rubocop:disable Performance/RedundantMatch # This class is needed to find and collect all obscenity from text files -# This class smells of :reek:Attribute -# This class smells of :reek:InstanceVariableAssumption class FindObscenity - attr_accessor :obscenity + attr_reader :obscenity def initialize(battler) @battler = battler + @mistakes = [] + @obscenity = [] + end + + def dir_count + Dir[File.join("./rap-battles/#{@battler}/", '**', '*')].count { |file| File.file?(file) } end def initialize_mistakes - @mistakes = [] - file = File.new('./mistakes') - file.each { |line| @mistakes << line.delete("\n") } + File.new('./mistakes').each { |line| @mistakes << line.delete("\n") } end - # This method smells of :reek:DuplicateMethodCall # This method smells of :reek:NestedIterators - # This method smells of :reek:TooManyStatements - # This method smells of :reek:UncommunicativeVariableName - def check_battles_for_obscenity - initialize_mistakes - @obscenity = [] - 1.upto(Dir[File.join("./rap-battles/#{@battler}/", '**', '*')].count { |file| File.file?(file) }) do |i| - file = File.new("./rap-battles/#{@battler}/#{i}") - file.each do |line| - mass = line.split - mass.each do |word| - if word.match(/.*\*.*[А-Яа-я.,]$/) - word = word.delete '.' ',' '?»' '"' '!' ';' + def first_check + 1.upto(dir_count) do |text| + File.new("./rap-battles/#{@battler}/#{text}").each do |line| + line.split.each do |word| + if word =~ /.*\*.*[А-Яа-я.,]$/ + word = word.delete '.', ',', '?»', '"', '!', ';' @obscenity << word end end - rus_obs = RussianObscenity.find(line) - rus_obs.each do |word| - @mistakes.each do |mis| - if mis.casecmp(word) - rus_obs.delete(word) - end - end + end + end + end + + # This method smells of :reek:NestedIterators + def check_rus_obs + 1.upto(dir_count) do |text| + File.new("./rap-battles/#{@battler}/#{text}").each do |line| + RussianObscenity.find(line).each do |word| + @mistakes.each { |mis| @obscenity << word unless mis.casecmp(word) } end - rus_obs.each { |word| @obscenity << word } end end end + + def check_battles_for_obscenity + initialize_mistakes + first_check + check_rus_obs + end end -# rubocop:enable Metrics/MethodLength -# rubocop:enable Metrics/AbcSize -# rubocop:enable Style/IfUnlessModifier -# rubocop:enable Lint/ImplicitStringConcatenation -# rubocop:enable Performance/RedundantMatch diff --git a/2355/2/main.rb b/2355/2/main.rb index 782980e5d..5b5c84ce4 100644 --- a/2355/2/main.rb +++ b/2355/2/main.rb @@ -3,53 +3,55 @@ require 'optparse' require 'terminal-table' -# rubocop:disable Lint/UnusedBlockArgument -# rubocop:disable Metrics/LineLength # rubocop:disable Metrics/BlockLength -# rubocop:disable Style/UnneededInterpolation -# rubocop:disable Style/ConditionalAssignment -# rubocop:disable Layout/TrailingWhitespace +# This disable is needed because this block is the main logic of the program. OptionParser.new do |opts| opts.on('--top-bad-words=') do |bad| - if !bad.empty? - bad_words = bad.to_i - else - bad_words = 1 - end + bad_words = if !bad.empty? + bad.to_i + else + 1 + end top = TopBad.new top.set_battlers_names top.set_top_obscenity - top_bad_words = top.top_obscenity.sort_by { |key, val| val } + top_bad_words = top.top_obscenity.sort_by { |_key, val| val } top_bad_words = top_bad_words.reverse table = Terminal::Table.new do |t| (bad_words - 1).times do |i| - t << ["#{top_bad_words[i][0]}", "#{top_bad_words[i][1]}" + ' нецензурных слов(а)', "#{top.average_bad_words_in_battle("#{top_bad_words[i][0]}")}" + ' слов(а) на баттл', "#{top.average_words_in_round("#{top_bad_words[i][0]}")}" + ' слов(а) в раунде'] + t << [top_bad_words[i][0], + top_bad_words[i][1].to_s + ' нецензурных слов(а)', + top.average_bad_words_in_battle(top_bad_words[i][0]).to_s + ' слов(а) на баттл', + top.average_words_in_round(top_bad_words[i][0]).to_s + ' слов(а) в раунде'] t << :separator end i = bad_words - 1 - t << ["#{top_bad_words[i][0]}", "#{top_bad_words[i][1]}" + ' нецензурных слов(а)', "#{top.average_bad_words_in_battle("#{top_bad_words[i][0]}")}" + ' слов(а) на баттл', "#{top.average_words_in_round("#{top_bad_words[i][0]}")}" + ' слов(а) в раунде'] + t << [top_bad_words[i][0], + top_bad_words[i][1].to_s + ' нецензурных слов(а)', + top.average_bad_words_in_battle(top_bad_words[i][0]).to_s + ' слов(а) на баттл', + top.average_words_in_round(top_bad_words[i][0]).to_s + ' слов(а) в раунде'] end puts table end opts.on('--top-words=') do |top_w| - if !top_w.empty? - top_words = top_w.to_i - else - top_words = 30 - end + top_words = if !top_w.empty? + top_w.to_i + else + 30 + end opts.on('--name=') do |b_name| - if b_name.empty? + if b_name.empty? puts 'Choose your destiny!' else name_b = b_name top = TopBad.new top.set_battlers_names - if top.battlers.index("#{name_b}").nil? - puts 'Я не знаю рэпера ' + "#{name_b}" + ', но знаю таких: ' - top.battlers.each { |battl| puts battl } + if !top.battlers.include?(name_b) + puts 'Я не знаю рэпера ' + name_b + ', но знаю таких: ' + top.battlers.each { |battler| puts battler } else - t_w = TopWord.new("#{name_b}") + t_w = TopWord.new(name_b) t_w.check_all_words t_w.top_words_counter t_w.res(top_words) @@ -58,9 +60,4 @@ end end end.parse! -# rubocop:enable Lint/UnusedBlockArgument -# rubocop:enable Metrics/LineLength # rubocop:enable Metrics/BlockLength -# rubocop:enable Style/UnneededInterpolation -# rubocop:enable Style/ConditionalAssignment -# rubocop:enable Layout/TrailingWhitespace diff --git a/2355/2/top_bad_words.rb b/2355/2/top_bad_words.rb index c8614a70a..a266761b4 100644 --- a/2355/2/top_bad_words.rb +++ b/2355/2/top_bad_words.rb @@ -1,10 +1,8 @@ require './find_obscenity.rb' -# rubocop:disable Lint/Syntax # This class is needed for first level of Task 2 -# This class smells of :reek:Attribute class TopBad - attr_accessor :battlers, :top_obscenity + attr_reader :battlers, :top_obscenity def initialize @battlers = [] @@ -16,33 +14,32 @@ def set_battlers_names file.each { |line| @battlers << line.delete("\n") } end + # This method smells of :reek:UtilityFunction + def dir_count(battler) + Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) } + end + # This method smells of :reek:DuplicateMethodCall def set_top_obscenity - 0.upto(battlers.size - 1) do |index - check = FindObscenity.new(@battlers[indexx]) + 0.upto(battlers.size - 1) do |index| + check = FindObscenity.new(@battlers[index]) check.check_battles_for_obscenity - top_obscenity["#{@battlers[indexx]}"] = check.obscenity.size + top_obscenity[@battlers[index]] = check.obscenity.size end end # This method smells of :reek:DuplicateMethodCall # This method smells of :reek:NestedIterators - # This method smells of :reek:TooManyStatements - # This method smells of :reek:UtilityFunction - def average_words_in_round(battler) - counter = 0 - 1.upto(Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }) do |indexx| - file = File.new("./rap-battles/#{battler}/#{indexx}") - file.each do |line| - mass = line.split - mass.each { counter += 1 } + def average_words_in_round(battler, counter = 0) + 1.upto(dir_count(battler)) do |text| + File.new("./rap-battles/#{battler}/#{text}").each do |line| + line.split.each { counter += 1 } end end - counter / ((Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }) * 3) + counter / (dir_count(battler) * 3) end def average_bad_words_in_battle(battler) - top_obscenity["#{battler}"] / (Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }) - end + top_obscenity[battler] / dir_count(battler) + end end -# rubocop:enable Lint/Syntax diff --git a/2355/2/top_words.rb b/2355/2/top_words.rb index b3f4114b3..6d1595e4c 100644 --- a/2355/2/top_words.rb +++ b/2355/2/top_words.rb @@ -1,14 +1,6 @@ -# rubocop:disable Metrics/AbcSize -# rubocop:disable Metrics/MethodLength -# rubocop:disable Style/UnneededInterpolation -# rubocop:disable Style/IfUnlessModifier -# rubocop:disable Style/NegatedWhile -# rubocop:disable Lint/UnusedBlockArgument -# rubocop:disable Lint/ImplicitStringConcatenation # This class is needed to find most popular words from text files -# This class smells of :reek:Attribute class TopWord - attr_accessor :battler + attr_reader :battler def initialize(battler) @battler = battler @@ -16,16 +8,16 @@ def initialize(battler) @top_words = {} end + def dir_count + Dir[File.join("./rap-battles/#{@battler}/", '**', '*')].count { |file| File.file?(file) } + end + # This method smells of :reek:NestedIterators - # This method smells of :reek:TooManyStatements - # This method smells of :reek:UncommunicativeVariableName def check_all_words - 1.upto(Dir[File.join("./rap-battles/#{@battler}/", '**', '*')].count { |file| File.file?(file) }) do |i| - file = File.new("./rap-battles/#{@battler}/#{i}") - file.each do |line| - mass = line.split - mass.each do |word| - word = word.delete '.' ',' '?»' '"' '!' ';' + 1.upto(dir_count) do |text| + File.new("./rap-battles/#{@battler}/#{text}").each do |line| + line.split.each do |word| + word = word.delete '.', ',', '?»', '"', '!', ';' @words << word end end @@ -33,39 +25,22 @@ def check_all_words end # This method smells of :reek:DuplicateMethodCall - # This method smells of :reek:NilCheck # This method smells of :reek:TooManyStatements def top_words_counter - mistakes = [] - file = File.new('./pretexts') - file.each { |line| mistakes << line.delete("\n") } - while !@words.empty? + pretexts = [] + File.new('./pretexts').each { |line| pretexts << line.delete("\n") } + while @words.any? counter = 0 - @words.each do |word| - if word == @words[0] && mistakes.index(word).nil? - counter += 1 - end - end - @top_words["#{@words[0]}"] = counter - @words.delete("#{@words[0]}") + @words.each { |word| counter += 1 if word == @words[0] && !pretexts.include?(word) } + @top_words[@words[0]] = counter + @words.delete(@words[0]) end end # This method smells of :reek:DuplicateMethodCall - # This method smells of :reek:TooManyStatements - # This method smells of :reek:UncommunicativeVariableName def res(value) - res = [] - @top_words = @top_words.sort_by { |key, val| val } + @top_words = @top_words.sort_by { |_key, val| val } @top_words = @top_words.reverse - 0.upto(value - 1) { |i| res << @top_words[i] } - res.size.times { |i| puts "#{res[i][0]}" + ' - ' + "#{res[i][1]}" + ' раз(а)' } + 0.upto(value - 1) { |index| puts @top_words[index][0] + ' - ' + @top_words[index][1].to_s + ' раз(а)' } end end -# rubocop:enable Metrics/AbcSize -# rubocop:enable Metrics/MethodLength -# rubocop:enable Style/UnneededInterpolation -# rubocop:enable Style/IfUnlessModifier -# rubocop:enable Style/NegatedWhile -# rubocop:enable Lint/UnusedBlockArgument -# rubocop:enable Lint/ImplicitStringConcatenation