/
date_checker.rb
206 lines (178 loc) · 5.06 KB
/
date_checker.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
register_checker("Date_Checker")
class Date_Checker < Checker
def initialize
super
@years = {}
1975.upto(2020) do |year|
@years[year] = 0
end
@days_ab = {'mon' => 0, 'tues' => 0, 'wed' => 0, 'thurs' => 0, 'fri' => 0, 'sat' => 0, 'sun' => 0}
@months_ab = {"jan" => 0, "feb" => 0, "mar" => 0, "apr" => 0, "may" => 0, "jun" => 0, "jul" => 0, "aug" => 0, "sept" => 0, "oct" => 0, "nov" => 0, "dec" => 0}
@days = {'monday' => 0, 'tuesday' => 0, 'wednesday' => 0, 'thursday' => 0, 'friday' => 0, 'saturday' => 0, 'sunday' => 0}
@months = {"january" => 0, "february" => 0, "march" => 0, "april" => 0, "may" => 0, "june" => 0, "july" => 0, "august" => 0, "september" => 0, "october" => 0, "november" => 0, "december" => 0}
@description = "Days, months and years"
end
def process_word (word, extras = nil)
@years.each_pair do |year, count|
if /#{year}/.match word
@years[year] += 1
end
end
@days_ab.each_pair do |day, count|
if /#{day}/i.match word
@days_ab[day] += 1
end
end
@months_ab.each_pair do |month, count|
if /#{month}/i.match word
@months_ab[month] += 1
end
end
@days.each_pair do |day, count|
if /#{day}/i.match word
@days[day] += 1
end
end
@months.each_pair do |month, count|
if /#{month}/i.match word
@months[month] += 1
end
end
@total_words_processed += 1
end
def freq_sort(data)
count_ordered = []
data.each_pair do |val, count|
count_ordered << [val, count] unless count == 0
end
freqs_sorted = count_ordered.sort do |x,y|
(x[1] <=> y[1]) * -1
end
return freqs_sorted
end
def get_results()
ret_str = "Dates\n"
ret_str << "\nMonths\n"
disp = false
@months.each_pair do |month, count|
unless count == 0
disp = true
ret_str << "#{month} = #{count.to_s} (#{((count.to_f/@total_words_processed) * 100).round(2).to_s}%)\n" unless count == 0
end
end
unless disp
ret_str << "None found\n"
end
months_sorted = freq_sort(@months)
ret_str << "\nMonths (Frequency ordered)\n"
disp = false
months_sorted.each do |data|
month = data[0]
count = data[1]
unless count == 0
disp = true
ret_str << "#{month} = #{count.to_s} (#{((count.to_f/@total_words_processed) * 100).round(2).to_s}%)\n" unless count == 0
end
end
unless disp
ret_str << "None found\n"
end
ret_str << "\nDays\n"
disp = false
@days.each_pair do |day, count|
unless count == 0
disp = true
ret_str << "#{day} = #{count.to_s} (#{((count.to_f/@total_words_processed) * 100).round(2).to_s}%)\n" unless count == 0
end
end
unless disp
ret_str << "None found\n"
end
days_sorted = freq_sort(@days)
ret_str << "\nDays (Frequency ordered)\n"
disp = false
days_sorted.each do |data|
day = data[0]
count = data[1]
unless count == 0
disp = true
ret_str << "#{day} = #{count.to_s} (#{((count.to_f/@total_words_processed) * 100).round(2).to_s}%)\n" unless count == 0
end
end
unless disp
ret_str << "None found\n"
end
ret_str << "\nMonths (Abreviated)\n"
disp = false
@months_ab.each_pair do |month, count|
unless count == 0
disp = true
ret_str << "#{month} = #{count.to_s} (#{((count.to_f/@total_words_processed) * 100).round(2).to_s}%)\n" unless count == 0
end
end
unless disp
ret_str << "None found\n"
end
months_ab_sorted = freq_sort(@months_ab)
ret_str << "\nMonths (Abreviated) (Frequency ordered)\n"
disp = false
months_ab_sorted.each do |data|
month = data[0]
count = data[1]
unless count == 0
disp = true
ret_str << "#{month} = #{count.to_s} (#{((count.to_f/@total_words_processed) * 100).round(2).to_s}%)\n" unless count == 0
end
end
unless disp
ret_str << "None found\n"
end
ret_str << "\nDays (Abreviated)\n"
disp = false
@days_ab.each_pair do |day, count|
unless count == 0
disp = true
ret_str << "#{day} = #{count.to_s} (#{((count.to_f/@total_words_processed) * 100).round(2).to_s} %)\n" unless count == 0
end
end
unless disp
ret_str << "None found\n"
end
days_ab_sorted = freq_sort(@days_ab)
ret_str << "\nDays (Abreviated) (Frequency ordered)\n"
disp = false
days_ab_sorted.each do |data|
day = data[0]
count = data[1]
unless count == 0
disp = true
ret_str << "#{day} = #{count.to_s} (#{((count.to_f/@total_words_processed) * 100).round(2).to_s} %)\n" unless count == 0
end
end
unless disp
ret_str << "None found\n"
end
ret_str << "\nIncludes years\n"
disp = false
@years.each_pair do |number, count|
unless count == 0
disp = true
ret_str << "#{number.to_s} = #{count.to_s} (#{((count.to_f/@total_words_processed) * 100).round(2).to_s}%)\n" unless count == 0
end
end
unless disp
ret_str << "None found\n"
end
years_freq_sort = freq_sort(@years)
ret_str << "\nYears (Top #{@cap_at.to_s})\n"
disp = false
years_freq_sort[0, @cap_at].each do |data|
disp = true
ret_str << "#{data[0].to_s} = #{data[1].to_s} (#{((data[1].to_f/@total_words_processed) * 100).round(2).to_s}%)\n"
end
unless disp
ret_str << "None found\n"
end
return ret_str
end
end