@@ -9,23 +9,28 @@ class Frequency_Checker < Checker
9
9
10
10
def initialize
11
11
super
12
- @description = "Frequency Checks "
12
+ @description = "Count the frequency of characters in each position, output as either CSV or text "
13
13
14
14
@frequencies = [ ]
15
15
@position_count = [ ]
16
16
17
17
@show_empty = false
18
+ @as_csv = false
18
19
19
- @cli_params = [ [ '--freq.show_empty' , GetoptLong ::NO_ARGUMENT ] ]
20
+ @cli_params = [ [ '--freq.show_empty' , GetoptLong ::NO_ARGUMENT ] , [ '--freq.as_csv' , GetoptLong :: NO_ARGUMENT ] ]
20
21
end
21
22
22
23
def usage
23
- return "\t --freq.show_empty: show empty frequencies"
24
+ ret_str = "\t --freq.show_empty: show empty frequencies - does not apply to CSV\n "
25
+ ret_str << "\t --freq.as_csv: output in CSV format"
26
+ return ret_str
24
27
end
25
28
26
29
def parse_params opts
27
30
opts . each do |opt , arg |
28
31
case opt
32
+ when '--freq.as_csv'
33
+ @as_csv = true
29
34
when '--freq.show_empty'
30
35
@show_empty = true
31
36
end
@@ -36,20 +41,8 @@ def parse_params opts
36
41
def setup_new_frequency position
37
42
@frequencies [ position ] = { }
38
43
39
- "a" . upto ( "z" ) do |a |
40
- @frequencies [ position ] [ a ] = 0
41
- end
42
-
43
- "A" . upto ( "Z" ) do |a |
44
- @frequencies [ position ] [ a ] = 0
45
- end
46
-
47
- "0" . upto ( "9" ) do |a |
48
- @frequencies [ position ] [ a ] = 0
49
- end
50
-
51
- punct = "čćžšđČĆŽŠĐ!\" $%^&*()-=_+[]{};':@,.<>"
52
- punct . each_char do |a |
44
+ alphabet = "0123456789abcčćđdefghijklmnopqrsštuvwxyzžABCČĆDĐEFGHIJKLMNOPQRSŠTUVWXYZŽ !\" \# $%&'()*+,-./:;<=>?@[\\ ]^_`{|}~äüęé"
45
+ alphabet . each_char do |a |
53
46
@frequencies [ position ] [ a ] = 0
54
47
end
55
48
@@ -80,22 +73,56 @@ def process_word (word, extras = nil)
80
73
end
81
74
82
75
def get_results ( )
83
- ret_str = "Frequency Results\n "
84
- ret_str << "-----------------\n "
85
-
86
- position = 0
87
- @frequencies . each do |data |
88
- ret_str << "position: #{ position } - #{ @position_count [ position ] } character#{ ( @position_count [ position ] >1 ) ?'s' :'' } \n "
89
- @frequencies [ position ] . each_pair do |key , count |
90
- if ( @show_empty or count != 0 )
91
- ret_str << "#{ key } : #{ count } (#{ ( count . to_f /@position_count [ position ] ) * 100 } %) "
76
+ if @as_csv then
77
+ if @frequencies . count == 0 then
78
+ ret_str = "No data collected"
79
+ else
80
+ ret_str = ""
81
+
82
+ header = '"Position","' + @frequencies [ 0 ] . keys . join ( '","' ) + '"'
83
+ header . gsub! /"""/ , '"<quote>"'
84
+ ret_str << header + "\n "
85
+ ret_str << "Count\n "
86
+
87
+ position = 0
88
+ @frequencies . each do |data |
89
+ ret_str << position . to_s + "," + data . values . join ( ',' )
90
+ ret_str << "\n "
91
+ position += 1
92
+ end
93
+ ret_str << "\n \n \n "
94
+ ret_str << "Percentage\n "
95
+
96
+ position = 0
97
+ @frequencies . each do |data |
98
+ ret_str << position . to_s + ","
99
+ @frequencies [ position ] . each_pair do |key , count |
100
+ ret_str << ( ( count . to_f /@position_count [ position ] ) * 100 ) . round ( 2 ) . to_s + ","
101
+ end
102
+ ret_str << "\n "
103
+ position += 1
92
104
end
93
105
end
94
- # ret_str << @frequencies[position].inspect
106
+ else
107
+ ret_str = "Frequency Results\n "
108
+ ret_str << "-----------------\n "
109
+
110
+ position = 0
111
+ @frequencies . each do |data |
112
+ ret_str << "position: #{ position } - #{ @position_count [ position ] } character#{ ( @position_count [ position ] >1 ) ?'s' :'' } \n "
113
+ @frequencies [ position ] . each_pair do |key , count |
114
+ if ( @show_empty or count != 0 )
115
+ ret_str << "#{ key } : #{ count } (#{ ( count . to_f /@position_count [ position ] ) * 100 } %) "
116
+ end
117
+ end
118
+ # ret_str << @frequencies[position].inspect
119
+ ret_str << "\n "
120
+ position += 1
121
+ end
95
122
ret_str << "\n "
96
- position += 1
97
123
end
98
- ret_str << "\n "
124
+
125
+ return ret_str
99
126
100
127
return ret_str
101
128
end
0 commit comments