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

Commit

Permalink
Enable some rubocop methods and add README file
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei12942 committed Jul 16, 2018
1 parent ec9219e commit 5ac20ee
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 149 deletions.
49 changes: 49 additions & 0 deletions 2355/2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Инструкция по применению

Запустить в консоли файл __main.rb__ командой __ruby main.rb__, добавив в конце записи необходимые команды.
Список доступных команд:
* __--top-bad-words=__
* __--top-words=__
* __--name=__

### Команда __--top-bad-words=__

Использование данной команды приводит к тому, что на экране выводится топ исполнителей, в текста баттлов которых входит наибольшее количество нецензурных выражений. Принимает целочисленные агрументы и выводит столько исполнителей, сколько запросил пользователь. По умолчанию(если аргументы не были переданы) выводит только самого нецензурного исполнителя. Также в виде таблицы выводит данные о количестве поединков(баттлов), среднее количество нецензурных слов в поединке и среднее количество слов в раунде.

Пример работы программы.

```
ruby main.rb --top-bad-words=3
Гнойный | 12 батлов | 127 нецензурных слов | 10.58 слова на баттл | 232 слова в раунде |
Oxxxymiron | 7 батлов | 24 нецензурных слова | 3.42 слова на баттл | 317 слов в раунде |
Галат | 3 батла | 2 нецензурных слов | 0.66 слова на баттл | 207 слов в раунде |
```

### Команды __--top-words=__ и __--name=__

Данные команды используются вместе и их исполнение приводит к тому, что на экране выводится топ наиболее часто используемых слов заданного исполнителя.
Команда __--top-words=__ принимает целочисленные аргументы и по умолчанию имеет параметр 30, т.е. выводится топ-30 наиболее часто используемых слов.
Команда __--name=__ принимает строковые данные и является обязательной для заполнения, т.к. при отсутствии агрументов выдает ошибку. Также при отсутствии заданного исполнителя в списке баттлеров для анализа, программа выдаст список доступных исполнителей.

Пример работы программы.

```
ruby main.rb --top-words=20 --name=Толик
Рэпер Толик не известен мне. Зато мне известны:
Гнойный
Oxxxymiron
Галат
...
ruby main.rb --top-words=5 --name=Oxxymiron
Факты - 5 раз
Папочку - 2 раза
Микрофоны - 1 раз
Птички - 1 раз
Пожертвую - 1 раз
```

78 changes: 34 additions & 44 deletions 2355/2/find_obscenity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,56 @@
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
# rubocop:disable Style/IfUnlessModifier
# rubocop:disable Layout/DefEndAlignment
# rubocop:disable Layout/Tab
# rubocop:disable Layout/IndentationWidth
# rubocop:disable Layout/IndentationConsistency
# rubocop:disable Layout/CommentIndentation
# 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_accessor :obscenity

def initialize(battler)
@battler = battler
end
def initialize(battler)
@battler = battler
end

def initialize_mistakes
@mistakes = []
file = File.new('./mistakes')
file.each { |line| @mistakes << line.delete("\n") }
end
def initialize_mistakes
@mistakes = []
file = File.new('./mistakes')
file.each { |line| @mistakes << line.delete("\n") }
end

# This method smells of :reek:DuplicateMethodCall
# 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 '.' ',' '?»' '&quot' '!' ';'
@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
rus_obs.each { |word| @obscenity << word }
end
end
end
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 '.' ',' '?»' '&quot' '!' ';'
@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
rus_obs.each { |word| @obscenity << word }
end
end
end
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
# rubocop:enable Style/IfUnlessModifier
# rubocop:enable Layout/DefEndAlignment
# rubocop:enable Layout/Tab
# rubocop:enable Layout/IndentationWidth
# rubocop:enable Layout/IndentationConsistency
# rubocop:enable Layout/CommentIndentation
# rubocop:enable Lint/ImplicitStringConcatenation
# rubocop:enable Performance/RedundantMatch
55 changes: 23 additions & 32 deletions 2355/2/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,41 @@
# rubocop:disable Metrics/BlockLength
# rubocop:disable Style/UnneededInterpolation
# rubocop:disable Style/ConditionalAssignment
# rubocop:disable Layout/IndentationConsistency
# rubocop:disable Layout/IndentationWidth
# rubocop:disable Layout/ElseAlignment
# rubocop:disable Layout/Tab
# rubocop:disable Layout/TrailingWhitespace

OptionParser.new do |opts|
opts.on('--top-bad-words=') do |bad|
if !bad.empty?
bad_words = bad.to_i
else
bad_words = 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_bad_words.reverse
if !bad.empty?
bad_words = bad.to_i
else
bad_words = 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_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 << :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]}")}" + ' слов(а) в раунде']
(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 << :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]}")}" + ' слов(а) в раунде']
end
puts table
end

opts.on('--top-words=') do |top_w|
if !top_w.empty?
if !top_w.empty?
top_words = top_w.to_i
else
top_words = 30
end
else
top_words = 30
end
opts.on('--name=') do |b_name|
if b_name.empty?
puts'Choose your destiny!'
else
name_b = b_name
puts 'Choose your destiny!'
else
name_b = b_name
top = TopBad.new
top.set_battlers_names
if top.battlers.index("#{name_b}").nil?
Expand All @@ -68,8 +63,4 @@
# rubocop:enable Metrics/BlockLength
# rubocop:enable Style/UnneededInterpolation
# rubocop:enable Style/ConditionalAssignment
# rubocop:enable Layout/IndentationConsistency
# rubocop:enable Layout/IndentationWidth
# rubocop:enable Layout/ElseAlignment
# rubocop:enable Layout/Tab
# rubocop:enable Layout/TrailingWhitespace
78 changes: 32 additions & 46 deletions 2355/2/top_bad_words.rb
Original file line number Diff line number Diff line change
@@ -1,62 +1,48 @@
require './find_obscenity.rb'

# rubocop:disable Metrics/LineLength
# rubocop:disable Metrics/AbcSize
# rubocop:disable Layout/CommentIndentation
# rubocop:disable Layout/IndentationWidth
# rubocop:disable Layout/DefEndAlignment
# rubocop:disable Layout/IndentationConsistency
# rubocop:disable Layout/Tab
# rubocop:disable Style/UnneededInterpolation
# 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_accessor :battlers, :top_obscenity

def initialize
@battlers = []
@top_obscenity = {}
end
def initialize
@battlers = []
@top_obscenity = {}
end

def set_battlers_names
file = File.new('./battlers')
file.each { |line| @battlers << line.delete("\n") }
end
def set_battlers_names
file = File.new('./battlers')
file.each { |line| @battlers << line.delete("\n") }
end

# This method smells of :reek:DuplicateMethodCall
# This method smells of :reek:DuplicateMethodCall
def set_top_obscenity
0.upto(battlers.size - 1) do |indexx|
check = FindObscenity.new(@battlers[indexx])
check.check_battles_for_obscenity
top_obscenity["#{@battlers[indexx]}"] = check.obscenity.size
end
end
0.upto(battlers.size - 1) do |index
check = FindObscenity.new(@battlers[indexx])
check.check_battles_for_obscenity
top_obscenity["#{@battlers[indexx]}"] = check.obscenity.size
end
end

# This method smells of :reek:DuplicateMethodCall
# 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 }
end
end
counter / ((Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }) * 3)
end
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 }
end
end
counter / ((Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }) * 3)
end

def average_bad_words_in_battle(battler)
top_obscenity["#{battler}"] / (Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) })
end
def average_bad_words_in_battle(battler)
top_obscenity["#{battler}"] / (Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) })
end
end
# rubocop:enable Metrics/LineLength
# rubocop:enable Metrics/AbcSize
# rubocop:enable Layout/CommentIndentation
# rubocop:enable Layout/IndentationWidth
# rubocop:enable Layout/DefEndAlignment
# rubocop:enable Layout/IndentationConsistency
# rubocop:enable Layout/Tab
# rubocop:enable Style/UnneededInterpolation
# rubocop:enable Lint/Syntax
44 changes: 17 additions & 27 deletions 2355/2/top_words.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,32 @@
# rubocop:disable Style/NegatedWhile
# rubocop:disable Lint/UnusedBlockArgument
# rubocop:disable Lint/ImplicitStringConcatenation
# rubocop:disable Layout/IndentationConsistency
# rubocop:disable Layout/IndentationWidth
# rubocop:disable Layout/EndAlignment
# rubocop:disable Layout/DefEndAlignment
# rubocop:disable Layout/Tab
# This class is needed to find most popular words from text files
# This class smells of :reek:Attribute
class TopWord
attr_accessor :battler
attr_accessor :battler

def initialize(battler)
@battler = battler
@words = []
def initialize(battler)
@battler = battler
@words = []
@top_words = {}
end
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 '.' ',' '?»' '&quot' '!' ';'
@words << word
end
end
end
end
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 '.' ',' '?»' '&quot' '!' ';'
@words << word
end
end
end
end

# This method smells of :reek:DuplicateMethodCall
# This method smells of :reek:NilCheck
Expand All @@ -53,7 +48,7 @@ def top_words_counter
end
@top_words["#{@words[0]}"] = counter
@words.delete("#{@words[0]}")
end
end
end

# This method smells of :reek:DuplicateMethodCall
Expand All @@ -74,8 +69,3 @@ def res(value)
# rubocop:enable Style/NegatedWhile
# rubocop:enable Lint/UnusedBlockArgument
# rubocop:enable Lint/ImplicitStringConcatenation
# rubocop:enable Layout/IndentationConsistency
# rubocop:enable Layout/IndentationWidth
# rubocop:enable Layout/EndAlignment
# rubocop:enable Layout/DefEndAlignment
# rubocop:enable Layout/Tab

0 comments on commit 5ac20ee

Please sign in to comment.