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

Commit b0fa517

Browse files
committed
Enable some rubocop methods and add README file
1 parent e3957b9 commit b0fa517

File tree

5 files changed

+155
-149
lines changed

5 files changed

+155
-149
lines changed

2355/2/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Инструкция по применению
2+
3+
Запустить в консоли файл __main.rb__ командой __ruby main.rb__, добавив в конце записи необходимые команды.
4+
Список доступных команд:
5+
* __--top-bad-words=__
6+
* __--top-words=__
7+
* __--name=__
8+
9+
### Команда __--top-bad-words=__
10+
11+
Использование данной команды приводит к тому, что на экране выводится топ исполнителей, в текста баттлов которых входит наибольшее количество нецензурных выражений. Принимает целочисленные агрументы и выводит столько исполнителей, сколько запросил пользователь. По умолчанию(если аргументы не были переданы) выводит только самого нецензурного исполнителя. Также в виде таблицы выводит данные о количестве поединков(баттлов), среднее количество нецензурных слов в поединке и среднее количество слов в раунде.
12+
13+
Пример работы программы.
14+
15+
```
16+
ruby main.rb --top-bad-words=3
17+
18+
Гнойный | 12 батлов | 127 нецензурных слов | 10.58 слова на баттл | 232 слова в раунде |
19+
Oxxxymiron | 7 батлов | 24 нецензурных слова | 3.42 слова на баттл | 317 слов в раунде |
20+
Галат | 3 батла | 2 нецензурных слов | 0.66 слова на баттл | 207 слов в раунде |
21+
```
22+
23+
### Команды __--top-words=__ и __--name=__
24+
25+
Данные команды используются вместе и их исполнение приводит к тому, что на экране выводится топ наиболее часто используемых слов заданного исполнителя.
26+
Команда __--top-words=__ принимает целочисленные аргументы и по умолчанию имеет параметр 30, т.е. выводится топ-30 наиболее часто используемых слов.
27+
Команда __--name=__ принимает строковые данные и является обязательной для заполнения, т.к. при отсутствии агрументов выдает ошибку. Также при отсутствии заданного исполнителя в списке баттлеров для анализа, программа выдаст список доступных исполнителей.
28+
29+
Пример работы программы.
30+
31+
```
32+
33+
ruby main.rb --top-words=20 --name=Толик
34+
35+
Рэпер Толик не известен мне. Зато мне известны:
36+
Гнойный
37+
Oxxxymiron
38+
Галат
39+
...
40+
41+
ruby main.rb --top-words=5 --name=Oxxymiron
42+
43+
Факты - 5 раз
44+
Папочку - 2 раза
45+
Микрофоны - 1 раз
46+
Птички - 1 раз
47+
Пожертвую - 1 раз
48+
```
49+

2355/2/find_obscenity.rb

+34-44
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,56 @@
33
# rubocop:disable Metrics/MethodLength
44
# rubocop:disable Metrics/AbcSize
55
# rubocop:disable Style/IfUnlessModifier
6-
# rubocop:disable Layout/DefEndAlignment
7-
# rubocop:disable Layout/Tab
8-
# rubocop:disable Layout/IndentationWidth
9-
# rubocop:disable Layout/IndentationConsistency
10-
# rubocop:disable Layout/CommentIndentation
116
# rubocop:disable Lint/ImplicitStringConcatenation
127
# rubocop:disable Performance/RedundantMatch
138
# This class is needed to find and collect all obscenity from text files
149
# This class smells of :reek:Attribute
1510
# This class smells of :reek:InstanceVariableAssumption
1611
class FindObscenity
17-
attr_accessor :obscenity
12+
attr_accessor :obscenity
1813

19-
def initialize(battler)
20-
@battler = battler
21-
end
14+
def initialize(battler)
15+
@battler = battler
16+
end
2217

23-
def initialize_mistakes
24-
@mistakes = []
25-
file = File.new('./mistakes')
26-
file.each { |line| @mistakes << line.delete("\n") }
27-
end
18+
def initialize_mistakes
19+
@mistakes = []
20+
file = File.new('./mistakes')
21+
file.each { |line| @mistakes << line.delete("\n") }
22+
end
2823

29-
# This method smells of :reek:DuplicateMethodCall
24+
# This method smells of :reek:DuplicateMethodCall
3025
# This method smells of :reek:NestedIterators
3126
# This method smells of :reek:TooManyStatements
3227
# This method smells of :reek:UncommunicativeVariableName
3328
def check_battles_for_obscenity
34-
initialize_mistakes
35-
@obscenity = []
36-
1.upto(Dir[File.join("./rap-battles/#{@battler}/", '**', '*')].count { |file| File.file?(file) }) do |i|
37-
file = File.new("./rap-battles/#{@battler}/#{i}")
38-
file.each do |line|
39-
mass = line.split
40-
mass.each do |word|
41-
if word.match(/.*\*.*[А-Яа-я.,]$/)
42-
word = word.delete '.' ',' '?»' '&quot' '!' ';'
43-
@obscenity << word
44-
end
45-
end
46-
rus_obs = RussianObscenity.find(line)
47-
rus_obs.each do |word|
48-
@mistakes.each do |mis|
49-
if mis.casecmp(word)
50-
rus_obs.delete(word)
51-
end
52-
end
53-
end
54-
rus_obs.each { |word| @obscenity << word }
55-
end
56-
end
57-
end
29+
initialize_mistakes
30+
@obscenity = []
31+
1.upto(Dir[File.join("./rap-battles/#{@battler}/", '**', '*')].count { |file| File.file?(file) }) do |i|
32+
file = File.new("./rap-battles/#{@battler}/#{i}")
33+
file.each do |line|
34+
mass = line.split
35+
mass.each do |word|
36+
if word.match(/.*\*.*[А-Яа-я.,]$/)
37+
word = word.delete '.' ',' '?»' '&quot' '!' ';'
38+
@obscenity << word
39+
end
40+
end
41+
rus_obs = RussianObscenity.find(line)
42+
rus_obs.each do |word|
43+
@mistakes.each do |mis|
44+
if mis.casecmp(word)
45+
rus_obs.delete(word)
46+
end
47+
end
48+
end
49+
rus_obs.each { |word| @obscenity << word }
50+
end
51+
end
52+
end
5853
end
5954
# rubocop:enable Metrics/MethodLength
6055
# rubocop:enable Metrics/AbcSize
6156
# rubocop:enable Style/IfUnlessModifier
62-
# rubocop:enable Layout/DefEndAlignment
63-
# rubocop:enable Layout/Tab
64-
# rubocop:enable Layout/IndentationWidth
65-
# rubocop:enable Layout/IndentationConsistency
66-
# rubocop:enable Layout/CommentIndentation
6757
# rubocop:enable Lint/ImplicitStringConcatenation
6858
# rubocop:enable Performance/RedundantMatch

2355/2/main.rb

+23-32
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,41 @@
88
# rubocop:disable Metrics/BlockLength
99
# rubocop:disable Style/UnneededInterpolation
1010
# rubocop:disable Style/ConditionalAssignment
11-
# rubocop:disable Layout/IndentationConsistency
12-
# rubocop:disable Layout/IndentationWidth
13-
# rubocop:disable Layout/ElseAlignment
14-
# rubocop:disable Layout/Tab
1511
# rubocop:disable Layout/TrailingWhitespace
16-
1712
OptionParser.new do |opts|
1813
opts.on('--top-bad-words=') do |bad|
19-
if !bad.empty?
20-
bad_words = bad.to_i
21-
else
22-
bad_words = 1
23-
end
24-
top = TopBad.new
25-
top.set_battlers_names
26-
top.set_top_obscenity
27-
top_bad_words = top.top_obscenity.sort_by { |key, val| val }
28-
top_bad_words = top_bad_words.reverse
14+
if !bad.empty?
15+
bad_words = bad.to_i
16+
else
17+
bad_words = 1
18+
end
19+
top = TopBad.new
20+
top.set_battlers_names
21+
top.set_top_obscenity
22+
top_bad_words = top.top_obscenity.sort_by { |key, val| val }
23+
top_bad_words = top_bad_words.reverse
2924
table = Terminal::Table.new do |t|
30-
(bad_words - 1).times do |i|
31-
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]}")}" + ' слов(а) в раунде']
32-
t << :separator
33-
end
34-
i = bad_words - 1
35-
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]}")}" + ' слов(а) в раунде']
25+
(bad_words - 1).times do |i|
26+
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]}")}" + ' слов(а) в раунде']
27+
t << :separator
28+
end
29+
i = bad_words - 1
30+
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]}")}" + ' слов(а) в раунде']
3631
end
3732
puts table
3833
end
3934

4035
opts.on('--top-words=') do |top_w|
41-
if !top_w.empty?
36+
if !top_w.empty?
4237
top_words = top_w.to_i
43-
else
44-
top_words = 30
45-
end
38+
else
39+
top_words = 30
40+
end
4641
opts.on('--name=') do |b_name|
4742
if b_name.empty?
48-
puts'Choose your destiny!'
49-
else
50-
name_b = b_name
43+
puts 'Choose your destiny!'
44+
else
45+
name_b = b_name
5146
top = TopBad.new
5247
top.set_battlers_names
5348
if top.battlers.index("#{name_b}").nil?
@@ -68,8 +63,4 @@
6863
# rubocop:enable Metrics/BlockLength
6964
# rubocop:enable Style/UnneededInterpolation
7065
# rubocop:enable Style/ConditionalAssignment
71-
# rubocop:enable Layout/IndentationConsistency
72-
# rubocop:enable Layout/IndentationWidth
73-
# rubocop:enable Layout/ElseAlignment
74-
# rubocop:enable Layout/Tab
7566
# rubocop:enable Layout/TrailingWhitespace

2355/2/top_bad_words.rb

+32-46
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,48 @@
11
require './find_obscenity.rb'
22

3-
# rubocop:disable Metrics/LineLength
4-
# rubocop:disable Metrics/AbcSize
5-
# rubocop:disable Layout/CommentIndentation
6-
# rubocop:disable Layout/IndentationWidth
7-
# rubocop:disable Layout/DefEndAlignment
8-
# rubocop:disable Layout/IndentationConsistency
9-
# rubocop:disable Layout/Tab
10-
# rubocop:disable Style/UnneededInterpolation
3+
# rubocop:disable Lint/Syntax
114
# This class is needed for first level of Task 2
125
# This class smells of :reek:Attribute
136
class TopBad
14-
attr_accessor :battlers, :top_obscenity
7+
attr_accessor :battlers, :top_obscenity
158

16-
def initialize
17-
@battlers = []
18-
@top_obscenity = {}
19-
end
9+
def initialize
10+
@battlers = []
11+
@top_obscenity = {}
12+
end
2013

21-
def set_battlers_names
22-
file = File.new('./battlers')
23-
file.each { |line| @battlers << line.delete("\n") }
24-
end
14+
def set_battlers_names
15+
file = File.new('./battlers')
16+
file.each { |line| @battlers << line.delete("\n") }
17+
end
2518

26-
# This method smells of :reek:DuplicateMethodCall
19+
# This method smells of :reek:DuplicateMethodCall
2720
def set_top_obscenity
28-
0.upto(battlers.size - 1) do |indexx|
29-
check = FindObscenity.new(@battlers[indexx])
30-
check.check_battles_for_obscenity
31-
top_obscenity["#{@battlers[indexx]}"] = check.obscenity.size
32-
end
33-
end
21+
0.upto(battlers.size - 1) do |index
22+
check = FindObscenity.new(@battlers[indexx])
23+
check.check_battles_for_obscenity
24+
top_obscenity["#{@battlers[indexx]}"] = check.obscenity.size
25+
end
26+
end
3427

35-
# This method smells of :reek:DuplicateMethodCall
28+
# This method smells of :reek:DuplicateMethodCall
3629
# This method smells of :reek:NestedIterators
3730
# This method smells of :reek:TooManyStatements
3831
# This method smells of :reek:UtilityFunction
3932
def average_words_in_round(battler)
40-
counter = 0
41-
1.upto(Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }) do |indexx|
42-
file = File.new("./rap-battles/#{battler}/#{indexx}")
43-
file.each do |line|
44-
mass = line.split
45-
mass.each { counter += 1 }
46-
end
47-
end
48-
counter / ((Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }) * 3)
49-
end
33+
counter = 0
34+
1.upto(Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }) do |indexx|
35+
file = File.new("./rap-battles/#{battler}/#{indexx}")
36+
file.each do |line|
37+
mass = line.split
38+
mass.each { counter += 1 }
39+
end
40+
end
41+
counter / ((Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) }) * 3)
42+
end
5043

51-
def average_bad_words_in_battle(battler)
52-
top_obscenity["#{battler}"] / (Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) })
53-
end
44+
def average_bad_words_in_battle(battler)
45+
top_obscenity["#{battler}"] / (Dir[File.join("./rap-battles/#{battler}/", '**', '*')].count { |file| File.file?(file) })
46+
end
5447
end
55-
# rubocop:enable Metrics/LineLength
56-
# rubocop:enable Metrics/AbcSize
57-
# rubocop:enable Layout/CommentIndentation
58-
# rubocop:enable Layout/IndentationWidth
59-
# rubocop:enable Layout/DefEndAlignment
60-
# rubocop:enable Layout/IndentationConsistency
61-
# rubocop:enable Layout/Tab
62-
# rubocop:enable Style/UnneededInterpolation
48+
# rubocop:enable Lint/Syntax

2355/2/top_words.rb

+17-27
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,32 @@
55
# rubocop:disable Style/NegatedWhile
66
# rubocop:disable Lint/UnusedBlockArgument
77
# rubocop:disable Lint/ImplicitStringConcatenation
8-
# rubocop:disable Layout/IndentationConsistency
9-
# rubocop:disable Layout/IndentationWidth
10-
# rubocop:disable Layout/EndAlignment
11-
# rubocop:disable Layout/DefEndAlignment
12-
# rubocop:disable Layout/Tab
138
# This class is needed to find most popular words from text files
149
# This class smells of :reek:Attribute
1510
class TopWord
16-
attr_accessor :battler
11+
attr_accessor :battler
1712

18-
def initialize(battler)
19-
@battler = battler
20-
@words = []
13+
def initialize(battler)
14+
@battler = battler
15+
@words = []
2116
@top_words = {}
22-
end
17+
end
2318

2419
# This method smells of :reek:NestedIterators
2520
# This method smells of :reek:TooManyStatements
2621
# This method smells of :reek:UncommunicativeVariableName
2722
def check_all_words
28-
1.upto(Dir[File.join("./rap-battles/#{@battler}/", '**', '*')].count { |file| File.file?(file) }) do |i|
29-
file = File.new("./rap-battles/#{@battler}/#{i}")
30-
file.each do |line|
31-
mass = line.split
32-
mass.each do |word|
33-
word = word.delete '.' ',' '?»' '&quot' '!' ';'
34-
@words << word
35-
end
36-
end
37-
end
38-
end
23+
1.upto(Dir[File.join("./rap-battles/#{@battler}/", '**', '*')].count { |file| File.file?(file) }) do |i|
24+
file = File.new("./rap-battles/#{@battler}/#{i}")
25+
file.each do |line|
26+
mass = line.split
27+
mass.each do |word|
28+
word = word.delete '.' ',' '?»' '&quot' '!' ';'
29+
@words << word
30+
end
31+
end
32+
end
33+
end
3934

4035
# This method smells of :reek:DuplicateMethodCall
4136
# This method smells of :reek:NilCheck
@@ -53,7 +48,7 @@ def top_words_counter
5348
end
5449
@top_words["#{@words[0]}"] = counter
5550
@words.delete("#{@words[0]}")
56-
end
51+
end
5752
end
5853

5954
# This method smells of :reek:DuplicateMethodCall
@@ -74,8 +69,3 @@ def res(value)
7469
# rubocop:enable Style/NegatedWhile
7570
# rubocop:enable Lint/UnusedBlockArgument
7671
# rubocop:enable Lint/ImplicitStringConcatenation
77-
# rubocop:enable Layout/IndentationConsistency
78-
# rubocop:enable Layout/IndentationWidth
79-
# rubocop:enable Layout/EndAlignment
80-
# rubocop:enable Layout/DefEndAlignment
81-
# rubocop:enable Layout/Tab

0 commit comments

Comments
 (0)