From ae7b257a96e22e01fff1a3e1fffc1cd3d840d5c6 Mon Sep 17 00:00:00 2001 From: bigbrother13 Date: Sat, 21 Jul 2018 03:44:30 +0300 Subject: [PATCH 1/2] battle texts handler (level 1) --- 2358/2/constants.rb | 110 ++++++++++++++++++++++++++++++++++++++++++++ 2358/2/index.rb | 101 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 211 insertions(+) create mode 100644 2358/2/constants.rb create mode 100644 2358/2/index.rb diff --git a/2358/2/constants.rb b/2358/2/constants.rb new file mode 100644 index 000000000..c281ffd84 --- /dev/null +++ b/2358/2/constants.rb @@ -0,0 +1,110 @@ +TEXTS_PATH = "#{Dir.pwd}/texts".freeze +RAPPER_NAMES = { + 'Billy Milligan' => /Milligan/, + 'Galat' => /Galat|Галат/, + 'Giga 1' => /ГИГА|Giga 1/, + 'MoonStar' => /MoonStara/, + 'John Rai' => /John rai|John Rai/, + 'Johnyboy' => /Johnyboy/, + 'Noize MC' => /Noiz/, + 'Jubilee' => /Jubille|Jubilee/, + 'Oxxxymiron' => /Oxxxymiron/, + 'Tvoigreh' => /voigreh/, + 'Артем Лоик' => /Артем Лоик|Лоика/, + 'Басота' => /Басот/, + 'Букер' => /Букер/, + 'Гарри Топор' => /Гарри|Топор/, + 'Гнойный' => /Мармелад|Гнойный|Слава|КПСС/, + 'Дуня' => /Дун/, + 'ХХОС' => /ХХОС|Хип|хоп/, + 'Замай' => /Зама/, + 'Илья Мирный' => /Мирн/, + 'Витя Classic' => /Вит|Classic/, + 'Rickey F' => /ickey/, + 'Династ' => /инаст/, + 'МЦ Похоронил' => /Похоронил/, + 'Sawyer' => /Sawyer/, + 'Ярмак' => /Ярмак/, + 'Млечный' => /Млечн/, + 'Эрнесто Заткнитесь' => /Эрнесто/, + 'Abbalbisk' => /Abbalbisk/, + 'Alphavite' => /Alphavite/, + 'Mytee Dee' => /Mytee|Майти/, + 'Palmdropov' => /Palmdropov/, + 'Heavy' => /Heavy/, + 'Ruskey' => /Ruskey/, + 'ST' => /ST/, + 'Саша Скул' => /Саша Скул/, + '1917' => /1917/, + 'Lodoss' => /Lodoss/, + 'Пиэм' => /Пиэм/, + 'Tanir' => /Tanir/, + 'Meowizzy' => /Meowizzy/, + 'Obe 1 Kanobe' => /Obe 1 Kanobe/, + 'Walkie' => /Walkie/, + 'Punkteer' => /Punkteer/, + 'Микси' => /Микси/, + 'Леха Медь' => /Леха Медь/, + 'Заебатсу' => /Заебатсу/, + 'Энди Картрайт' => /Энди Картрайт/, + 'J.Makonnen' => /J.Makonnen/, + 'Chet' => /Chet/, + 'Тот самый Коля' => /Тот самый Коля/, + 'Артема Татищевского' => /Артема Татищевского/, + 'Браги' => /Браги/, + 'CZAR' => /CZAR/, + 'D.Masta' => /D.Masta/, + 'Хохла' => /Хохла/, + 'LeTai' => /LeTai/, + 'Типси Типа' => /Типси Типа/, + 'Drago' => /Drago/, + 'Lil Dik' => /Lil Dik/, + 'ЛСП' => /ЛСП/, + 'Guf' => /Guf/, + 'Bes' => /Bes/, + 'Эль' => /Эль/, + 'Брол' => /Брол/, + 'Эмио Афишл' => /Эмио Афишл/, + 'Dom1no' => /Dom1no/, + 'Sector' => /Sector/, + 'Сявы' => /Сявы/, + 'Paragrin' => /Paragrin/, + '13|47' => /13|47/, + 'Teeraps' => /Teeraps/, + 'Электромышь' => /Электромышь/, + 'СД' => /СД/, + 'Подземный Принц (Хатт)' => /одземный/, + 'Леши GS' => /Леши GS/, + 'Dizaster' => /Dizaster/, + 'Хованского' => /Хованского/, + 'Mufasah' => /Mufasah/, + 'Сын Проститутки' => /Сын Проститутки/, + 'Кореш' => /Кореш/, + 'Крип-А-Крип' => /Крип-А-Крип/, + 'Ларина' => /Ларина/, + 'Mozee Montana' => /Mozee Montana/, + 'Rokki Roketto' => /Rokki Roketto/, + 'Хайда' => /Хайда/, + 'MC No Limit' => /MC No Limit/, + "N'rage" => /N'rage/, + 'Саморез & МС MoonStar' => /Саморез & МС MoonStar/, + 'I1' => /I1/, + 'Edik_Kingsta' => /Edik_Kingsta/, + 'Miles' => /Miles/, + 'Грязный Рамирес' => /Грязный Рамирес/, + 'Джарахов' => /Джарахов/, + 'Niggarex' => /Niggarex/, + 'Just' => /Just/, + 'Yanix' => /Yanix/, + 'Марины Кацубы' => /Марины Кацубы/, + 'ATL' => /ATL/, + "Sin'а" => /Sin'а/, + 'Дима Гамбит' => /Дима Гамбит/, + 'Pitty' => /Pitty/, + "(Pra(Killa'Gramm)" => /Killa/, + 'OBJECT' => /OBJECT/, + 'Птаха' => /Птаха/, + 'Казанский' => /Казанский/, + 'Redo' => /Redo/, + 'VS94SKI' => /VS94SKI/ +}.freeze diff --git a/2358/2/index.rb b/2358/2/index.rb new file mode 100644 index 000000000..469833a7b --- /dev/null +++ b/2358/2/index.rb @@ -0,0 +1,101 @@ +require 'pry' +require 'russian_obscenity' +require 'terminal-table' +require_relative 'constants' + +# :reek:TooManyInstanceVariables +class TextHandler + def initialize(top) + @battles_count = {} + @bad_words_counter = {} + @words_per_battle_counter = {} + @words_per_round_counter = {} + @average_words_per_round_counter = {} + @top = top + end + + def show_result + calculation + show_table + end + + private + + # rubocop:disable Metrics/MethodLength + # rubocop:disable Metrics/AbcSize + # rubocop:disable Metrics/CyclomaticComplexity + # rubocop:disable Metrics/PerceivedComplexity + # :reek:TooManyStatements + # :reek:NilCheck + # :reek:NestedIterators + def calculation + Dir.new(TEXTS_PATH).each do |file| + next if file =~ /^\./ + rapper_name = file.split(/ против | VS | vs /)[0].split.join(' ') + + name_from_hash = RAPPER_NAMES.find { |_, reg| rapper_name =~ reg } + + next if name_from_hash.nil? + rapper_name = name_from_hash.first + + lines = File.readlines("#{TEXTS_PATH}/#{file}") + + words_from_text = lines.join.tr("\n", ' ').delete('.,;').split + + bad_words_count = words_from_text.select do |word| + RussianObscenity.obscene?(word) || word.include?('*') + end.count + + if @bad_words_counter[rapper_name] + @bad_words_counter[rapper_name] += bad_words_count + else + @bad_words_counter[rapper_name] = bad_words_count + end + + if @battles_count[rapper_name] + @battles_count[rapper_name] += 1 + else + @battles_count[rapper_name] = 1 + end + + if @words_per_round_counter[rapper_name] + @words_per_round_counter[rapper_name] += words_from_text.count + else + @words_per_round_counter[rapper_name] = words_from_text.count + end + end + + RAPPER_NAMES.each do |name, _| + @words_per_battle_counter[name] = (@bad_words_counter[name].to_f / @battles_count[name]).round(2) + @average_words_per_round_counter[name] = @words_per_round_counter[name] / (@battles_count[name] * 3) + end + end + + # rubocop:enable Metrics/AbcSize + # rubocop:enable Metrics/CyclomaticComplexity + # rubocop:enable Metrics/PerceivedComplexity + # :reek:TooManyStatements + def show_table + rows = [] + + @bad_words_counter.sort_by { |_, counter| counter }.reverse.first(@top).to_h.each do |name, counter| + rows << [ + name, + "#{@battles_count[name]} батлов", + "#{counter} нецензурных слов", + "#{@words_per_battle_counter[name]} cлов на баттл", + "#{@average_words_per_round_counter[name]} слов в раунде" + ] + end + + table = Terminal::Table.new( + headings: ['Рэппер', 'Батлы', 'Маты', 'Слов на батл', 'Слов в раунде'], + rows: rows + ) + puts table + end + # rubocop:enable Metrics/MethodLength +end + +top = (ARGV[0] || '--top-bad-words=100').split('=').last.to_i +TextHandler.new(top).show_result From 242169b48b66cff25eb4f3287b3415a59d0cbed8 Mon Sep 17 00:00:00 2001 From: bigbrother13 Date: Thu, 26 Jul 2018 01:33:33 +0300 Subject: [PATCH 2/2] Add second level --- 2358/2/constants.rb | 4 +++ 2358/2/index.rb | 62 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/2358/2/constants.rb b/2358/2/constants.rb index c281ffd84..fb23df928 100644 --- a/2358/2/constants.rb +++ b/2358/2/constants.rb @@ -1,4 +1,8 @@ +# :reek:NilCheck TEXTS_PATH = "#{Dir.pwd}/texts".freeze +# rubocop:disable Metrics/LineLength +SHORT_WORDS = %w[в на и я с не ни по под от у ты что Я И ТЫ как это Но - тебя но меня он А мне за все бы был кто так же его то Это тебе Ты а про вы мой Он тут Что из есть у здесь нет Как к Если Не там даже Вы для Ты ф про вы мой Он тут Что из у Как к Не там Вы где для В до всё мы или – У То *** раз Вот Мы еще со их Ну о без].freeze +# rubocop:enable Metrics/LineLength RAPPER_NAMES = { 'Billy Milligan' => /Milligan/, 'Galat' => /Galat|Галат/, diff --git a/2358/2/index.rb b/2358/2/index.rb index 469833a7b..f8d778401 100644 --- a/2358/2/index.rb +++ b/2358/2/index.rb @@ -5,26 +5,30 @@ # :reek:TooManyInstanceVariables class TextHandler - def initialize(top) + def initialize(top, name) @battles_count = {} @bad_words_counter = {} @words_per_battle_counter = {} @words_per_round_counter = {} @average_words_per_round_counter = {} + @words_counter = {} @top = top + @name = name end - + # rubocop:disable Layout/EmptyLineBetweenDefs + # :reek:NilCheck def show_result calculation - show_table + if @name.nil? + show_table + else + show_favorite_word + end end + # rubocop:enable Layout/EmptyLineBetweenDefs private - # rubocop:disable Metrics/MethodLength - # rubocop:disable Metrics/AbcSize - # rubocop:disable Metrics/CyclomaticComplexity - # rubocop:disable Metrics/PerceivedComplexity # :reek:TooManyStatements # :reek:NilCheck # :reek:NestedIterators @@ -40,12 +44,18 @@ def calculation lines = File.readlines("#{TEXTS_PATH}/#{file}") - words_from_text = lines.join.tr("\n", ' ').delete('.,;').split + words_from_text = lines.join.tr("\n", ' ').delete('.,;!?').split bad_words_count = words_from_text.select do |word| RussianObscenity.obscene?(word) || word.include?('*') end.count + if @words_counter[rapper_name] + @words_counter[rapper_name] += words_from_text + else + @words_counter[rapper_name] = words_from_text + end + if @bad_words_counter[rapper_name] @bad_words_counter[rapper_name] += bad_words_count else @@ -70,11 +80,9 @@ def calculation @average_words_per_round_counter[name] = @words_per_round_counter[name] / (@battles_count[name] * 3) end end - - # rubocop:enable Metrics/AbcSize - # rubocop:enable Metrics/CyclomaticComplexity - # rubocop:enable Metrics/PerceivedComplexity # :reek:TooManyStatements + + # rubocop:disable Metrics/MethodLength def show_table rows = [] @@ -94,8 +102,34 @@ def show_table ) puts table end + + # rubocop:disable Metrics/LineLength + # rubocop:disable Metrics/AbcSize + # :reek:TooManyStatements + def show_favorite_word + if RAPPER_NAMES[@name] + counter_array = (@words_counter[@name] - SHORT_WORDS).group_by { |name| name }.map { |name, counter| [name, counter.count] } + list_of_words = Hash[counter_array].sort_by { |_, counter| counter }.reverse + + list_of_words.first(@top).each do |pair| + puts "#{pair[0]} - #{pair[1]}\n" + end + else + puts "Рэпер #{@name} не известен мне. Зато мне известны:" + RAPPER_NAMES.keys.sample(5).each do |name| + puts "#{name}\n" + end + end + end # rubocop:enable Metrics/MethodLength + # rubocop:enable Metrics/LineLength + # rubocop:enable Metrics/AbcSize end -top = (ARGV[0] || '--top-bad-words=100').split('=').last.to_i -TextHandler.new(top).show_result +name_argument = ARGV.find { |argv| argv =~ /name/ } +top_argument = ARGV.find { |argv| argv =~ /top/ } + +top = top_argument.split('=').last.to_i if top_argument +rapper_name = name_argument.split('=').last if name_argument + +TextHandler.new(top || 30, rapper_name).show_result