-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer.rb
89 lines (76 loc) · 2.23 KB
/
analyzer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# analyzer.rb
def analyze file_name
file = File.readlines(file_name)
string = file.join
line_count = file.size
character_count = string.size
character_count_without_whitespace = string.split(/./).size
word_count = string.split.size
sentence_count = string.split(/\.|\?|!/).size
paragraph_count = string.split(/\r\n\r\n/).size
words_per_sentence = word_count.to_f / sentence_count
sentence_per_paragraph = sentence_count.to_f / paragraph_count
puts 'analysis results:'
puts "Character count: #{character_count}"
puts "Character count (excluding spaces): #{character_count_without_whitespace}"
puts "Line count: #{line_count}"
puts "Word count: #{word_count}"
puts "Sentence count: #{sentence_count}"
puts "Paragraph count: #{paragraph_count}"
puts "Average number of words per sentence: #{"%.2f" % words_per_sentence}"
puts "Average number of sentences per paragraph : #{"%.2f" % sentence_per_paragraph}"
end
analyze 'text.txt'
# String Array
# very oop solution. clean and versitile...
# class FileAnalyzer
# def initialize file
# @content = File.read(file)
# end
#
# def lines
# @content.split(/\n/).length
# end
#
# def characters
# @content.length
# end
#
# def characters_without_whitespaces
# @content.gsub(/ /,'').length
# end
#
# def words
# @content.split.length
# end
#
# def sentences
# @content.split(/[.!?]/).length
# end
#
# def paragraphs
# @content.split(/\n\n/).length
# end
#
# def words_per_sentence
# words / sentences
# end
#
# def sentences_per_paragraph
# sentences / paragraphs
# end
# end
#
# def display_file_stats_for file_info
# puts "Total lines: #{file_info.lines}"
# puts "Total characters: #{file_info.characters}"
# puts "Total characters without white space: #{file_info.characters_without_whitespaces}"
# puts "Total words: #{file_info.words}"
# puts "Total sentences: #{file_info.sentences}"
# puts "Total paragraphs: #{file_info.paragraphs}"
# puts "Avg words per sentences: #{file_info.words_per_sentence}"
# puts "Avg sentences per paragraph: #{file_info.sentences_per_paragraph}"
# end
#
# analyzer = FileAnalyzer.new 'text.txt'
# display_file_stats_for analyzer