Skip to content

Commit c981106

Browse files
committed
save frequency data as csv
1 parent 716fb8c commit c981106

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

checkers_available/frequency.rb

+56-29
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,28 @@ class Frequency_Checker < Checker
99

1010
def initialize
1111
super
12-
@description = "Frequency Checks"
12+
@description = "Count the frequency of characters in each position, output as either CSV or text"
1313

1414
@frequencies = []
1515
@position_count = []
1616

1717
@show_empty = false
18+
@as_csv = false
1819

19-
@cli_params = [['--freq.show_empty', GetoptLong::NO_ARGUMENT]]
20+
@cli_params = [['--freq.show_empty', GetoptLong::NO_ARGUMENT], ['--freq.as_csv', GetoptLong::NO_ARGUMENT]]
2021
end
2122

2223
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
2427
end
2528

2629
def parse_params opts
2730
opts.each do |opt, arg|
2831
case opt
32+
when '--freq.as_csv'
33+
@as_csv = true
2934
when '--freq.show_empty'
3035
@show_empty = true
3136
end
@@ -36,20 +41,8 @@ def parse_params opts
3641
def setup_new_frequency position
3742
@frequencies[position] = {}
3843

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|
5346
@frequencies[position][a] = 0
5447
end
5548

@@ -80,22 +73,56 @@ def process_word (word, extras = nil)
8073
end
8174

8275
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
92104
end
93105
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
95122
ret_str << "\n"
96-
position += 1
97123
end
98-
ret_str << "\n"
124+
125+
return ret_str
99126

100127
return ret_str
101128
end

0 commit comments

Comments
 (0)