Skip to content
This repository has been archived by the owner on Jun 27, 2019. It is now read-only.

2358-2 #266

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions 2358/2/constants.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# :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|Галат/,
'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
135 changes: 135 additions & 0 deletions 2358/2/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
require 'pry'
require 'russian_obscenity'
require 'terminal-table'
require_relative 'constants'

# :reek:TooManyInstanceVariables
class TextHandler
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
if @name.nil?
show_table
else
show_favorite_word
end
end
# rubocop:enable Layout/EmptyLineBetweenDefs

private

# :reek:TooManyStatements
# :reek:NilCheck
# :reek:NestedIterators
def calculation

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/AbcSize: Assignment Branch Condition size for calculation is too high. [43.65/15]
Metrics/CyclomaticComplexity: Cyclomatic complexity for calculation is too high. [8/6]
Metrics/MethodLength: Method has too many lines. [36/10]
Metrics/PerceivedComplexity: Perceived complexity for calculation is too high. [12/7]

Dir.new(TEXTS_PATH).each do |file|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/BlockLength: Block has too many lines. [30/25]

next if file =~ /^\./
rapper_name = file.split(/ против | VS | vs /)[0].split.join(' ')

name_from_hash = RAPPER_NAMES.find { |_, reg| rapper_name =~ reg }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NestedIterators: TextHandler#calculation contains iterators nested 2 deep. More info.


next if name_from_hash.nil?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NilCheck: TextHandler#calculation performs a nil-check. More info.

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 @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
@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
# :reek:TooManyStatements

# rubocop:disable Metrics/MethodLength
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: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

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