2 # Lame-o-Nickometer backend
4 # (c) 1998 Adam Spiers <adam.spiers@new.ox.ac.uk>
6 # You may do whatever you want with this code, but give me credit.
22 my $term = (lc $message eq 'me') ? $::who : $message;
23 &::DEBUG("nickometer $_:$term:$message");
25 if ($term =~ /^$::mask{chan}$/) {
26 &::status("Doing nickometer for chan $term.");
28 if (!&::validChan($term)) {
29 &::msg($::who, "error: channel is invalid.");
35 foreach (keys %{ $::channels{lc $term}{''} }) {
38 &WARN("nickometer: nick in chan $term undefined?");
42 my $value = &nickometer($str);
43 $nickometer{$value}{$str} = 1;
47 ### TODO: compact with map?
49 foreach (sort {$b <=> $a} keys %nickometer) {
50 my $str = join(", ", sort keys %{ $nickometer{$_} });
51 push(@list, "$str ($_%)");
54 &::performStrictReply( &::formListReply(0, "Nickometer list for $term ", @list) );
60 my $percentage = &nickometer($term);
62 if ($percentage =~ /NaN/) {
63 $percentage = "off the scale";
65 $percentage = sprintf("%0.4f", $percentage);
66 $percentage =~ s/(\.\d+)0+$/$1/;
70 if ($::msgType eq 'public') {
71 &::say("'$term' is $percentage lame, $::who");
73 &::msg($::who, "the 'lame nick-o-meter' reading for $term is $percentage, $::who");
83 # return unless &loadPerlModule("Getopt::Std");
84 return unless &::loadPerlModule("Math::Trig");
87 &::DEBUG("nickometer: arg == NULL. $text");
91 # Deal with special cases (precede with \ to prevent de-k3wlt0k)
105 '[l1](oo?|u)[sz]er' => 500,
116 foreach my $special (keys %special_cost) {
117 my $special_pattern = $special;
118 my $raw = ($special_pattern =~ s/^\\//);
120 unless (defined $raw) {
121 $nick =~ tr/023457+8/ozeasttb/;
123 &punish($special_cost{$special}, "matched special case /$special_pattern/")
124 if (defined $nick and $nick =~ /$special_pattern/i);
127 # Allow Perl referencing
130 # C-- ain't so bad either
133 # Punish consecutive non-alphas
135 /my $consecutive = length($1);
136 &punish(&slow_pow(10, $consecutive),
137 "$consecutive total consecutive non-alphas")
142 # Remove balanced brackets (and punish a little bit) and punish for unmatched
143 while (s/^([^()]*) (\() (.*) (\)) ([^()]*) $/$1$3$5/x ||
144 s/^([^{}]*) (\{) (.*) (\}) ([^{}]*) $/$1$3$5/x ||
145 s/^([^\[\]]*) (\[) (.*) (\]) ([^\[\]]*) $/$1$3$5/x)
147 print "Removed $2$4 outside parentheses; nick now $_\n" if $verbose;
148 &punish(15, "brackets");
150 my $parentheses = tr/(){}[]/(){}[]/;
151 &punish(&slow_pow(10, $parentheses),
152 "$parentheses unmatched " .
153 ($parentheses == 1 ? 'parenthesis' : 'parentheses'))
157 my @k3wlt0k_weights = (5, 5, 2, 5, 2, 3, 1, 2, 2, 2);
158 for my $digit (0 .. 9) {
159 my $occurrences = s/$digit/$digit/g || 0;
160 &punish($k3wlt0k_weights[$digit] * $occurrences * 30,
162 (($occurrences == 1) ? 'occurrence' : 'occurrences') .
167 # An alpha caps is not lame in middle or at end, provided the first
170 s/^([^A-Za-z]*[A-Z].*[a-z].*?)[_-]?([A-Z])/$1\l$2/;
172 # A caps first alpha is sometimes not lame
173 s/^([^A-Za-z]*)([A-Z])([a-z])/$1\l$2$3/;
175 # Punish uppercase to lowercase shifts and vice-versa, modulo
177 my $case_shifts = &case_shifts($orig_case);
178 &punish(&slow_pow(9, $case_shifts),
179 $case_shifts . ' case ' .
180 (($case_shifts == 1) ? 'shift' : 'shifts'))
181 if ($case_shifts > 1 && /[A-Z]/);
183 # Punish lame endings (TorgoX, WraithX et al. might kill me for this :-)
184 &punish(50, 'last alpha lame') if $orig_case =~ /[XZ][^a-zA-Z]*$/;
186 # Punish letter to numeric shifts and vice-versa
187 my $number_shifts = &number_shifts($_);
188 &punish(&slow_pow(9, $number_shifts),
189 $number_shifts . ' letter/number ' .
190 (($number_shifts == 1) ? 'shift' : 'shifts'))
191 if $number_shifts > 1;
193 # Punish extraneous caps
194 my $caps = tr/A-Z/A-Z/;
195 &punish(&slow_pow(7, $caps), "$caps extraneous caps") if $caps;
197 # One and only one trailing underscore is OK.
200 # Now punish anything that's left
202 $remains =~ tr/a-zA-Z0-9//d;
203 my $remains_length = length($remains);
205 &punish(50 * $remains_length + &slow_pow(9, $remains_length),
206 $remains_length . ' extraneous ' .
207 (($remains_length == 1) ? 'symbol' : 'symbols'))
210 print "\nRaw lameness score is $score\n" if $verbose;
212 # Use an appropriate function to map [0, +inf) to [0, 100)
213 my $percentage = 100 *
214 (1 + &Math::Trig::tanh(($score-400)/400)) *
215 (1 - 1/(1+$score/5)) / 2;
217 my $digits = 2 * (2 - &round_up(log(100 - $percentage) / log(10)));
219 return sprintf "%.${digits}f", $percentage;
222 sub case_shifts ($) {
223 # This is a neat trick suggested by freeside. Thanks freeside!
227 $shifts =~ tr/A-Za-z//cd;
228 $shifts =~ tr/A-Z/U/s;
229 $shifts =~ tr/a-z/l/s;
231 return length($shifts) - 1;
234 sub number_shifts ($) {
237 $shifts =~ tr/A-Za-z0-9//cd;
238 $shifts =~ tr/A-Za-z/l/s;
239 $shifts =~ tr/0-9/n/s;
241 return length($shifts) - 1;
247 return $x ** &slow_exponent($y);
250 sub slow_exponent ($) {
253 return 1.3 * $x * (1 - atan($x/6) *2/$pi);
259 return int($float) + ((int($float) == $float) ? 0 : 1);
263 my ($damage, $reason) = @_;
265 return unless $damage;
268 print "$damage lameness points awarded: $reason\n" if $verbose;