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

Commit

Permalink
Enable rubocop and rework project's structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksei12942 committed Jul 22, 2018
1 parent ac98a92 commit 6ee0321
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 106 deletions.
8 changes: 1 addition & 7 deletions 2355/2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,12 @@ ruby main.rb --top-bad-words=6
+--------------------------+-------------------------+---------------------+----------------------+
| Леха Медь | 276 нецензурных слов(а) | 92 слов(а) на баттл | 626 слов(а) в раунде |
+--------------------------+-------------------------+---------------------+----------------------+
| Rickey F | 268 нецензурных слов(а) | 44 слов(а) на баттл | 541 слов(а) в раунде |
+--------------------------+-------------------------+---------------------+----------------------+
| Хип-хоп одинокой старухи | 237 нецензурных слов(а) | 79 слов(а) на баттл | 620 слов(а) в раунде |
+--------------------------+-------------------------+---------------------+----------------------+
| Букер | 162 нецензурных слов(а) | 40 слов(а) на баттл | 322 слов(а) в раунде |
+--------------------------+-------------------------+---------------------+----------------------+
| Эмио Афишл | 155 нецензурных слов(а) | 77 слов(а) на баттл | 574 слов(а) в раунде |
+--------------------------+-------------------------+---------------------+----------------------+
| Mytee Dee | 129 нецензурных слов(а) | 25 слов(а) на баттл | 225 слов(а) в раунде |
+--------------------------+-------------------------+---------------------+----------------------+
|
```

### Команды __--top-words=__ и __--name=__
Expand Down
19 changes: 19 additions & 0 deletions 2355/2/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require './parser.rb'
require 'optparse'

OptionParser.new do |opts|
opts.on('--top-bad-words=') do |bad|
task = Parser.new
task.bad_words = bad
task.top_bad_words_values
task.print_table
end
opts.on('--top-words=') do |most|
task = Parser.new
task.top_words = most
opts.on('--name=') do |battler_name|
task.name_value(battler_name)
task.name_check
end
end
end.parse!
7 changes: 6 additions & 1 deletion 2355/2/find_obscenity.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'russian_obscenity'

# This class is needed to find and collect all obscenity from text files
class FindObscenity
class Obscenity
attr_reader :obscenity

def initialize(battler)
Expand All @@ -18,6 +18,11 @@ def initialize_mistakes
File.new('./mistakes').each { |line| @mistakes << line.delete("\n") }
end

# In methods first_check and check_rus_obs I use disabling reek:NestedIterators,
# because I believe that this method of implementing the search for specific words
# in a large text is the most acceptable.
# I would have each block do-end to make a separate function and call them all one by one,
# but in my opinion, this will lower the readability of the code.
# This method smells of :reek:NestedIterators
def first_check
1.upto(dir_count) do |text|
Expand Down
63 changes: 0 additions & 63 deletions 2355/2/main.rb

This file was deleted.

76 changes: 76 additions & 0 deletions 2355/2/parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
require './top_bad_words.rb'
require './top_words.rb'
require 'terminal-table'

# This class is needed to parsing console params
class Parser
attr_reader :bad_words, :top_words, :name

def initialize
@top_bad_words = []
@top = TopBad.new
end

def bad_words=(bad)
@bad_words = !bad.empty? ? bad.to_i : 1
end

def top_bad_words_values
@top.battlers_names
@top.top_obscenity_check
@top_bad_words = (@top.top_obscenity.sort_by { |_key, val| val }).reverse
end

def top_words=(most)
@top_words = !most.empty? ? most.to_i : 30
end

def name_value(battler_name)
@name = battler_name
end

def print_top_words
t_w = TopWord.new(@name)
t_w.check_all_words
t_w.pretexts_value
t_w.top_words_counter
t_w.res(top_words)
end

def raper
@top.battlers_names
@top.battlers
end

def name_check
if @name.empty?
puts 'Choose your destiny!'
elsif !raper.include?(@name)
puts 'Я не знаю рэпера ' + @name + ', но знаю таких: '
raper.each { |battler| puts battler }
else
print_top_words
end
end

def words_per_battle(elem)
@top.average_bad_words_in_battle(elem).to_s
end

def words_per_round(elem)
@top.average_words_in_round(elem).to_s
end

def print_table
table = Terminal::Table.new do |tb|
@bad_words.times do |index|
value = @top_bad_words[index]
elem = value[0]
tb << [elem, value[1].to_s + ' нецензурных слов(а)',
words_per_battle(elem) + ' слов(а) на баттл',
words_per_round(elem) + ' слов(а) в раунде']
end
end
puts table
end
end
38 changes: 22 additions & 16 deletions 2355/2/top_bad_words.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,45 @@ class TopBad
def initialize
@battlers = []
@top_obscenity = {}
@dir_count = 0
@words_count = 0
end

def set_battlers_names
def battlers_names
file = File.new('./battlers')
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) }
@dir_count = Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }
end

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

# This method smells of :reek:DuplicateMethodCall
# This method smells of :reek:NestedIterators
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
def words_in_text(battler, text)
File.new("./rap-battles/#{battler}/#{text}").each do |line|
line.split.each { @words_count += 1 }
end
counter / (dir_count(battler) * 3)
end

def average_words_in_round(battler)
@words_count = 0
dir_count(battler)
1.upto(@dir_count) do |text|
words_in_text(battler, text)
end
@words_count / (@dir_count * 3)
end

def average_bad_words_in_battle(battler)
top_obscenity[battler] / dir_count(battler)
dir_count(battler)
top_obscenity[battler] / @dir_count
end
end
47 changes: 28 additions & 19 deletions 2355/2/top_words.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class TopWord

def initialize(battler)
@battler = battler
@pretexts = []
@words = []
@top_words = {}
end
Expand All @@ -12,35 +13,43 @@ def dir_count
Dir[File.join("./rap-battles/#{@battler}/", '**', '*')].count { |file| File.file?(file) }
end

# This method smells of :reek:NestedIterators
def clear_words(line)
line.split.each do |word|
word = word.delete '.', ',', '?»', '&quot', '!', ';'
@words << word
end
end

def check_words_in_text(text)
File.new("./rap-battles/#{@battler}/#{text}").each do |line|
clear_words(line)
end
end

def check_all_words
1.upto(dir_count) do |text|
File.new("./rap-battles/#{@battler}/#{text}").each do |line|
line.split.each do |word|
word = word.delete '.', ',', '?»', '&quot', '!', ';'
@words << word
end
end
check_words_in_text(text)
end
end

# This method smells of :reek:DuplicateMethodCall
# This method smells of :reek:TooManyStatements
def pretexts_value
File.new('./pretexts').each { |word| @pretexts << word.delete("\n") }
end

def top_words_counter
pretexts = []
File.new('./pretexts').each { |line| pretexts << line.delete("\n") }
while @words.any?
counter = 0
@words.each { |word| counter += 1 if word == @words[0] && !pretexts.include?(word) }
@top_words[@words[0]] = counter
@words.delete(@words[0])
check = @words.first
@top_words[check] = 0
@words.each { |word| @top_words[check] += 1 if word == check && !@pretexts.include?(word) }
@words.delete(check)
end
end

# This method smells of :reek:DuplicateMethodCall
def res(value)
@top_words = @top_words.sort_by { |_key, val| val }
@top_words = @top_words.reverse
0.upto(value - 1) { |index| puts @top_words[index][0] + ' - ' + @top_words[index][1].to_s + ' раз(а)' }
@top_words = (@top_words.sort_by { |_key, val| val }).reverse
0.upto(value - 1) do |index|
word = @top_words[index]
puts word[0] + ' - ' + word[1].to_s + ' раз(а)'
end
end
end

0 comments on commit 6ee0321

Please sign in to comment.