root/lang/perl/Acme-Samurai/trunk/lib/Acme/Samurai.pm @ 18107

Revision 18107, 4.5 kB (checked in by tomi-ru, 5 years ago)

前作った武士語のやつ

Line 
1package Acme::Samurai;
2use strict;
3use warnings;
4our $VERSION = '0.013';
5
6use utf8;
7use base 'Acme::Samurai::Base';
8use Lingua::JA::Alphabet::Yomi;
9use Lingua::JA::Numbers;
10use Unicode::Japanese;
11
12sub gozaru {
13    shift->new->transform(@_);
14}
15
16sub prepare {
17    my $self = shift;
18    my $text = Unicode::Japanese->new($self->text);
19    $text->z2hNum->h2zAlpha;
20    $self->text($text->getu);
21}
22
23sub finalize {
24    my $self = shift;
25    $self->{text} =~ s/(?:ておりまする|ていまする?)\b/ており候/g;
26    $self->{text} =~ s/(?:どうも)?かたじけない(?:ございま(?:する|す|した))?/かたじけない/g;
27}
28
29sub 名詞_rule {
30    my ($self, $node) = @_;
31   
32    if ($node->features->{extra}) {
33        return $node->features->{extra};
34    }
35    elsif ($node->features->{category1} eq '数' and
36        $node->surface =~ /^[0-9]+$/) {
37        if ($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 '数') {
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
62sub 動詞_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
76sub 形容詞_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
86sub 助詞_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
105sub 助動詞_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
128sub 感動詞_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
1371;
138__END__
139
140=encoding utf-8
141
142=head1 NAME
143
144Acme::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
155Acme::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 AUTHOR
167
168Naoki Tomita E<lt>tomita@cpan.orgE<gt>
169
170Special thanks to kazina, this module started from てきすたー dictionary.
171
172and Hiroko Nagashima, Shin Yamauchi for addition vocabulary.
173
174=head1 LICENSE
175
176This library is free software; you can redistribute it and/or modify
177it under the same terms as Perl itself.
178
179=head1 SEE ALSO
180
181Sample form: L<http://samurai.koneta.org/>
182
183L<Text::MeCab>
184
185L<http://coderepos.org/share/browser/lang/perl/Acme-Samurai> (repository)
186
187=cut
Note: See TracBrowser for help on using the browser.