| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 44 | 163 | ▶ | substr ${$s}, $i, 1, $TOGGLE_ACCENT->{ $c } || $c; | ||||||||||||||||||||||||||
|
Condition33%1/3
$Lingua::TR::ASCII::TOGGLE_ACCENT->{$c} || $c
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
| |||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||
| 54 | 1598 | my $pl = $PATTERN->{ lc $tr }; | |||||||||||||||||||||||||||
| 55 | 1598 | ▶ | my $m = $pl ? $self->_matches( $pl, $point || 0 ) : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$point || 0
MC/DC100%1/1
$point || 0
$point
Truth table100%2/2
A
A= $point
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 1598 | ▶ | return $tr eq 'I' ? ( $ch eq $tr ? ! $m : $m ) | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$point || 0
$point
Truth table100%2/2
A
A= $point
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 70 | 4642 | $end = CONTEXT_SIZE; | |||||||||||||||||||||||||||
| 71 | 4642 | ▶ | while ( ++$end <= $len ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 25399 | my $s = substr $str, $start, $end - $start; | |||||||||||||||||||||||||||
| 73 | 25399 | ▶ | my $r = $dlist->{ $s } || next; | ||||||||||||||||||||||||||
|
Condition100%2/2
$dlist->{$s} || (next)
MC/DC100%1/1
$dlist->{$s} || (next)
$dlist->{$s}
Truth table100%2/2
A
A= $dlist->{$s}
| |||||||||||||||||||||||||||||
| 74 | 815 | ▶ | $rank = $r if abs $r < abs $rank; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$size ||= 10
$size
Truth table50%1/2
A
A= $size
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 91 | 6035 | my $char = substr $self->{turkish}, $index, 1; | |||||||||||||||||||||||||||
| 92 | 6035 | my $x = $lookup->{ $char }; | |||||||||||||||||||||||||||
| 93 | 6035 | ▶ | if ( $x ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$i < length $s and not $space
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 119 | 422 | $UPCASE_ACCENTS | |||||||||||||||||||||||||||
| 120 | ); | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 422 | return $s; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 2 | 1; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||