| 1 | package Acme::Samurai; |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | our $VERSION = '0.02'; |
|---|
| 5 | |
|---|
| 6 | use utf8; |
|---|
| 7 | use base 'Acme::Samurai::Base'; |
|---|
| 8 | use Lingua::JA::Alphabet::Yomi; |
|---|
| 9 | use Lingua::JA::Numbers; |
|---|
| 10 | use Unicode::Japanese; |
|---|
| 11 | |
|---|
| 12 | sub gozaru { |
|---|
| 13 | shift->new->transform(@_); |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | sub prepare { |
|---|
| 17 | my $self = shift; |
|---|
| 18 | my $text = Unicode::Japanese->new($self->text); |
|---|
| 19 | $text->z2hNum->h2zAlpha; |
|---|
| 20 | $self->text($text->getu); |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | sub finalize { |
|---|
| 24 | my $self = shift; |
|---|
| 25 | $self->{text} =~ s/(?:ておりまする|ていまする?)\b/ており候/g; |
|---|
| 26 | $self->{text} =~ s/(?:どうも)?かたじけない(?:ございま(?:する|す|した))?/かたじけない/g; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | sub 名詞_rule { |
|---|
| 30 | my ($self, $node) = @_; |
|---|
| 31 | |
|---|
| 32 | if ($node->features->{extra}) { |
|---|
| 33 | return $node->features->{extra}; |
|---|
| 34 | } |
|---|
| 35 | elsif ($node->features->{category1} eq '数' and $node->surface =~ /^[0-9]+$/) { # arabic number |
|---|
| 36 | if ($node->surface =~ /^0/ or |
|---|
| 37 | $node->prev_node->surface =~ /[..]/) { |
|---|
| 38 | my $r = ""; |
|---|
| 39 | $r .= Lingua::JA::Numbers::num2ja($_) for split //, $node->surface; |
|---|
| 40 | return $r; |
|---|
| 41 | } else { |
|---|
| 42 | return Lingua::JA::Numbers::num2ja($node->surface); |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | elsif ($node->features->{category1} eq '数') { # maybe kanji number |
|---|
| 46 | my $text = $node->surface; |
|---|
| 47 | $text =~ tr{〇一二三四五六七八九十百万} |
|---|
| 48 | {零壱弐参四伍六七八九拾佰萬}; |
|---|
| 49 | return $text; |
|---|
| 50 | } |
|---|
| 51 | elsif ($node->surface =~ /^\p{Latin}+$/) { |
|---|
| 52 | my $text = $node->features->{pronounse} || $node->surface; |
|---|
| 53 | $text = Lingua::JA::Alphabet::Yomi->alphabet2yomi($text); |
|---|
| 54 | $text = Unicode::Japanese->new($text)->kata2hira->getu; |
|---|
| 55 | return $text; |
|---|
| 56 | } |
|---|
| 57 | return; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | *記号_rule = \&名詞_rule; |
|---|
| 61 | |
|---|
| 62 | sub 動詞_rule { |
|---|
| 63 | my ($self, $node) = @_; |
|---|
| 64 | if ($node->surface =~ /(.+?)(じる)$/) { |
|---|
| 65 | return "$1ずる"; |
|---|
| 66 | } |
|---|
| 67 | if ($node->surface eq 'い' and |
|---|
| 68 | $node->feature =~ /^動詞,非自立,[*],[*],一段,連用形/ and |
|---|
| 69 | $node->next_node->features->{pos} !~ /詞/) { |
|---|
| 70 | return "おっ" if $node->next_node->features->{original} eq 'た'; |
|---|
| 71 | return "おり" if $node->next_node->features->{original} eq 'ます'; |
|---|
| 72 | } |
|---|
| 73 | return; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | sub 形容詞_rule { |
|---|
| 77 | my ($self, $node) = @_; |
|---|
| 78 | if ($node->surface =~ /^(.+?)(しい|しく)$/) { |
|---|
| 79 | my $a = $1; |
|---|
| 80 | my $b = { 'しい' => 'しき', 'しく' => 'しゅう' }->{$2}; |
|---|
| 81 | return "$a$b"; |
|---|
| 82 | } |
|---|
| 83 | return; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | sub 助詞_rule { |
|---|
| 87 | my ($self, $node) = @_; |
|---|
| 88 | if ($node->feature eq '助詞,終助詞,*,*,*,*,の,の,の,のか' and |
|---|
| 89 | $node->prev_node->surface eq 'な') { |
|---|
| 90 | $self->parts_pop; |
|---|
| 91 | return "なの"; |
|---|
| 92 | } |
|---|
| 93 | elsif ($node->surface eq 'ので' and |
|---|
| 94 | $node->prev_node->surface eq 'な') { |
|---|
| 95 | $self->parts_pop; |
|---|
| 96 | return "ゆえに"; |
|---|
| 97 | } |
|---|
| 98 | elsif ($node->surface eq 'ね' and |
|---|
| 99 | $node->prev_node->surface eq 'の') { |
|---|
| 100 | return "だな"; |
|---|
| 101 | } |
|---|
| 102 | return; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | sub 助動詞_rule { |
|---|
| 106 | my ($self, $node) = @_; |
|---|
| 107 | if ($node->surface eq 'ない') { |
|---|
| 108 | if ($node->prev_node->surface eq 'し' and |
|---|
| 109 | $node->next_node->surface and |
|---|
| 110 | $node->next_node->features->{pos} !~ /詞/) { |
|---|
| 111 | $self->parts_pop; |
|---|
| 112 | return "せぬ"; |
|---|
| 113 | } |
|---|
| 114 | if ($node->prev_node->surface ne 'し' and |
|---|
| 115 | $node->prev_node->features->{inflect_type} eq '未然形') { |
|---|
| 116 | return "ぬ"; |
|---|
| 117 | } |
|---|
| 118 | } |
|---|
| 119 | elsif ($node->surface eq 'なけれ') { |
|---|
| 120 | if ($node->prev_node->surface eq 'し') { |
|---|
| 121 | $self->parts_pop; |
|---|
| 122 | return "せね"; |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | return; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | sub 感動詞_rule { |
|---|
| 129 | my ($self, $node) = @_; |
|---|
| 130 | if ($node->next_node->features->{pos} !~ /詞/) { |
|---|
| 131 | my $text = $node->features->{extra} || $node->surface; |
|---|
| 132 | return $text . "でござる"; |
|---|
| 133 | } |
|---|
| 134 | return; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | 1; |
|---|
| 138 | __END__ |
|---|
| 139 | |
|---|
| 140 | =encoding utf-8 |
|---|
| 141 | |
|---|
| 142 | =head1 NAME |
|---|
| 143 | |
|---|
| 144 | Acme::Samurai - Samurai de gozaru |
|---|
| 145 | |
|---|
| 146 | =head1 SYNOPSIS |
|---|
| 147 | |
|---|
| 148 | use Acme::Samurai; |
|---|
| 149 | use utf8; |
|---|
| 150 | |
|---|
| 151 | Acme::Samurai->gozaru("私、侍です"); # それがし、侍でござる |
|---|
| 152 | |
|---|
| 153 | =head1 DESCRIPTION |
|---|
| 154 | |
|---|
| 155 | Acme::Samurai translates present-day Japanese to 時代劇 |
|---|
| 156 | (L<http://en.wikipedia.org/wiki/Jidaigeki>) speak. |
|---|
| 157 | |
|---|
| 158 | =head1 METHODS |
|---|
| 159 | |
|---|
| 160 | =over 4 |
|---|
| 161 | |
|---|
| 162 | =item gozaru( $text ) |
|---|
| 163 | |
|---|
| 164 | =back |
|---|
| 165 | |
|---|
| 166 | =head1 SEE ALSO |
|---|
| 167 | |
|---|
| 168 | Test form: L<http://samurai.koneta.org/> |
|---|
| 169 | |
|---|
| 170 | L<Text::MeCab> |
|---|
| 171 | |
|---|
| 172 | L<http://coderepos.org/share/browser/lang/perl/Acme-Samurai> (repository) |
|---|
| 173 | |
|---|
| 174 | =head1 AUTHOR |
|---|
| 175 | |
|---|
| 176 | Naoki Tomita E<lt>tomita@cpan.orgE<gt> |
|---|
| 177 | |
|---|
| 178 | Special thanks to kazina, this module started from てきすたー dictionary. |
|---|
| 179 | L<http://kazina.com/texter/index.html> |
|---|
| 180 | |
|---|
| 181 | and Hiroko Nagashima, Shin Yamauchi for addition samurai vocabulary. |
|---|
| 182 | |
|---|
| 183 | =head1 LICENSE |
|---|
| 184 | |
|---|
| 185 | This library is free software; you can redistribute it and/or modify |
|---|
| 186 | it under the same terms as Perl itself. |
|---|
| 187 | |
|---|
| 188 | =cut |
|---|