]> git.donarmstrong.com Git - infobot.git/blob - src/Modules/nickometer.pl
Initial revision
[infobot.git] / 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 use strict;
12
13 my $pi          = 3.1415;
14 my $score       = 0;
15 my $verbose     = 0;
16
17 sub nickometer ($) {
18   return unless &loadPerlModule("Getopt::Std");
19   return unless &loadPerlModule("Math::Trig");
20
21   local $_ = shift;
22   $score = 0;
23
24   if (!defined) {
25     &DEBUG("nickometer: arg == NULL.");
26     return;
27   }
28
29   # Deal with special cases (precede with \ to prevent de-k3wlt0k)
30   my %special_cost = (
31         '69'                    => 500,
32         'dea?th'                => 500,
33         'dark'                  => 400,
34         'n[i1]ght'              => 300,
35         'n[i1]te'               => 500,
36         'fuck'                  => 500,
37         'sh[i1]t'               => 500,
38         'coo[l1]'               => 500,
39         'kew[l1]'               => 500,
40         'lame'                  => 500,
41         'dood'                  => 500,
42         'dude'                  => 500,
43         '[l1](oo?|u)[sz]er'     => 500,
44         '[l1]eet'               => 500,
45         'e[l1]ite'              => 500,
46         '[l1]ord'               => 500,
47         'pron'                  => 1000,
48         'warez'                 => 1000,
49         'xx'                    => 100,
50         '\[rkx]0'               => 1000,
51         '\0[rkx]'               => 1000,
52   );
53
54   foreach my $special (keys %special_cost) {
55     my $special_pattern = $special;
56     my $raw = ($special_pattern =~ s/^\\//);
57     my $nick = $_;
58     unless (defined $raw) {
59       $nick =~ tr/023457+8/ozeasttb/;
60     }
61     &punish($special_cost{$special}, "matched special case /$special_pattern/")
62       if (defined $nick and $nick =~ /$special_pattern/i);
63   }
64
65   # Allow Perl referencing
66   s/^\\([A-Za-z])/$1/;
67
68   # C-- ain't so bad either
69   s/^C--$/C/;
70
71   # Punish consecutive non-alphas
72   s/([^A-Za-z0-9]{2,})
73    /my $consecutive = length($1);
74     &punish(&slow_pow(10, $consecutive),
75             "$consecutive total consecutive non-alphas")
76       if $consecutive;
77     $1
78    /egx;
79
80   # Remove balanced brackets and punish for unmatched
81   while (s/^([^()]*)   (\() (.*) (\)) ([^()]*)   $/$1$3$5/x ||
82          s/^([^{}]*)   (\{) (.*) (\}) ([^{}]*)   $/$1$3$5/x ||
83          s/^([^\[\]]*) (\[) (.*) (\]) ([^\[\]]*) $/$1$3$5/x)
84   {
85     print "Removed $2$4 outside parentheses; nick now $_\n" if $verbose;
86   }
87   my $parentheses = tr/(){}[]/(){}[]/;
88   &punish(&slow_pow(10, $parentheses),
89           "$parentheses unmatched " .
90             ($parentheses == 1 ? 'parenthesis' : 'parentheses'))
91     if $parentheses;
92
93   # Punish k3wlt0k
94   my @k3wlt0k_weights = (5, 5, 2, 5, 2, 3, 1, 2, 2, 2);
95   for my $digit (0 .. 9) {
96     my $occurrences = s/$digit/$digit/g || 0;
97     &punish($k3wlt0k_weights[$digit] * $occurrences * 30,
98             $occurrences . ' ' .
99               (($occurrences == 1) ? 'occurrence' : 'occurrences') .
100               " of $digit")
101       if $occurrences;
102   }
103
104   # An alpha caps is not lame in middle or at end, provided the first
105   # alpha is caps.
106   my $orig_case = $_;
107   s/^([^A-Za-z]*[A-Z].*[a-z].*?)[_-]?([A-Z])/$1\l$2/;
108
109   # A caps first alpha is sometimes not lame
110   s/^([^A-Za-z]*)([A-Z])([a-z])/$1\l$2$3/;
111
112   # Punish uppercase to lowercase shifts and vice-versa, modulo
113   # exceptions above
114   my $case_shifts = &case_shifts($orig_case);
115   &punish(&slow_pow(9, $case_shifts),
116           $case_shifts . ' case ' .
117             (($case_shifts == 1) ? 'shift' : 'shifts'))
118     if ($case_shifts > 1 && /[A-Z]/);
119
120   # Punish lame endings (TorgoX, WraithX et al. might kill me for this :-)
121   &punish(50, 'last alpha lame') if $orig_case =~ /[XZ][^a-zA-Z]*$/;
122
123   # Punish letter to numeric shifts and vice-versa
124   my $number_shifts = &number_shifts($_);
125   &punish(&slow_pow(9, $number_shifts),
126           $number_shifts . ' letter/number ' .
127             (($number_shifts == 1) ? 'shift' : 'shifts'))
128     if $number_shifts > 1;
129
130   # Punish extraneous caps
131   my $caps = tr/A-Z/A-Z/;
132   &punish(&slow_pow(7, $caps), "$caps extraneous caps") if $caps;
133
134   # Now punish anything that's left
135   my $remains = $_;
136   $remains =~ tr/a-zA-Z0-9//d;
137   my $remains_length = length($remains);
138
139   &punish(50 * $remains_length + &slow_pow(9, $remains_length),
140           $remains_length . ' extraneous ' .
141             (($remains_length == 1) ? 'symbol' : 'symbols'))
142     if $remains;
143
144   print "\nRaw lameness score is $score\n" if $verbose;
145
146   # Use an appropriate function to map [0, +inf) to [0, 100)
147   my $percentage = 100 *
148                 (1 + tanh(($score-400)/400)) *
149                 (1 - 1/(1+$score/5)) / 2;
150
151   my $digits = 2 * (2 - &round_up(log(100 - $percentage) / log(10)));
152
153   return sprintf "%.${digits}f", $percentage;
154 }
155
156 sub case_shifts ($) {
157   # This is a neat trick suggested by freeside.  Thanks freeside!
158
159   my $shifts = shift;
160
161   $shifts =~ tr/A-Za-z//cd;
162   $shifts =~ tr/A-Z/U/s;
163   $shifts =~ tr/a-z/l/s;
164
165   return length($shifts) - 1;
166 }
167
168 sub number_shifts ($) {
169   my $shifts = shift;
170
171   $shifts =~ tr/A-Za-z0-9//cd;
172   $shifts =~ tr/A-Za-z/l/s;
173   $shifts =~ tr/0-9/n/s;
174
175   return length($shifts) - 1;
176 }
177
178 sub slow_pow ($$) {
179   my ($x, $y) = @_;
180
181   return $x ** &slow_exponent($y);
182 }
183
184 sub slow_exponent ($) {
185   my $x = shift;
186
187   return 1.3 * $x * (1 - atan($x/6) *2/$pi);
188 }
189
190 sub round_up ($) {
191   my $float = shift;
192
193   return int($float) + ((int($float) == $float) ? 0 : 1);
194 }
195
196 sub punish ($$) {
197   my ($damage, $reason) = @_;
198
199   return unless $damage;
200
201   $score += $damage;
202   print "$damage lameness points awarded: $reason\n" if $verbose;
203 }
204
205 1;