Permalink
Browse files

Adding Punctuation Spacer,

Improvemented Numeric Spacer.
  • Loading branch information...
1 parent b9f5228 commit 1953caa8735b9777f2ef1d260538a8bacd9678ed sng2c committed Apr 25, 2012
@@ -3,7 +3,7 @@ use Any::Moose;
use namespace::autoclean;
use Module::Load;
use Carp;
-our $VERSION = '0.003';
+our $VERSION = '0.004';
has 'splitter' => (
@@ -11,13 +11,19 @@ sub Space{
sub _space{
my $str = shift;
- $str =~ s/(\d[\d,]*(\.\d+)?)(\D)/$1 $3/gm;
- $str =~ s/([^,.\d])(\d[\d,]*(\.\d+)?)/$1 $2/gm;
+ $str =~ s/([\d\.,]*\d)/_num($`,$1,$')/gme;
$str =~ s/\s+/ /g;
$str =~ s/^\s//g;
$str =~ s/\s$//g;
return $str;
}
+
+sub _num{
+ my ($p,$m,$n) = @_;
+ $m =~ s/^[\.,]/$& /;
+ $m =~ s/[\.,]$/ $&/;
+ return " $m ";
+}
=pod
=head1 NAME
@@ -0,0 +1,47 @@
+package Template::Reverse::Spacer::Punctuation;
+use Any::Moose;
+use namespace::autoclean;
+
+our $VERSION = '0.001';
+sub Space{
+ my $self = shift;
+ my $str = shift;
+ return _space($str);
+}
+
+sub _space{
+ my $str = shift;
+ $str =~ s/([~`!\@#\$\%^&*()_+\-=\[\]{};:'",<\.>\/\?\|\\]+)/_punc($`,$1,$')/ge;
+
+ $str =~ s/\s+/ /g;
+ $str =~ s/^\s//g;
+ $str =~ s/\s$//g;
+ return $str;
+}
+sub _punc{
+ my ($p,$m,$n) = @_;
+
+ if( $m =~/[\.,]/ && $p =~ /\d$/ && $n =~ /^\d/ ){
+ return $m;
+ }
+ else{
+ return " $m ";
+ }
+}
+
+=pod
+
+=head1 NAME
+
+Template::Reverse::Spacer::Punctuation - Insert spaces around punctuations.
+
+=head1 SYNOPSIS
+
+ package Template::Reverse::Spacer::Punctuation;
+ my $num = Template::Reverse::Spacer::Punctuation->new;
+ $num->Space('hello,world!!'); # 'hello , world !!'
+
+=cut
+
+__PACKAGE__->meta->make_immutable;
+1;
View
@@ -43,12 +43,21 @@ $m4 = '<span value=" 1 " height= 12 px> 1,000 dollars</span>';
is $m3, $m4;
+$m3 = space("2.3.3.4");
+$m4 = "2.3.3.4";
+is $m3, $m4;
+
TODO:{
- local $TODO="Need more time to dig";
- $m3 = space("2.3.3.4");
- $m4 = "2.3.3.4";
- is $m3, $m4;
-}
+local $TODO="not implement";
+$m3 = space("2,33");
+$m4 = "2 , 33";
+is $m3, $m4;
+
+$m3 = space("2,333,2,444");
+$m4 = "2,333 , 2,444";
+is $m3, $m4;
+};
+
=pod
my ($f1,$f2) = @ARGV;
View
@@ -2,10 +2,12 @@ use Test::More;
use Data::Dumper;
BEGIN{
use_ok('Template::Reverse');
+use_ok('Template::Reverse::Spacer::Punctuation');
+use_ok('Template::Reverse::Spacer::Numeric');
};
my $rev = Template::Reverse->new({
- spacers=>['Template::Reverse::Spacer::Numeric'], # at first spacing/unspacing text by
+ spacers=>['Template::Reverse::Spacer::Numeric','Template::Reverse::Spacer::Punctuation'], # at first spacing/unspacing text by
});
is ref($rev),'Template::Reverse';
@@ -18,16 +20,27 @@ my ($str1,$str2,$res);
$str1 = "A B C D E F";
$str2 = "A B C D E F";
$res = $rev->space($str1);
-is $res, $str2;
+is $res, $str2, 'numeric';
$str1 = "가격 1200 원";
$str2 = "가격 1200 원";
$res = $rev->space($str1);
-is $res, $str2;
+is $res, $str2, 'numeric';
$str1 = "가격 1200원";
$str2 = "가격 1200 원";
$res = $rev->space($str1);
-is $res, $str2;
+is $res, $str2, 'numeric';
+
+$str1 = "A.B.C.D.E";
+$str2 = "A . B . C . D . E";
+$res = $rev->space($str1);
+is $res, $str2, 'punctuation';
+
+$str1 = "12.1A.B.C.D.E";
+$str2 = "12.1 A . B . C . D . E";
+$res = $rev->space($str1);
+is $res, $str2, 'punctuation';
+
done_testing();
View
@@ -0,0 +1,67 @@
+use Test::More;
+
+BEGIN{
+use_ok("Template::Reverse::Spacer::Punctuation");
+};
+
+sub space{
+ my $str = shift;
+ return Template::Reverse::Spacer::Punctuation->Space($str);
+}
+
+$m1 = space("ABC DEF HIJ");
+$m2 = "ABC EF HIJ";
+isnt $m1, $m2;
+
+$m3 = space(".,:ABC");
+$m4 = ".,: ABC";
+is $m3, $m4;
+
+$m3 = space(".ABC");
+$m4 = ". ABC";
+is $m3, $m4;
+
+$m3 = space("1,123");
+$m4 = "1,123";
+is $m3, $m4;
+
+$m3 = space("1,123.2ABC");
+$m4 = "1,123.2ABC";
+is $m3, $m4;
+
+$m3 = space("1,123.2.ABC");
+$m4 = "1,123.2 . ABC";
+is $m3, $m4;
+
+$m3 = space('<span value="1"> 1,000dollars</span>');
+$m4 = '< span value =" 1 "> 1,000dollars </ span >';
+is $m3, $m4;
+
+
+$m3 = space('<span value="1" height=12px> 1,000dollars</span>');
+$m4 = '< span value =" 1 " height = 12px > 1,000dollars </ span >';
+is $m3, $m4;
+
+
+$m3 = space("2.3.3.4");
+$m4 = "2.3.3.4";
+is $m3, $m4;
+
+$m3 = space("2,3,3,4");
+$m4 = "2,3,3,4";
+is $m3, $m4;
+
+$m3 = space("2.3,3.4");
+$m4 = "2.3,3.4";
+is $m3, $m4;
+
+$m3 = space("2,300.3.4");
+$m4 = "2,300.3.4";
+is $m3, $m4;
+
+$m3 = space("Hello,world!!");
+$m4 = "Hello , world !!";
+is $m3, $m4;
+
+
+done_testing();

0 comments on commit 1953caa

Please sign in to comment.