- # Remove balanced brackets and punish for unmatched
- while (s/^([^()]*) (\() (.*) (\)) ([^()]*) $/$1$3$5/x ||
- s/^([^{}]*) (\{) (.*) (\}) ([^{}]*) $/$1$3$5/x ||
- s/^([^\[\]]*) (\[) (.*) (\]) ([^\[\]]*) $/$1$3$5/x)
- {
- print "Removed $2$4 outside parentheses; nick now $_\n" if $verbose;
- }
- my $parentheses = tr/(){}[]/(){}[]/;
- &punish(&slow_pow(10, $parentheses),
- "$parentheses unmatched " .
- ($parentheses == 1 ? 'parenthesis' : 'parentheses'))
- if $parentheses;
-
- # Punish k3wlt0k
- my @k3wlt0k_weights = (5, 5, 2, 5, 2, 3, 1, 2, 2, 2);
- for my $digit (0 .. 9) {
- my $occurrences = s/$digit/$digit/g || 0;
- &punish($k3wlt0k_weights[$digit] * $occurrences * 30,
- $occurrences . ' ' .
- (($occurrences == 1) ? 'occurrence' : 'occurrences') .
- " of $digit")
- if $occurrences;
- }
-
- # An alpha caps is not lame in middle or at end, provided the first
- # alpha is caps.
- my $orig_case = $_;
- s/^([^A-Za-z]*[A-Z].*[a-z].*?)[_-]?([A-Z])/$1\l$2/;
-
- # A caps first alpha is sometimes not lame
- s/^([^A-Za-z]*)([A-Z])([a-z])/$1\l$2$3/;
-
- # Punish uppercase to lowercase shifts and vice-versa, modulo
- # exceptions above
- my $case_shifts = &case_shifts($orig_case);
- &punish(&slow_pow(9, $case_shifts),
- $case_shifts . ' case ' .
- (($case_shifts == 1) ? 'shift' : 'shifts'))
- if ($case_shifts > 1 && /[A-Z]/);
-
- # Punish lame endings (TorgoX, WraithX et al. might kill me for this :-)
- &punish(50, 'last alpha lame') if $orig_case =~ /[XZ][^a-zA-Z]*$/;
-
- # Punish letter to numeric shifts and vice-versa
- my $number_shifts = &number_shifts($_);
- &punish(&slow_pow(9, $number_shifts),
- $number_shifts . ' letter/number ' .
- (($number_shifts == 1) ? 'shift' : 'shifts'))
- if $number_shifts > 1;
-
- # Punish extraneous caps
- my $caps = tr/A-Z/A-Z/;
- &punish(&slow_pow(7, $caps), "$caps extraneous caps") if $caps;
-
- # Now punish anything that's left
- my $remains = $_;
- $remains =~ tr/a-zA-Z0-9//d;
- my $remains_length = length($remains);
-
- &punish(50 * $remains_length + &slow_pow(9, $remains_length),
- $remains_length . ' extraneous ' .
- (($remains_length == 1) ? 'symbol' : 'symbols'))
- if $remains;
-
- print "\nRaw lameness score is $score\n" if $verbose;
-
- # Use an appropriate function to map [0, +inf) to [0, 100)
- my $percentage = 100 *
- (1 + tanh(($score-400)/400)) *
- (1 - 1/(1+$score/5)) / 2;
-
- my $digits = 2 * (2 - &round_up(log(100 - $percentage) / log(10)));
-
- return sprintf "%.${digits}f", $percentage;
+ # Remove balanced brackets (and punish a little bit) and punish for unmatched
+ while ($text =~ s/^([^()]*) (\() (.*) (\)) ([^()]*) $/$1$3$5/x
+ || $text =~ s/^([^{}]*) (\{) (.*) (\}) ([^{}]*) $/$1$3$5/x
+ || $text =~ s/^([^\[\]]*) (\[) (.*) (\]) ([^\[\]]*) $/$1$3$5/x )
+ {
+ print "Removed $2$4 outside parentheses; nick now $_\n" if $verbose;
+ &punish( 15, 'brackets' );
+ }
+ my $parentheses = $text =~ tr/(){}[]/(){}[]/;
+ &punish(
+ &slow_pow( 10, $parentheses ),
+ "$parentheses unmatched "
+ . ( $parentheses == 1 ? 'parenthesis' : 'parentheses' )
+ ) if $parentheses;
+
+ # Punish k3wlt0k
+ my @k3wlt0k_weights = ( 5, 5, 2, 5, 2, 3, 1, 2, 2, 2 );
+ for my $digit ( 0 .. 9 ) {
+ my $occurrences = $text =~ s/$digit/$digit/g || 0;
+ &punish(
+ $k3wlt0k_weights[$digit] * $occurrences * 30,
+ $occurrences . ' '
+ . ( ( $occurrences == 1 ) ? 'occurrence' : 'occurrences' )
+ . " of $digit"
+ ) if $occurrences;
+ }
+
+ # An alpha caps is not lame in middle or at end, provided the first
+ # alpha is caps.
+ my $orig_case = $text;
+ $text =~ s/^([^A-Za-z]*[A-Z].*[a-z].*?)[_-]?([A-Z])/$1\l$2/;
+
+ # A caps first alpha is sometimes not lame
+ $text =~ s/^([^A-Za-z]*)([A-Z])([a-z])/$1\l$2$3/;
+
+ # Punish uppercase to lowercase shifts and vice-versa, modulo
+ # exceptions above
+ my $case_shifts = &case_shifts($orig_case);
+ &punish(
+ &slow_pow( 9, $case_shifts ),
+ $case_shifts . ' case ' . ( ( $case_shifts == 1 ) ? 'shift' : 'shifts' )
+ ) if ( $case_shifts > 1 && /[A-Z]/ );
+
+ # Punish lame endings (TorgoX, WraithX et al. might kill me for this :-)
+ &punish( 50, 'last alpha lame' ) if $orig_case =~ /[XZ][^a-zA-Z]*$/;
+
+ # Punish letter to numeric shifts and vice-versa
+ my $number_shifts = &number_shifts($_);
+ &punish(
+ &slow_pow( 9, $number_shifts ),
+ $number_shifts
+ . ' letter/number '
+ . ( ( $number_shifts == 1 ) ? 'shift' : 'shifts' )
+ ) if $number_shifts > 1;
+
+ # Punish extraneous caps
+ my $caps = $text =~ tr/A-Z/A-Z/;
+ &punish( &slow_pow( 7, $caps ), "$caps extraneous caps" ) if $caps;
+
+ # One and only one trailing underscore is OK.
+ $text =~ s/\_$//;
+
+ # Now punish anything that's left
+ my $remains = $text;
+ $remains =~ tr/a-zA-Z0-9//d;
+ my $remains_length = length($remains);
+
+ &punish(
+ 50 * $remains_length + &slow_pow( 9, $remains_length ),
+ $remains_length
+ . ' extraneous '
+ . ( ( $remains_length == 1 ) ? 'symbol' : 'symbols' )
+ ) if $remains;
+
+ print "\nRaw lameness score is $score\n" if $verbose;
+
+ # Use an appropriate function to map [0, +inf) to [0, 100)
+ my $percentage = 100 * ( 1 + &Math::Trig::tanh( ( $score - 400 ) / 400 ) ) *
+ ( 1 - 1 / ( 1 + $score / 5 ) ) / 2;
+
+ my $digits = 2 * ( 2 - &round_up( log( 100 - $percentage ) / log(10) ) );
+
+ return sprintf "%.${digits}f", $percentage;