Acme-Nyaa
view release on metacpan or search on metacpan
lib/Acme/Nyaa.pm view on Meta::CPAN
package Acme::Nyaa;
use strict;
use warnings;
use utf8;
use 5.010001;
use Encode;
use Module::Load;
use version; our $VERSION = qv('0.0.10');
my $Default = 'ja';
sub new {
# Constructor of Acme::Nyaa
my $class = shift;
my $argvs = { @_ };
return $class if ref $class eq __PACKAGE__;
$argvs->{'objects'} = [];
$argvs->{'language'} ||= $Default;
$argvs->{'loaded-languages'} = [];
$argvs->{'objectid'} = int rand 2**24;
$argvs->{'encoding'} = q();
$argvs->{'utf8flag'} = undef;
my $nyaan = bless $argvs, __PACKAGE__;
my $klass = $nyaan->loadmodule( $argvs->{'language'} );
my $this1 = $nyaan->findobject( $klass, 1 );
$nyaan->{'subclass'} = $klass;
return $nyaan;
}
sub subclass {
my $self = shift;
return $self->{'subclass'};
}
sub language {
my $self = shift;
my $lang = shift // $self->{'language'};
return $self->{'language'} if $lang eq $self->{'language'};
return $self->{'language'} unless $lang =~ m/\A[a-zA-Z]{2}\z/;
my $nekoobject = undef;
my $referclass = $self->loadmodule( $lang );
return $self->{'language'} unless length $referclass;
return $self->{'language'} if $referclass eq $self->subclass;
$nekoobject = $self->findobject( $referclass, 1 );
return $self->{'language'} unless ref $nekoobject eq $referclass;
$self->{'language'} = $lang;
$self->{'subclass'} = $referclass;
return $self->{'language'};
}
sub objects {
my $self = shift;
$self->{'objects'} ||= [];
return $self->{'objects'};
}
sub cat {
my $self = shift;
my $text = shift // return q();
my $neko = $self->findobject( $self->subclass, 1 );
return $text unless ref $neko;
return $neko->cat( $text );
}
sub neko {
my $self = shift;
my $text = shift // return q();
my $neko = $self->findobject( $self->subclass, 1 );
return $text unless ref $neko;
return $neko->neko( $text );
}
sub nyaa {
my $self = shift;
lib/Acme/Nyaa.pm view on Meta::CPAN
my $self = shift;
my $text = shift // return q();
my $neko = $self->findobject( $self->subclass, 1 );
return $text unless ref $neko;
return $neko->straycat( $text );
}
sub loadmodule {
my $self = shift;
my $lang = shift;
my $list = $self->{'loaded-languages'};
my $referclass = __PACKAGE__.'::'.ucfirst( lc $lang );
my $alterclass = __PACKAGE__.'::'.ucfirst( $Default );
return q() unless length $lang;
return $referclass if( grep { lc $lang eq $_ } @$list );
eval {
Module::Load::load $referclass;
push @$list, lc $lang;
};
return $referclass unless $@;
return $alterclass if( grep { 'ja' eq $_ } @$list );
Module::Load::load $alterclass;
push @$list, $Default;
return $alterclass;
}
sub findobject {
my $self = shift;
my $name = shift;
my $new1 = shift || 0;
my $this = undef;
my $objs = $self->{'objects'} || [];
return unless length $name;
for my $e ( @$objs ) {
next unless ref($e) eq $name;
$this = $e;
}
return $this if ref $this;
return unless $new1;
$this = $name->new;
push @$objs, $this;
return $this;
}
sub reckon {
# Implement at sub class
my $self = shift;
return $self->{'encoding'};
}
sub toutf8 {
my $self = shift;
my $argv = shift;
my $text = undef;
$text = ref $argv ? $$argv : $argv;
return $text unless length $text;
$self->reckon( \$text );
return $text if $self->{'utf8flag'};
return $text unless $self->{'encoding'};
if( not $self->{'encoding'} =~ m/(?:ascii|utf8)/ ) {
Encode::from_to( $text, $self->{'encoding'}, 'utf8' );
}
$text = Encode::decode_utf8 $text unless utf8::is_utf8 $text;
return $text;
}
sub utf8to {
my $self = shift;
my $argv = shift;
my $text = undef;
$text = ref $argv ? $$argv : $argv;
return $text unless $self->{'encoding'};
return $text unless length $text;
$text = Encode::encode_utf8 $text if utf8::is_utf8 $text;
if( $self->{'encoding'} ne 'utf8' ) {
Encode::from_to( $text, 'utf8', $self->{'encoding'} );
}
return $text;
}
1;
__END__
=encoding utf8
=head1 NAME
Acme::Nyaa - Convert texts like which a cat is talking in Japanese
=head1 SYNOPSIS
use Acme::Nyaa;
my $kijitora = Acme::Nyaa->new;
print $kijitora->cat( \'ç«ãããããã' ); # => ç«ããããããã£ã¼ã
print $kijitora->neko( \'ç¥ã¨åè§£ãã' ); # => ãã³ã¨åè§£ãã
=head1 DESCRIPTION
Acme::Nyaa is a converter which translate Japanese texts to texts like which a cat talking.
Language modules are available only Japanese (L<Acme::Nyaa::Ja>) for now.
Nyaa is C<ãã£ã¼>, Cats living in Japan meows C<nyaa>.
=head1 CLASS METHODS
=head2 B<new( [I<%argv>] )>
new() is a constructor of Acme::Nyaa
my $kijitora = Acme::Nyaa->new();
=head1 INSTANCE METHODS
=head2 B<cat( I<\$text> )>
cat() is a converter that appends string C<ãã£ã¼> at the end of each sentence.
my $kijitora = Acme::Nyaa->new;
my $nekotext = 'ç«ãããããã';
print $kijitora->cat( \$nekotext );
# ç«ããããããã£ã¼ã
=head2 B<neko( I<\$text> )>
neko() is a converter that replace a noun with C<ãã³>.
my $kijitora = Acme::Nyaa->new;
my $nekotext = 'ç¥ã®ãã°ãã¯çªç¶ã«ãã';
print $kijitora->neko( \$nekotext );
# ãã³ã®ãã°ãã¯çªç¶ã«ãã
=head2 B<nyaa( [I<\$text>] )>
nyaa() returns string: C<ãã£ã¼>.
my $kijitora = Acme::Nyaa->new;
print $kijitora->nyaa(); # ãã£ã¼
print $kijitora->nyaa('京é½'); # 京é½ãã£ã¼
=head2 B<straycat( I<\@array-ref> | I<\$scalar-ref> [,1] )>
straycat() converts multi-lined sentences. If 2nd argument is given then
( run in 0.633 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )