ASCII.pm

filter: Statementstmt 100% 88 / 88 Branchbran 92.8% 26 / 28 Conditioncond 76.4% 13 / 17 MC/DCmcdc 60.0% 6 / 10 Subroutinesub 100% 16 / 16 Podpod 100% 2 / 2 totaltotal 93.7% 151 / 161 CC 23 SCAR 13.6
CC
23
Cov
95%
_needs_correction
7.2
_matches
6.0
_deasciify
4.0
CRAP 3.9
↑ Summary ASCII/Data.pm »
1 package Lingua::TR::ASCII;
2 2 $Lingua::TR::ASCII::VERSION = '0.16';
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Lingua::TR::ASCII::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our @EXPORT = qw( ascii_to_turkish turkish_to_ascii );
10  
11 12 ▶sub ascii_to_turkish {
Subroutine ascii_to_turkish: called CC=2 SCAR=6.9
Pod documented
12 12 my($str) = @_;
13 12 ▶ return $str if ! $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 3 9 12
14 9 return __PACKAGE__->_new( $str )->_deasciify;
15 }
16  
17 9 ▶sub turkish_to_ascii {
Subroutine turkish_to_ascii: called CC=1 SCAR=0.0
Pod documented
18 9 my($str, $encoding) = @_;
19 9 require Text::Unidecode;
20 2 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 return Text::Unidecode::unidecode( $str );
22 }
23  
24 9 ▶sub _new {
Subroutine _new: called CC=1 SCAR=0.0
25 9 my($class, $input) = @_;
26 9 my $self = {
27 input => $input,
28 length => length $input,
29 turkish => $input,
30 };
31 9 bless $self, $class;
32 9 return $self;
33 }
34  
35 # Convert a string with ASCII-only letters into one with Turkish letters.
36 9 ▶sub _deasciify {
Subroutine _deasciify: called CC=4 SCAR=13.9
37 9 my($self) = @_;
38 9 my $s = \$self->{turkish};
39 9 my @chars = split m{}xms, ${$s};
40  
41 9 for my $i ( 0 .. $#chars ) {
42 1598 my $c = $chars[$i];
43 1598 ▶ next if ! $self->_needs_correction( $c, $i );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_needs_correction($c, $i) 1435 163 1598
44 163 ▶ substr ${$s}, $i, 1, $TOGGLE_ACCENT->{ $c } || $c;
Condition33%1/3
$Lingua::TR::ASCII::TOGGLE_ACCENT->{$c} || $c
l!l&&r!l&&!r
16300
MC/DC0%0/2
$Lingua::TR::ASCII::TOGGLE_ACCENT->{$c} || $c
$Lingua::TR::ASCII::TOGGLE_ACCENT->{$c} $c
Truth table33%1/3
A || B
A= $Lingua::TR::ASCII::TOGGLE_ACCENT->{$c}B= $c
ABresult
1X1
011
000
45 }
46  
47 9 return ${$s};
48 }
49  
50 # Determine if char at cursor needs correction.
51 1598 ▶sub _needs_correction {
Subroutine _needs_correction: called CC=7 SCAR=19.8
52 1598 my($self, $ch, $point) = @_;
53 1598 ▶ my $tr = $ASCIIFY->{ $ch } || $ch;
Condition66%2/3
$Lingua::TR::ASCII::ASCIIFY->{$ch} || $ch
l!l&&r!l&&!r
015935
MC/DC50%1/2
$Lingua::TR::ASCII::ASCIIFY->{$ch} || $ch
$Lingua::TR::ASCII::ASCIIFY->{$ch} $ch
Truth table66%2/3
A || B
A= $Lingua::TR::ASCII::ASCIIFY->{$ch}B= $ch
ABresult
1X1
011
000
54 1598 my $pl = $PATTERN->{ lc $tr };
55 1598 ▶ my $m = $pl ? $self->_matches( $pl, $point || 0 ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$pl ? : 422 1176 1598
Condition100%2/2
$point || 0
l!l
4211
MC/DC100%1/1
$point || 0
$point
Truth table100%2/2
A
A= $point
Aresult
11
00
56  
57 1598 ▶ return $tr eq 'I' ? ( $ch eq $tr ? ! $m : $m )
Branch: 3 branches
BranchTrueFalseTotal
$tr eq 'I' ? : 8 1590 1598
$ch eq $tr ? : 8 0 8
$ch eq $tr ? : 1590 0 1590
58 : ( $ch eq $tr ? $m : ! $m );
59 }
60  
61 # Check if the pattern is in the pattern table.
62 422 ▶sub _matches {
Subroutine _matches: called CC=6 SCAR=17.9
63 422 my($self, $dlist, $point) = @_;
64 422 ▶ my $str = $self->_get_context( $point || 0 );
Condition100%2/2
$point || 0
l!l
4211
MC/DC100%1/1
$point || 0
$point
Truth table100%2/2
A
A= $point
Aresult
11
00
65 422 my $rank = 2 * keys %{ $dlist };
66 422 my $len = length $str;
67 422 my($start, $end);
68  
69 422 ▶ while ( $start++ <= CONTEXT_SIZE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($start++ <= 10) 4642 422 5064
70 4642 $end = CONTEXT_SIZE;
71 4642 ▶ while ( ++$end <= $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (++$end <= $len) 25399 4642 30041
72 25399 my $s = substr $str, $start, $end - $start;
73 25399 ▶ my $r = $dlist->{ $s } || next;
Condition100%2/2
$dlist->{$s} || (next)
l!l
81524584
MC/DC100%1/1
$dlist->{$s} || (next)
$dlist->{$s}
Truth table100%2/2
A
A= $dlist->{$s}
Aresult
11
00
74 815 ▶ $rank = $r if abs $r < abs $rank;
Branch: 1 branch
BranchTrueFalseTotal
if abs $r < abs $rank 501 314 815
75 }
76 }
77  
78 422 return $rank > 0;
79 }
80  
81 422 ▶sub _get_context {
Subroutine _get_context: called CC=2 SCAR=6.9
82 422 my($self, $point, $size) = @_;
83 422 ▶ $size ||= CONTEXT_SIZE;
Condition50%1/2
$size ||= 10
l!l
0422
MC/DC0%0/1
$size ||= 10
$size
Truth table50%1/2
A
A= $size
Aresult
11
00
84 422 my($s, $i, $space, $index);
85  
86 844 ▶ my $morph = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
87 844 my($next, $lookup) = @_;
88 844 $index = $point;
89 844 $space = 0;
90 844 ▶ while ( $next->() ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$next()) 6035 844 6879
91 6035 my $char = substr $self->{turkish}, $index, 1;
92 6035 my $x = $lookup->{ $char };
93 6035 ▶ if ( $x ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x) 4926 1109 6035
94 4926 substr $s, abs $i, 1, $x;
95 4926 $space = 0;
96 4926 $i++;
97 4926 next;
98 }
99 1109 ▶ next if $space;
Branch: 1 branch
BranchTrueFalseTotal
if $space 135 974 1109
100 974 $space = 1;
101 974 $i++;
102 }
103 422 };
104  
105 422 $s = q{ } x ( 1 + ( 2 * $size ) );
106 422 $i = 1 + $size;
107 422 substr $s, $size, 1, 'X';
108  
109 $morph->(
110 2309 ▶ sub { $i < length $s && ! $space && ++$index < $self->{length} },
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
$i < length $s and not $space and ++$index < $self->{"length"} 1887 422 2309
Condition100%3/3
$i < length $s and not $space
!ll&&!rl&&r
214011887
MC/DC100%2/2
$i < length $s and not $space
$i < length $s not $space
Truth table100%3/3
A and not(B)
A= $i < length $sB= not $space
ABresult
0X0
100
111
111 422 $DOWNCASE_ASCIIFY
112 );
113  
114 422 $s = substr $s, 0, $i;
115 422 $i = 0 - --$size;
116  
117 $morph->(
118 4570 ▶ sub { $i <= 0 && --$index >= 0 },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$i <= 0 and --$index >= 0 4181 389 4570
119 422 $UPCASE_ACCENTS
120 );
121  
122 422 return $s;
123 }
124  
125 2 1;
126  
127 __END__
↑ Summary ASCII/Data.pm »