| 1 | package Acme::Ikamusume::Rule; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use utf8; | ||
| 5 | use Carp; | ||
| 6 | use Lingua::JA::Kana; | ||
| 7 | |||
| 8 | sub set_rules { | ||
| 9 | my ($self, $translator) = @_; | ||
| 10 | my @rules = $self->rules; | ||
| 11 | while (my ($trigger, $code) = splice(@rules, 0, 2)) { | ||
| 12 | $translator->add_trigger( | ||
| 13 | name => $trigger, | ||
| 14 | callback => $code, | ||
| 15 | abortable => 1, | ||
| 16 | ); | ||
| 17 | } | ||
| 18 | } | ||
| 19 | |||
| 20 | use constant NEXT => 1; # for trigger control | ||
| 21 | use constant LAST => undef; # ditto | ||
| 22 | |||
| 23 | use constant CURR => -1; # for array accessor | ||
| 24 | use constant PREV => -2; # ditto | ||
| 25 | |||
| 26 | our @EBI_ACCENT = qw(! ♪ ♪ ♫ ♬ ♡); | ||
| 27 | |||
| 28 | sub rules { | ||
| 29 | |||
| 30 | # use userdic extra field | ||
| 31 | 'node.has_extra' => sub { | ||
| 32 | my ($self, $node, $words) = @_; | ||
| 33 | if (my $word = $node->features->{extra}[0]) { | ||
| 34 | $words->[CURR] = $word; | ||
| 35 | } | ||
| 36 | NEXT; | ||
| 37 | }, | ||
| 38 | |||
| 39 | # IKA: inflection | ||
| 40 | 'node.has_extra' => sub { | ||
| 41 | my ($self, $node, $words) = @_; | ||
| 42 | if (($node->features->{extra}[1] || "") eq 'inflection') { | ||
| 43 | if ($node->prev->features->{pos} eq '名詞') { | ||
| 44 | $words->[CURR] = 'じゃなイカ'; | ||
| 45 | } | ||
| 46 | elsif ($node->prev->features->{pos} eq '副詞') { | ||
| 47 | $words->[CURR] = 'でゲソか'; | ||
| 48 | } | ||
| 49 | elsif ($node->prev->features->{pos} eq '助動詞' and | ||
| 50 | $node->prev->surface eq 'です' and | ||
| 51 | $words->[PREV - 1] !~ /^[いイ]{2}$/) { | ||
| 52 | $words->[PREV] = 'じゃなイ'; | ||
| 53 | $words->[CURR] = 'カ'; | ||
| 54 | } | ||
| 55 | |||
| 56 | if ($words->[PREV] =~ /(?:イー?カ|ゲソ)$/) { | ||
| 57 | return NEXT; | ||
| 58 | } | ||
| 59 | if ($node->prev->features->{inflect} =~ /五段/) { | ||
| 60 | $words->[PREV] = _inflect_5step($words->[PREV], '.' => 'a'); | ||
| 61 | $words->[CURR] = 'なイカ'; | ||
| 62 | } | ||
| 63 | elsif ($node->prev->features->{inflect} =~ /一段|カ変|サ変/) { | ||
| 64 | $words->[CURR] = 'なイカ'; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | NEXT; | ||
| 68 | }, | ||
| 69 | |||
| 70 | # formal MASU to casual | ||
| 71 | 'node.readable' => sub { | ||
| 72 | my ($self, $node, $words) = @_; | ||
| 73 | unless ($node->features->{original} eq 'ます' and | ||
| 74 | $node->features->{pos} eq '助動詞' and | ||
| 75 | $node->prev->features->{pos} eq '動詞') { | ||
| 76 | return NEXT; | ||
| 77 | } | ||
| 78 | if ($node->features->{inflect_type} eq '基本形') { # ます | ||
| 79 | $words->[PREV] = $node->prev->features->{original}; | ||
| 80 | $words->[CURR] = ''; | ||
| 81 | |||
| 82 | if ($node->next->features->{pos} =~ /^助詞/) { | ||
| 83 | $words->[CURR] .= 'でゲソ'; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | if ($node->features->{inflect_type} eq '連用形' and # ます | ||
| 87 | $node->features->{category3} !~ /五段/) { # 五段 => { -i/っ/ん/い } | ||
| 88 | $words->[CURR] = ''; | ||
| 89 | } | ||
| 90 | NEXT; | ||
| 91 | }, | ||
| 92 | |||
| 93 | # no honorific | ||
| 94 | 'node.readable' => sub { | ||
| 95 | my ($self, $node, $words) = @_; | ||
| 96 | if ($node->feature =~ /^名詞,接尾,人名,/ and | ||
| 97 | $words->[PREV] ne 'イカ娘') { | ||
| 98 | $words->[CURR] = ''; | ||
| 99 | } | ||
| 100 | NEXT; | ||
| 101 | }, | ||
| 102 | |||
| 103 | # IKA/GESO: replace | ||
| 104 | 'node.readable' => sub { | ||
| 105 | my ($self, $node, $words) = @_; | ||
| 106 | $words->[CURR] =~ s/い[いー]か(.)/イーカ$1/g; | ||
| 107 | $words->[CURR] =~ s/いか/イカ/g; | ||
| 108 | $words->[CURR] =~ s/げそ/ゲソ/g; | ||
| 109 | |||
| 110 | return NEXT if $words->[CURR] =~ /イー?カ|ゲソ/; | ||
| 111 | |||
| 112 | my $curr = katakana2hiragana($node->features->{yomi}); | ||
| 113 | my $next = katakana2hiragana($node->next->features->{yomi} || ""); | ||
| 114 | my $prev = katakana2hiragana($node->prev->features->{yomi} || ""); | ||
| 115 | |||
| 116 | $words->[CURR] = $curr if $curr =~ s/い[いー]か(.)/イーカ$1/g; | ||
| 117 | $words->[CURR] = $curr if $curr =~ s/いか/イカ/g; | ||
| 118 | $words->[CURR] = $curr if $curr =~ s/げそ/ゲソ/g; | ||
| 119 | |||
| 120 | $words->[CURR] = $curr if $next =~ /^か./ && $curr =~ s/い[いー]$/イー/; | ||
| 121 | $words->[CURR] = $curr if $prev =~ /い[いー]$/ && $curr =~ s/^か(.)/カ$1/; | ||
| 122 | |||
| 123 | $words->[CURR] = $curr if join('', @$words[0 .. @$words - 2]) =~ /[いイ]$/ | ||
| 124 | && $curr =~ s/^か/カ/; | ||
| 125 | $words->[CURR] = $curr if $next =~ /^か/ && $curr =~ s/い$/イ/; | ||
| 126 | |||
| 127 | $words->[CURR] = $curr if $next =~ /^そ/ && $curr =~ s/げ$/ゲ/; | ||
| 128 | $words->[CURR] = $curr if $prev =~ /げ$/ && $curr =~ s/^そ/ソ/; | ||
| 129 | |||
| 130 | NEXT; | ||
| 131 | }, | ||
| 132 | |||
| 133 | # IKA/GESO: DA + postp | ||
| 134 | 'node.readable' => sub { | ||
| 135 | my ($self, $node, $words) = @_; | ||
| 136 | if ($node->prev->surface eq 'だ' and | ||
| 137 | $words->[PREV] eq 'でゲソ' and | ||
| 138 | ( | ||
| 139 | $node->features->{pos} =~ /助詞|助動詞/ or | ||
| 140 | $node->features->{category1} eq '接尾' | ||
| 141 | ) | ||
| 142 | ) { | ||
| 143 | my $kana = Lingua::JA::Kana::kana2romaji($words->[CURR]); | ||
| 144 | |||
| 145 | if ($kana =~/^(?:ze|n[aeo]|yo|wa)/) { | ||
| 146 | $words->[CURR] = ''; | ||
| 147 | $words->[PREV] = 'じゃなイカ'; | ||
| 148 | } | ||
| 149 | if ($kana =~ /^zo/) { | ||
| 150 | $words->[CURR] = ''; | ||
| 151 | } | ||
| 152 | } | ||
| 153 | NEXT; | ||
| 154 | }, | ||
| 155 | 'node.readable' => sub { | ||
| 156 | my ($self, $node, $words) = @_; | ||
| 157 | if ($node->features->{category1} eq '終助詞' and | ||
| 158 | join("", @$words) =~ /(?:でゲソ|じゃなイカ)[よなね]$/) { | ||
| 159 | $words->[CURR] = ''; | ||
| 160 | } | ||
| 161 | NEXT; | ||
| 162 | }, | ||
| 163 | |||
| 164 | # IKA: IIKA | ||
| 165 | 'node.readable' => sub { | ||
| 166 | my ($self, $node, $words) = @_; | ||
| 167 | if (not $words->[PREV] or $words->[PREV] !~ /^(?:[いイ]{2})$/) { | ||
| 168 | return NEXT; | ||
| 169 | } | ||
| 170 | if ($node->surface =~ /^(?:です|でしょう)$/ and | ||
| 171 | $node->next->surface =~ /^か/) { | ||
| 172 | $words->[PREV] = 'いイ'; | ||
| 173 | $words->[CURR] = ''; | ||
| 174 | } | ||
| 175 | if ($node->surface eq 'でしょうか') { | ||
| 176 | $words->[PREV] = 'いイ'; | ||
| 177 | $words->[CURR] = 'カ'; | ||
| 178 | } | ||
| 179 | NEXT; | ||
| 180 | }, | ||
| 181 | |||
| 182 | # GESO/IKA: eos | ||
| 183 | 'node.readable' => sub { | ||
| 184 | my ($self, $node, $words) = @_; | ||
| 185 | if ($node->next->stat == 3 or # MECAB_EOS_NODE | ||
| 186 | ( | ||
| 187 | $node->next->features->{pos} eq '記号' and | ||
| 188 | $node->next->features->{category1} =~ /句点|括弧閉|GESO可/ | ||
| 189 | ) | ||
| 190 | ) { | ||
| 191 | if ($node->features->{pos} =~ /^(?:その他|記号|助詞|接頭詞|接続詞|連体詞)/) { | ||
| 192 | return NEXT; | ||
| 193 | } | ||
| 194 | |||
| 195 | if ($node->features->{pos} eq '助動詞' and | ||
| 196 | $words->[PREV] eq 'じゃ' and | ||
| 197 | $node->surface eq 'ない') { | ||
| 198 | $words->[CURR] = 'なイカ'; | ||
| 199 | return NEXT; | ||
| 200 | } | ||
| 201 | |||
| 202 | if ($node->features->{pos} =~ /^助動詞/ and | ||
| 203 | $words->[PREV] =~ /(?:ゲソ|イー?カ)/) { | ||
| 204 | return NEXT; | ||
| 205 | } | ||
| 206 | if (join('', @$words) =~ /(?:ゲソ|イー?カ)$/) { | ||
| 207 | return NEXT; | ||
| 208 | } | ||
| 209 | |||
| 210 | $words->[CURR] .= 'でゲソ'; | ||
| 211 | } | ||
| 212 | |||
| 213 | if ($node->features->{pos} eq '動詞' and | ||
| 214 | $node->features->{inflect_type} =~ '基本形' and | ||
| 215 | $node->next->features->{pos} =~ /^助詞/) { | ||
| 216 | $words->[CURR] .= 'でゲソ'; | ||
| 217 | } | ||
| 218 | |||
| 219 | NEXT; | ||
| 220 | }, | ||
| 221 | |||
| 222 | # EBI: accent | ||
| 223 | 'node' => sub { | ||
| 224 | my ($self, $node, $words) = @_; | ||
| 225 | $words->[CURR] =~ s{(エビ|えび|海老)}{ | ||
| 226 | $1 . $EBI_ACCENT[ int rand scalar @EBI_ACCENT ]; | ||
| 227 | }e; | ||
| 228 | NEXT; | ||
| 229 | }, | ||
| 230 | |||
| 231 | } | ||
| 232 | |||
| 233 | sub _inflect_5step { | ||
| 234 | my ($verb, $from, $to) = @_; | ||
| 235 | if (my ($kana) = $verb =~ /(\p{InHiragana})$/) { | ||
| 236 | $kana = Lingua::JA::Kana::kana2romaji($kana); | ||
| 237 | $kana =~ s/^sh/s/; | ||
| 238 | $kana =~ s/^ch/t/; | ||
| 239 | $kana =~ s/^ts/t/; | ||
| 240 | $kana =~ s/$from$/$to/; | ||
| 241 | $kana =~ s/^a$/wa/; | ||
| 242 | $kana =~ s/ti/chi/; | ||
| 243 | $kana =~ s/tu/tsu/; | ||
| 244 | $kana = Lingua::JA::Kana::romaji2hiragana($kana); | ||
| 245 | |||
| 246 | $verb =~ s/.$/$kana/; | ||
| 247 | } | ||
| 248 | $verb; | ||
| 249 | } | ||
| 250 | |||
| 251 | 1; | ||
| 252 | __END__ |