]> git.donarmstrong.com Git - infobot.git/blob - blootbot/src/Modules/nickometer.pl
nickometer external
[infobot.git] / blootbot / src / Modules / nickometer.pl
1 #
2 # Lame-o-Nickometer backend
3 #
4 # (c) 1998 Adam Spiers <adam.spiers@new.ox.ac.uk>
5 #
6 # You may do whatever you want with this code, but give me credit.
7 #
8 # $Id$
9 #
10
11 package nickometer;
12
13 use strict;
14
15 my $pi          = 3.14159265;
16 my $score       = 0;
17 my $verbose     = 1;
18
19 sub query {
20   my ($message) = @_;
21
22   my $term = (lc $message eq 'me') ? $::who : $message;
23   &::DEBUG("nickometer $_:$term:$message");
24
25   if ($term =~ /^$::mask{chan}$/) {
26     &::status("Doing nickometer for chan $term.");
27
28     if (!&::validChan($term)) {
29         &::msg($::who, "error: channel is invalid.");
30         return;
31     }
32
33     # step 1.
34     my %nickometer;
35     foreach (keys %{ $::channels{lc $term}{''} }) {
36       my $str   = $_;
37       if (!defined $str) {
38         &WARN("nickometer: nick in chan $term undefined?");
39         next;
40       }
41
42       my $value = &nickometer($str);
43       $nickometer{$value}{$str} = 1;
44     }
45
46     # step 2.
47     ### TODO: compact with map?
48     my @list;
49     foreach (sort {$b <=> $a} keys %nickometer) {
50       my $str = join(", ", sort keys %{ $nickometer{$_} });
51       push(@list, "$str ($_%)");
52     }
53
54     &::performStrictReply( &::formListReply(0, "Nickometer list for $term ", @list) );
55     &::DEBUG("test.");
56
57     return;
58   }
59
60   my $percentage = &nickometer($term);
61
62   if ($percentage =~ /NaN/) {
63     $percentage = "off the scale";
64   } else {
65     $percentage = sprintf("%0.4f", $percentage);
66     $percentage =~ s/(\.\d+)0+$/$1/;
67     $percentage .= '%';
68   }
69
70   if ($::msgType eq 'public') {
71     &::say("'$term' is $percentage lame, $::who");
72   } else {
73     &::msg($::who, "the 'lame nick-o-meter' reading for $term is $percentage, $::who");
74   }
75
76   return;
77 }
78
79 sub nickometer ($) {
80   my ($text) = @_;
81   $score = 0;
82
83 #  return unless &loadPerlModule("Getopt::Std");
84   return unless &::loadPerlModule("Math::Trig");
85
86   if (!defined $text) {
87     &::DEBUG("nickometer: arg == NULL. $text");
88     return;
89   }
90
91   # Deal with special cases (precede with \ to prevent de-k3wlt0k)
92   my %special_cost = (
93     '69'                => 500,
94     'dea?th'            => 500,
95     'dark'              => 400,
96     'n[i1]ght'          => 300,
97     'n[i1]te'           => 500,
98     'fuck'              => 500,
99     'sh[i1]t'           => 500,
100     'coo[l1]'           => 500,
101     'kew[l1]'           => 500,
102     'lame'              => 500,
103     'dood'              => 500,
104     'dude'              => 500,
105     '[l1](oo?|u)[sz]er' => 500,
106     '[l1]eet'           => 500,
107     'e[l1]ite'          => 500,
108     '[l1]ord'           => 500,
109     'pron'              => 1000,
110     'warez'             => 1000,
111     'xx'                => 100,
112     '\[rkx]0'           => 1000,
113     '\0[rkx]'           => 1000,
114   );
115
116   foreach my $special (keys %special_cost) {
117     my $special_pattern = $special;
118     my $raw = ($special_pattern =~ s/^\\//);
119     my $nick = $text;
120     unless (defined $raw) {
121       $nick =~ tr/023457+8/ozeasttb/;
122     }
123     &punish($special_cost{$special}, "matched special case /$special_pattern/")
124       if (defined $nick and $nick =~ /$special_pattern/i);
125   }
126
127   # Allow Perl referencing
128   s/^\\([A-Za-z])/$1/;
129
130   # C-- ain't so bad either
131   s/^C--$/C/;
132
133   # Punish consecutive non-alphas
134   s/([^A-Za-z0-9]{2,})
135    /my $consecutive = length($1);
136     &punish(&slow_pow(10, $consecutive),
137             "$consecutive total consecutive non-alphas")
138       if $consecutive;
139     $1
140    /egx;
141
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)
146   {
147     print "Removed $2$4 outside parentheses; nick now $_\n" if $verbose;
148     &punish(15, "brackets");
149   }
150   my $parentheses = tr/(){}[]/(){}[]/;
151   &punish(&slow_pow(10, $parentheses),
152           "$parentheses unmatched " .
153             ($parentheses == 1 ? 'parenthesis' : 'parentheses'))
154     if $parentheses;
155
156   # Punish k3wlt0k
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,
161             $occurrences . ' ' .
162               (($occurrences == 1) ? 'occurrence' : 'occurrences') .
163               " of $digit")
164       if $occurrences;
165   }
166
167   # An alpha caps is not lame in middle or at end, provided the first
168   # alpha is caps.
169   my $orig_case = $_;
170   s/^([^A-Za-z]*[A-Z].*[a-z].*?)[_-]?([A-Z])/$1\l$2/;
171
172   # A caps first alpha is sometimes not lame
173   s/^([^A-Za-z]*)([A-Z])([a-z])/$1\l$2$3/;
174
175   # Punish uppercase to lowercase shifts and vice-versa, modulo
176   # exceptions above
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]/);
182
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]*$/;
185
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;
192
193   # Punish extraneous caps
194   my $caps = tr/A-Z/A-Z/;
195   &punish(&slow_pow(7, $caps), "$caps extraneous caps") if $caps;
196
197   # One and only one trailing underscore is OK.
198   s/\_$//;
199
200   # Now punish anything that's left
201   my $remains = $_;
202   $remains =~ tr/a-zA-Z0-9//d;
203   my $remains_length = length($remains);
204
205   &punish(50 * $remains_length + &slow_pow(9, $remains_length),
206           $remains_length . ' extraneous ' .
207             (($remains_length == 1) ? 'symbol' : 'symbols'))
208     if $remains;
209
210   print "\nRaw lameness score is $score\n" if $verbose;
211
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;
216
217   my $digits = 2 * (2 - &round_up(log(100 - $percentage) / log(10)));
218
219   return sprintf "%.${digits}f", $percentage;
220 }
221
222 sub case_shifts ($) {
223   # This is a neat trick suggested by freeside.  Thanks freeside!
224
225   my $shifts = shift;
226
227   $shifts =~ tr/A-Za-z//cd;
228   $shifts =~ tr/A-Z/U/s;
229   $shifts =~ tr/a-z/l/s;
230
231   return length($shifts) - 1;
232 }
233
234 sub number_shifts ($) {
235   my $shifts = shift;
236
237   $shifts =~ tr/A-Za-z0-9//cd;
238   $shifts =~ tr/A-Za-z/l/s;
239   $shifts =~ tr/0-9/n/s;
240
241   return length($shifts) - 1;
242 }
243
244 sub slow_pow ($$) {
245   my ($x, $y) = @_;
246
247   return $x ** &slow_exponent($y);
248 }
249
250 sub slow_exponent ($) {
251   my $x = shift;
252
253   return 1.3 * $x * (1 - atan($x/6) *2/$pi);
254 }
255
256 sub round_up ($) {
257   my $float = shift;
258
259   return int($float) + ((int($float) == $float) ? 0 : 1);
260 }
261
262 sub punish ($$) {
263   my ($damage, $reason) = @_;
264
265   return unless $damage;
266
267   $score += $damage;
268   print "$damage lameness points awarded: $reason\n" if $verbose;
269 }
270
271 1;