Acme-AjiFry
view release on metacpan or search on metacpan
lib/Acme/AjiFry.pm view on Meta::CPAN
use warnings;
use strict;
use utf8;
use Encode;
use List::Util;
our $VERSION = '0.09';
use constant COLS => {
a => [
'ã', 'ã', 'ã', 'ã', 'ãª', 'ã¯', 'ã¾', 'ã', 'ã', 'ã',
'ã', 'ã', 'ã ', 'ã°', 'ã±', 'ã', 'ã', 'ã'
],
i => [
'ã', 'ã', 'ã', 'ã¡', 'ã«', 'ã²', 'ã¿', 'ã',
'ã', 'ã', 'ã¢', 'ã³', 'ã´', 'ã'
],
u => [
'ã', 'ã', 'ã', 'ã¤', 'ã¬', 'ãµ', 'ã', 'ã', 'ã', 'ã',
'ã', 'ã¥', 'ã¶', 'ã·', 'ã
', 'ã£', 'ã
'
],
e => [
'ã', 'ã', 'ã', 'ã¦', 'ã', 'ã¸', 'ã', 'ã',
'ã', 'ã', 'ã§', 'ã¹', 'ãº', 'ã',
],
o => [
'ã', 'ã', 'ã', 'ã¨', 'ã®', 'ã»', 'ã', 'ã', 'ã', 'ã',
'ã', 'ã', 'ã©', 'ã¼', 'ã½', 'ã', 'ã'
],
n => ['ã'],
};
use constant ROWS => {
a =>
[ 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã
', 'ã', 'ã' ],
k =>
[ 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã' ],
s =>
[ 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã' ],
t => [
'ã', 'ã¡', 'ã¤', 'ã¦', 'ã¨', 'ã ',
'ã¢', 'ã¥', 'ã§', 'ã©', 'ã£'
],
n => [ 'ãª', 'ã«', 'ã¬', 'ã', 'ã®' ],
h => [
'ã¯', 'ã²', 'ãµ', 'ã¸', 'ã»', 'ã°', 'ã³', 'ã¶',
'ã¹', 'ã¼', 'ã±', 'ã´', 'ã·', 'ãº', 'ã½'
],
m => [ 'ã¾', 'ã¿', 'ã', 'ã', 'ã' ],
y => [ 'ã', 'ã', 'ã', 'ã', 'ã
', 'ã' ],
r => [ 'ã', 'ã', 'ã', 'ã', 'ã' ],
w => [ 'ã', 'ã', 'ã' ],
};
use constant DULLNESS => [
'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã', 'ã',
'ã ', 'ã¢', 'ã¥', 'ã§', 'ã©', 'ã°', 'ã³', 'ã¶', 'ã¹', 'ã¼'
];
use constant P_SOUND => [ 'ã±', 'ã´', 'ã·', 'ãº', 'ã½' ];
use constant DOUBLE_CONSONANT =>
[ 'ã', 'ã', 'ã
', 'ã', 'ã', 'ã£', 'ã', 'ã
', 'ã', 'ã' ];
sub new {
my $class = shift;
return $class;
}
sub to_AjiFry {
my ( $self, $raw_string ) = @_;
my $chomped = chomp($raw_string);
unless ($raw_string) {
return "\n" if $chomped;
return '';
}
$raw_string = decode_utf8($raw_string);
my $ajifry_word = $self->_to_ajifry($raw_string);
$ajifry_word .= "\n" if $chomped;
return encode_utf8($ajifry_word);
}
sub translate_to_ajifry {
my ( $self, $raw_string ) = @_;
return $self->to_AjiFry($raw_string);
}
sub to_Japanese {
my ( $self, $ajifry_word ) = @_;
my $chomped = chomp($ajifry_word);
unless ($ajifry_word) {
return "\n" if $chomped;
return '';
}
$ajifry_word = decode_utf8($ajifry_word);
my $japanese_word = $self->_to_Japanese($ajifry_word);
$japanese_word .= "\n" if $chomped;
return encode_utf8($japanese_word);
}
sub translate_from_ajifry {
my ( $self, $ajifry_word ) = @_;
return $self->to_Japanese($ajifry_word);
}
sub _search_key_of_element {
my ( $self, $element, $hash ) = @_;
foreach my $key ( sort keys %$hash ) {
if ( List::Util::first { $_ eq $element } @{ $hash->{$key} } ) {
return $key;
}
}
}
sub _find_first {
my ( $self, $key, $list ) = @_;
return ( List::Util::first { $_ eq $key } @$list ) ? 1 : 0;
}
sub _find_duplicate_element_in_both_lists {
my $self = shift;
my ( $list_A, $list_B ) = @_;
my @duplicate_elements;
foreach my $element_A ( @{$list_A} ) {
foreach my $element_B ( @{$list_B} ) {
if ( $element_A eq $element_B ) {
push( @duplicate_elements, $element_A );
}
}
}
return @duplicate_elements;
}
sub _get_ajifry_word_by_consonant {
my $self = shift;
my $consonant = shift;
if ( $consonant eq 'a' ) {
return "é£ã";
}
elsif ( $consonant eq 'k' ) {
return "ãã©ã¤";
}
elsif ( $consonant eq 's' ) {
return "ãåºèº«";
}
elsif ( $consonant eq 't' ) {
return "ã¢ã¸";
}
elsif ( $consonant eq 'n' ) {
return "ãã";
}
elsif ( $consonant eq 'h' ) {
return "山岡";
}
elsif ( $consonant eq 'm' ) {
return "岡æ";
}
elsif ( $consonant eq 'y' ) {
return "ã´ã¯ï½¥ï½¥ï½¥";
}
elsif ( $consonant eq 'r' ) {
return "ãã";
}
elsif ( $consonant eq 'w' ) {
return "éå±±";
}
else {
return "";
}
}
sub _get_ajifry_word_by_vowel {
my $self = shift;
my $vowel = shift;
if ( $vowel eq 'a' ) {
return "é£ãé£ã";
}
elsif ( $vowel eq 'i' ) {
return "ãããã";
}
elsif ( $vowel eq 'u' ) {
return "ãåºèº«";
}
elsif ( $vowel eq 'e' ) {
return "ãã・・・";
}
elsif ( $vowel eq 'o' ) {
return "ã¢ã¸ãã©ã¤";
}
elsif ( $vowel eq 'n' ) {
return "京極";
}
else {
return "";
}
}
sub _get_consonant_by_ajifry_word {
my $self = shift;
my $ajifry_word = shift;
if ( $ajifry_word eq 'é£ã' ) {
return 'a';
}
elsif ( $ajifry_word eq 'ãã©ã¤' ) {
return 'k';
}
elsif ( $ajifry_word eq 'ãåºèº«' ) {
return 's';
}
elsif ( $ajifry_word eq 'ã¢ã¸' ) {
return 't';
}
elsif ( $ajifry_word eq 'ãã' ) {
return 'n';
}
elsif ( $ajifry_word eq '山岡' ) {
return 'h';
}
elsif ( $ajifry_word eq '岡æ' ) {
return 'm';
}
elsif ($ajifry_word eq 'ã´ã¯ã»ã»ã»'
|| $ajifry_word eq 'ã´ã¯ï½¥ï½¥ï½¥'
|| $ajifry_word eq 'ã´ã¯â¦' )
{
return 'y';
}
elsif ( $ajifry_word eq 'ãã' ) {
return 'r';
}
elsif ( $ajifry_word eq 'éå±±' ) {
return 'w';
}
else {
return;
}
}
sub _get_vowel_by_ajifry_word {
my $self = shift;
my $ajifry_word = shift;
if ( $ajifry_word eq 'é£ãé£ã' ) {
return 'a';
}
elsif ( $ajifry_word eq 'ãããã' ) {
return 'i';
}
elsif ( $ajifry_word eq 'ãåºèº«' ) {
return 'u';
}
elsif ($ajifry_word eq 'ããã»ã»ã»'
|| $ajifry_word eq 'ãã・・・'
|| $ajifry_word eq 'ããâ¦' )
{
return 'e';
}
elsif ( $ajifry_word eq 'ã¢ã¸ãã©ã¤' ) {
return 'o';
}
else {
return;
}
}
sub _to_ajifry {
my $self = shift;
my $raw_string = shift;
my @raw_chars = split //, $raw_string;
my $ajifry_word;
foreach my $raw_char (@raw_chars) {
my $vowel = $self->_search_key_of_element( $raw_char, COLS );
my $consonant = $self->_search_key_of_element( $raw_char, ROWS );
if ( !$vowel && !$consonant ) {
$ajifry_word .= $raw_char; # not HIRAGANA
next;
}
$ajifry_word .= "ä¸å·"
if $self->_find_first( $raw_char, DOUBLE_CONSONANT );
$ajifry_word .= $self->_get_ajifry_word_by_consonant($consonant);
$ajifry_word .= $self->_get_ajifry_word_by_vowel($vowel);
$ajifry_word .= "社主" if $self->_find_first( $raw_char, P_SOUND );
$ajifry_word .= "é¶äºº" if $self->_find_first( $raw_char, DULLNESS );
}
return $ajifry_word;
}
sub _to_Japanese {
my $self = shift;
my $ajifry_word = shift;
my $translated_word;
while (1) {
unless ($ajifry_word) {
last;
}
my $is_double_consonant = 0;
if ( $ajifry_word =~ s/^京極// ) {
$translated_word .= 'ã';
next;
}
elsif ( $ajifry_word =~ s/^ä¸å·// ) {
$is_double_consonant = 1;
}
my $consonant;
if ( $ajifry_word =~ s/^(é£ã|ãã©ã¤|ãåºèº«|ã¢ã¸|ãã|山岡|岡æ|ã´ã¯ã»ã»ã»|ã´ã¯ï½¥ï½¥ï½¥|ã´ã¯â¦|ãã|éå±±)//
)
{
$consonant = $1;
}
unless ($consonant) {
$ajifry_word =~ s/^(.)//;
$translated_word .= $1;
next;
}
my $vowel;
if ( $ajifry_word =~ s/^(é£ãé£ã|ãããã|ãåºèº«|ããã»ã»ã»|ãã・・・|ããâ¦|ã¢ã¸ãã©ã¤)//
)
{
$vowel = $1;
}
unless ($vowel) {
$translated_word .= $consonant;
$ajifry_word =~ s/^(.)//;
$translated_word .= $1;
next;
}
my $is_dullness;
$is_dullness = $1 if $ajifry_word =~ s/^(é¶äºº)//;
my $is_p_sound;
$is_p_sound = $1 if $ajifry_word =~ s/^(社主)//;
$consonant = $self->_get_consonant_by_ajifry_word($consonant);
$vowel = $self->_get_vowel_by_ajifry_word($vowel);
my @match_characters =
$self->_find_duplicate_element_in_both_lists( ROWS->{$consonant},
COLS->{$vowel} );
if ($is_p_sound) {
$translated_word .= $match_characters[2];
}
elsif ($is_dullness) {
$translated_word .= $match_characters[1];
}
elsif ( $is_double_consonant && $consonant eq 't' ) {
( run in 1.515 second using v1.01-cache-2.11-cpan-7fcb06a456a )