AI-MicroStructure
view release on metacpan or search on metacpan
bin/remote.pl view on Meta::CPAN
my @srcs = $class->sources($_[1]);
my $ua = LWP::UserAgent->new( env_proxy => 1 );
foreach my $src (@srcs) {
my $res = $ua->request( HTTP::Request->new( GET => $src ) );
if ( ! $res->is_success() ) {
carp "Failed to get content at $src (" . $res->status_line();
return;
}
# extract, cleanup and return the data
# if decoding the content fails, we just deal with the raw content
push @items =>
$class->extract( $res->decoded_content() || $res->content() );
}
# return unique items
my %seen;
return grep { !$seen{$_}++ } @items;
}
#
# transformation subroutines
#
sub tr_nonword {
my $str = shift;
$str =~ tr/a-zA-Z0-9_/_/c;
$str;
}
sub tr_accent {
my $str = shift;
$str =~ tr{ÃÃÃÃÃÃ
ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃà áâãäåçèéêëìÃîïñòóôõöøùúûüýÿ}
{AAAAAACEEEEIIIINOOOOOOUUUUYaaaaaaceeeeiiiinoooooouuuuyy};
return $str;
}
my %utf2asc = (
"\xc3\x89" => 'E',
"\xc3\xa0" => 'a',
"\xc3\xa1" => 'a',
"\xc3\xa9" => 'e',
"\xc3\xaf" => 'i',
"\xc3\xad" => 'i',
"\xc3\xb6" => 'o',
"\xc3\xb8" => 'o',
"\xc5\xa0" => 'S',
"\x{0160}" => 'S',
# for pokemons
"\x{0101}" => 'a',
"\x{012b}" => 'i',
"\x{014d}" => 'o',
"\x{016b}" => 'u',
"\xe2\x99\x80" => 'female',
"\xe2\x99\x82" => 'male',
"\x{2640}" => 'female',
"\x{2642}" => 'male',
);
my $utf_re = qr/(@{[join( '|', sort keys %utf2asc )]})/;
sub tr_utf8_basic {
my $str = shift;
$str =~ s/$utf_re/$utf2asc{$1}/go;
return $str;
}
1;
package main;
use Data::Printer;
my $ps = AI::MicroStructure::KnowHow->new();
my @go = $ps->remote_Knowledge();
print @go;
p $ps;
1;
__DATA__
package AI::MicroStructure::Knowledge;
use strict;
use AI::MicroStructure (); # do not export metaname and friends
#use AI::MicroStructure::RemoteKnowledge;
use List::Util qw( shuffle );
use Carp;
our @ISA = qw( AI::MicroStructure::RemoteKnowledge );
sub init {
my ($self, $data) = @_;
my $class = caller(0);
$data ||= AI::MicroStructure->load_data($class);
croak "The optional argument to init() must be a hash reference"
if ref $data ne 'HASH';
no strict 'refs';
no warnings;
${"$class\::Theme"} = ( split /::/, $class )[-1];
@{"$class\::Knowledge"} = split /\s+/, $data->{names};
*{"$class\::import"} = sub {
my $callpkg = caller(0);
my $theme = ${"$class\::Theme"};
my $meta = $class->new();
*{"$callpkg\::meta$theme"} = sub { $meta->name(@_) };
};
${"$class\::meta"} = $class->new();
}
sub name {
my ( $self, $count ) = @_;
my $class = ref $self;
if( ! $class ) { # called as a class method!
$class = $self;
no strict 'refs';
$self = ${"$class\::meta"};
}
no strict 'refs';
if( defined $count && $count == 0 ) {
return
wantarray ? shuffle @{"$class\::Knowledge"} : scalar @{"$class\::Knowledge"};
}
$count ||= 1;
my $Knowledge = $self->{cache};
{
no strict 'refs';
if (@{"$class\::Knowledge"}) {
push @$Knowledge, shuffle @{"$class\::Knowledge"} while @$Knowledge < $count;
}
}
splice( @$Knowledge, 0, $count );
}
sub new {
my $class = shift;
bless { cache => [] }, $class;
}
sub theme {
my $class = ref $_[0] || $_[0];
no strict 'refs';
return ${"$class\::Theme"};
}
1;
package AI::MicroStructure::pornstars;
use strict;
#use AI::MicroStructure::KnowHow;
our @ISA = qw( AI::MicroStructure::KnowHow );
__PACKAGE__->init();
our %Remote = (
source => {
female => 'http://en.wikipedia.org/wiki/Knowledge_of_female_porn_stars',
male => 'http://en.wikipedia.org/wiki/Knowledge_of_male_porn_stars'
},
extract => sub {
return
map { AI::MicroStructure::RemoteKnowledge::tr_accent($_) }
map { AI::MicroStructure::RemoteKnowledge::tr_utf8_basic($_) }
grep { ! /^Knowledge_|_Groups$/ }
map { s/[-\s']/_/g; s/[."]//g; $_ }
$_[0]
=~ m{^<li>(?:<[^>]*>)?(.*?)(?:(?: ?[-,(<]| aka | see ).*)?</li>}mig
},
,
);
1;
package main;
use Data::Printer;
my $ps = AI::MicroStructure::pornstars->new();
my @go = $ps->extract();
p @go;
p $ps;
1;
__DATA__
# names female
Abigail_Clayton
Adara_Michaels
Addison_Rose
Adele_Stevens
Adriana_Sage
Adrienne_Bellaire
Africa_Sexxx
Ai_Iijima
Aimee_Sweet
Aja
Akira_Fubuki
Alana_Evans
Alaura_Eden
Alex_Dane
Alex_Jordan
Alex_Taylor
Alexa_May
Alexa_Rae
Alexa_Weix
Alexandra_Nice
Alexandra_Silk
Alexis_Amore
Alexis_DeVell
Alexis_Malone
Alexis_May
Alicia_Alighatti
Alicia_Monet
Alicia_Rhodes
Alicia_Rio
Alisha_Klass
Alison_Angel
Aliyah_Yi
Allie
Allie_Sin
Allison_Wyte
Ally_Mac_Tyana
Allysin_Chaynes
Amber_Lynn
Amber_Michaels
Amber_Rain
Amber_Rose
( run in 3.010 seconds using v1.01-cache-2.11-cpan-e93a5daba3e )