Changeset 7900 for lang/perl/Acme-Shukugawa-Atom
- Timestamp:
- 03/13/08 19:51:16 (5 years ago)
- Location:
- lang/perl/Acme-Shukugawa-Atom/trunk
- Files:
-
- 3 modified
-
Changes (modified) (1 diff)
-
Makefile.PL (modified) (1 diff)
-
lib/Acme/Shukugawa/Atom.pm (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/perl/Acme-Shukugawa-Atom/trunk/Changes
r7895 r7900 3 3 4 4 0.00004 - 5 * Add OO interface 5 6 * Work with new tests (otsune) 6 7 * Add t/02_shisu.t -
lang/perl/Acme-Shukugawa-Atom/trunk/Makefile.PL
r7885 r7900 4 4 name('Acme-Shukugawa-Atom'); 5 5 all_from('lib/Acme/Shukugawa/Atom.pm'); 6 requires('Class::Accessor::Fast'); 6 7 requires('Text::MeCab', '0.20007'); 7 8 requires('Encode'); -
lang/perl/Acme-Shukugawa-Atom/trunk/lib/Acme/Shukugawa/Atom.pm
r7895 r7900 4 4 use strict; 5 5 use warnings; 6 use base qw(Class::Accessor::Fast); 6 7 use utf8; 7 8 use Encode qw(decode_utf8); … … 10 11 our $VERSION = '0.00003'; 11 12 12 sub translate 13 { 14 my $self = shift; 15 my $string = decode_utf8(shift); 16 17 $self->preprocess(\$string); 18 $self->runthrough(\$string); 19 $self->postprocess(\$string); 20 21 return $string; 22 } 13 __PACKAGE__->mk_accessors($_) for qw(custom_words); 23 14 24 15 # Special case handling -- this could be optimized further 25 16 # put it in a sharefile later 26 my (@SPECIAL, $EXCEPTION, $RE_SMALL, $RE_SYLLABLE, $RE_NBAR);17 our (@DEFAULT_WORDS, $RE_EXCEPTION, $RE_SMALL, $RE_SYLLABLE, $RE_NBAR); 27 18 BEGIN 28 19 { … … 30 21 $RE_SYLLABLE = decode_utf8("(?:.$RE_SMALL?)"); 31 22 $RE_NBAR = decode_utf8("^ンー"); 32 @ SPECIAL= (23 @DEFAULT_WORDS = ( 33 24 '急ぎで|急いでる?' => '巻きで', 34 25 '小飼弾|(?i)dankogai|(?i)kogaidan' => 'ガイダンコ', … … 50 41 '片付け|かたづけ' => 'わらう' 51 42 ); 52 $EXCEPTION = decode_utf8(join("|", 53 map { $SPECIAL[$_ * 2 + 1] } (0..$#SPECIAL/2) )); 43 } 44 45 sub _create_exception_re 46 { 47 my $self = shift; 48 my $custom = $self->custom_words; 49 50 return decode_utf8(join("|", 51 map { $custom->[$_ * 2 + 1] } (0..(scalar(@$custom) - 1)/2) )); 52 } 53 54 sub translate 55 { 56 my $self = shift; 57 my $string = decode_utf8(shift); 58 59 if (! ref $self) { 60 $self = $self->new({ custom_words => \@DEFAULT_WORDS, @_ }); 61 } 62 63 # Create local RE_EXCEPTION 64 local $RE_EXCEPTION = $self->_create_exception_re; 65 66 $self->preprocess(\$string); 67 $self->runthrough(\$string); 68 $self->postprocess(\$string); 69 70 return $string; 54 71 } 55 72 … … 57 74 { 58 75 my ($self, $strref) = @_; 59 60 for(0..$#SPECIAL/2) { 61 my $pattern = $SPECIAL[$_ * 2]; 62 my $replace = $SPECIAL[$_ * 2 + 1]; 76 my $custom = $self->custom_words; 77 78 for(0..(scalar(@$custom) - 1)/2) { 79 my $pattern = $custom->[$_ * 2]; 80 my $replace = $custom->[$_ * 2 + 1]; 63 81 $$strref =~ s/$pattern/$replace/g; 64 82 } … … 75 93 my $ret = ''; 76 94 77 foreach my $text (split(/($ EXCEPTION)/, $$strref)) {78 if ($text =~ /$ EXCEPTION/) {95 foreach my $text (split(/($RE_EXCEPTION)/, $$strref)) { 96 if ($text =~ /$RE_EXCEPTION/) { 79 97 $ret .= $text; 80 98 next; … … 222 240 my $newstring = Acme::Shukugawa::Atom->translate($string); 223 241 242 my $atom = Acme::Shukugawa::Atom->new( 243 # The default values are stored in @Acme::Shukugawa::Atom::DEFAULT_WORDS 244 custom_words => [ 245 'regexp1' => 'replacement1' 246 'regexp2' => 'replacement2' 247 'regexp3' => 'replacement3' 248 'regexp4' => 'replacement4' 249 .... 250 ] 251 ); 252 my $newstring = $atom->translate($string); 253 254 # shorter way 255 my $newstring = Acme::Shukugawa::Atom->translate($string, 256 custom_words => [ ... ] 257 ); 258 224 259 =head1 DESCRIPTION 225 260
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)