Acme-CPANAuthors-Not

 view release on metacpan or  search on metacpan

lib/Acme/CPANAuthors/Not.pm  view on Meta::CPAN


BEGIN {
  $HOWMANY = "WHAT DO YOU GET IF YOU MULTIPLY SIX BY NINE?";
}

sub _freq_table {
    my ($ids) = @_;

    # Compute frequency tables for each letter in the CPAN id, to try
    # to come up with vaguely sensible ids
    my @lengths;
    my @count; # ( offset into id => { letter => count } )
    for my $id (@$ids) {
        ++$lengths[length($id)];
        for my $i (0 .. length($id)) {
            my $letter = substr($id, $i, 1);
            $count[$i]{$letter}++;
        }
    }

    my @freq; # ( offset into id => <letter,probability> )
    for my $i (0 .. $#count) {
        # Bump up minimums of letters to one, just to allow all
        # possibilities.
        $count[$i]{$_} ||= 1 foreach ('A' .. 'Z');

        my $total = 0;
        $total += $_ foreach (values %{ $count[$i] });

        while (my ($letter, $count) = each %{ $count[$i] }) {
            push @{ $freq[$i] }, [ $letter, $count / $total ];
        }
    }

    my $length_total = 0;
    $_ ||= 0 foreach (@lengths);
    $length_total += $_ foreach (@lengths);
    $_ /= $length_total foreach (@lengths);

    return (\@lengths, \@freq);
}

sub _random_id {
    my ($lengths, $freq) = @_;

    my $lrand = rand();
    my $length = -1;
    while ($lrand > 0 && $length <= @$lengths) {
        $lrand -= $lengths->[++$length];
    }

    my $id;
    for (1 .. $length) {
        my $r = rand();
        my $lastr = $r;
        my @pick = @{ $freq->[$_] };
        while ($r > 0 && @pick > 1) {
            $r -= shift(@pick)->[1];
        }
        $id .= $pick[0]->[0];
    }

    return $id;

lib/Acme/CPANAuthors/Not.pm  view on Meta::CPAN

                  EIGHT => 8,
                  NINE => 7,
                }->{$1} || $1/eg;
        s/MULTIPLY (.*) BY (.*)/$1*$2/;
        s/WHAT DO YOU GET IF YOU(.*)\?/$1/;
    }
    $howmany = eval $howmany;

    # Compute frequency tables for each letter in the CPAN id, to try
    # to come up with vaguely sensible ids
    my ($length_freq, $letter_freq) = _freq_table([ keys %ids ]);

    # Generate $howmany random ids
    my @invalid_ids;
    while (@invalid_ids < $howmany) {
        my $id = _random_id($length_freq, $letter_freq);
        push @invalid_ids, $id unless exists $ids{$id};
    }

    # Pick a name for each author
    my $name_table = _name_table([ map { $_->name } $authors->authors ]);
    return map { $_ => _pick_name($_, $name_table) } @invalid_ids;
}

use Acme::CPANAuthors::Register(_generate());



( run in 0.721 second using v1.01-cache-2.11-cpan-65fba6d93b7 )