Detrans/Rules.pm

filter: Statementstmt 100% 60 / 60 Branchbran 97.0% 33 / 34 Conditioncond 72.7% 8 / 11 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 9 / 9 Podpod 57.1% 4 / 7 totaltotal 92.1% 118 / 128 CC 23 SCAR 23.3
CC
23
Cov
96%
convert
17.0
isInitial
3.0
isFinal
3.0
CRAP 10.3
↑ Summary Detrans.pm »
1 package MARC::Detrans::Rules;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 =head1 NAME
7  
8 MARC::Detrans::Rules - A set of detransliteration rules
9  
10 =head1 SYNOPSIS
11  
12 use MARC::Detrans::Rules;
13 my $rules = MARC::Detrans::Rules->new();
14 $rules->addRule( MARC::Detrans::Rule->new( from=>'a', to='b' ) );
15  
16 =head1 DESCRIPTION
17  
18 MARC::Detrans::Rules provides the core functionality for managing
19 detransliteration rules and for converting transliterated text to
20 MARC-8. A MARC::Detrans::Rules object is essentially a collection of
21 MARC::Detrans::Rule objects which are consulted during a call to convert().
22  
23 =head1 METHODS
24  
25 =cut
26  
27 =head2 new()
28  
29 Create an empty rules object to add individual rules to.
30  
31 =cut
32  
33 9 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
34 9 my $class = shift;
35 9 my $self = { rules => {}, error => undef };
36 9 ▶ return bless $self, ref( $class ) || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
090
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
37 }
38  
39 =head2 addRule()
40  
41 Add a MARC::Detrans::Rule to the rules object.
42  
43 =cut
44  
45 465 ▶sub addRule {
Subroutine addRule: called CC=2 SCAR=6.9
Pod documented
46 465 my ( $self, $rule ) = @_;
47 ## get first character off the source for lookup
48 ## since we'll be processing a character at a time
49 465 my $key = substr( $rule->from(), 0, 1 );
50 ## look for existing rules with this key
51 465 ▶ my $rules = exists($self->{rules}{$key}) ? $self->{rules}{$key} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'rules'}{$key} ? : 104 361 465
52 ## and the new rule and sort the rules so that the longest come first.
53 ## this will mean that when we go to use the rules in convert()
54 ## that the longest match will occur first.
55 465 push( @$rules, $rule );
56 465 @$rules = sort byRule @$rules;
57 ## stash away the new rules
58 465 $self->{rules}{$key} = $rules;
59 }
60  
61 309 ▶sub byRule {
Subroutine byRule: called CC=1 SCAR=0.0
Pod undocumented
62 return
63 309 length( $b->from() . $b->position() )
64 <=>
65 length( $a->from() . $a->position() )
66 }
67  
68 =head2 convert()
69  
70 convert() applies the rules contained in the MARC::Detrans::Rules object
71 to convert a string that is passed in.
72  
73 =cut
74  
75 82 ▶sub convert {
Subroutine convert: called CC=17 SCAR=28.3
Pod documented
76 82 my ( $self, $in ) = @_;
77 ## ok, this is probably the most complicated bit of the distro
78 ## and it's not really that bad.
79 82 my $inLength = length( $in );
80 82 my $out = '';
81 82 my $pos = 0;
82 82 my $currentEscape = '';
83 ## we're going to step through the source string and build up $out
84 ## to contain the de-transliterated text
85 82 ▶ while ( $pos < $inLength ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < $inLength) 1117 80 1197
86 ## extract the character at the current position
87 ## and look to see if we have a rule for it
88 1117 my $key = substr( $in, $pos, 1 );
89 1117 ▶ my $rules = exists $self->{rules}{$key} ? $self->{rules}{$key} : [];
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'rules'}{$key} ? : 1115 2 1117
90 1117 pos($in) = $pos;
91 1117 my $foundRule;
92 ## go through each of the rules and see if we've got a match
93 1117 foreach my $rule ( @$rules ) {
94 1513 my $from = $rule->from();
95 ## if the rule matches remember it for later and jump out of
96 ## the loop since we've got what we needed
97 ## \G anchors the match at our current position
98 ## \Q...\E makes sure that metacharacters in our pattern are escaped
99 1513 ▶ if ( $in =~ m/\G\Q$from\E/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /\G\Q$from\E/) 1128 385 1513
100 1128 ▶ my $position = $rule->position() || '';
Condition100%2/2
$rule->position || ''
l!l
341094
MC/DC100%1/1
$rule->position || ''
$rule->position
Truth table100%2/2
A
A= $rule->position
Aresult
11
00
101 1128 ▶ if ( $position eq 'initial' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($position eq 'initial') { } 22 1106 1128
elsif ($position eq 'medial') { } 8 1098 1106
elsif ($position eq 'final') { } 4 1094 1098
102 22 ▶ next unless isInitial( $in, $pos );
Branch: 1 branch
BranchTrueFalseTotal
unless isInitial($in, $pos) 9 13 22
103 }
104 elsif ( $position eq 'medial' ) {
105 8 ▶ next if isInitial( $in, $pos ) or isFinal( $in, $pos );
Branch: 1 branch
BranchTrueFalseTotal
if isInitial($in, $pos) or isFinal($in, $pos) 4 4 8
Condition66%2/3
isInitial($in, $pos) or isFinal($in, $pos)
l!l&&r!l&&!r
044
MC/DC50%1/2
isInitial($in, $pos) or isFinal($in, $pos)
isInitial($in, $pos) isFinal($in, $pos)
Truth table66%2/3
A or B
A= isInitial($in, $pos)B= isFinal($in, $pos)
ABresult
1X1
011
000
106 }
107 elsif ( $position eq 'final' ) {
108 4 ▶ next unless isFinal( $in, $pos );
Branch: 1 branch
BranchTrueFalseTotal
unless isFinal($in, $pos) 0 4 4
109 }
110 1115 $foundRule = $rule;
111 1115 last;
112 }
113 }
114 ## no matched rule, then we've got a character in the source
115 ## data which doesn't map. Store the error and return asap.
116 1117 ▶ if ( ! defined($foundRule) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $foundRule) 2 1115 1117
117 2 $self->{error} = sprintf(
118 qq(no matching rule found for "%s" [0x%x] at position %i),
119 $key, ord($key), $pos+1 );
120 2 return;
121 }
122 ## advance the position the amount of characters that we matched
123 1115 $pos += length( $foundRule->from() );
124 ## if the rule has an associated MARC-8 escape character tag it
125 ## onto the output text
126 1115 ▶ if ($foundRule->escape() and $foundRule->escape() ne $currentEscape) {
Branch: 1 branch
BranchTrueFalseTotal
if ($foundRule->escape and $foundRule->escape ne $currentEscape) 77 1038 1115
Condition100%3/3
$foundRule->escape and $foundRule->escape ne $currentEscape
!ll&&!rl&&r
19101977
MC/DC100%2/2
$foundRule->escape and $foundRule->escape ne $currentEscape
$foundRule->escape $foundRule->escape ne $currentEscape
Truth table100%3/3
A and B
A= $foundRule->escapeB= $foundRule->escape ne $currentEscape
ABresult
0X0
100
111
127 77 $out .= chr(0x1B).$foundRule->escape();
128 77 $currentEscape = $foundRule->escape();
129 }
130 ## append the new text
131 1115 $out .= $foundRule->to();
132 }
133 ## escape back to ASCII if approriate
134 80 ▶ if ( $currentEscape ) { $out .= chr(0x1B).chr(0x28).chr(0x42); }
Branch: 1 branch
BranchTrueFalseTotal
if ($currentEscape) 73 7 80
135 ## make sure error flag is undef since we're ok now
136 80 $self->{error} = undef;
137 ## return the new text!
138 80 return( $out );
139 }
140  
141 =head2 error()
142  
143 Will return the latest error encountered during a call to convert(). Can
144 be useful for determining why a call to convert() failed. A side effect
145 of calling error() is that the error slot is reset.
146  
147 =cut
148  
149 3 ▶sub error {
Subroutine error: called CC=1 SCAR=0.0
Pod documented
150 3 my $self = shift;
151 3 my $error = $self->{error};
152 3 $self->{error} = undef;
153 3 return( $error );
154 }
155  
156 =head1 AUTHORS
157  
158 =over 4
159  
160 =item * Ed Summers <ehs@pobox.com>
161  
162 =cut
163  
164 ## helper functions to determine whether a specific positon in a string
165 ## is at the start or at the end of a word.
166  
167 30 ▶sub isInitial {
Subroutine isInitial: called CC=3 SCAR=11.0
Pod undocumented
168 30 my ($string,$position) = @_;
169 30 ▶ return 1 if $position == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $position == 0 6 24 30
170 24 ▶ return 1 if substr($string,$position-1,1) =~ /\W/;
Branch: 1 branch
BranchTrueFalseTotal
if substr($string, $position - 1, 1) =~ /\W/ 7 17 24
171 17 return 0;
172 }
173  
174 12 ▶sub isFinal {
Subroutine isFinal: called CC=3 SCAR=11.0
Pod undocumented
175 12 my ($string,$position) = @_;
176 12 ▶ return 1 if $position == length($string)-1;
Branch: 1 branch
BranchTrueFalseTotal
if $position == length($string) - 1 6 6 12
177 6 ▶ return 1 if substr($string,$position+1,1) =~ /\W/;
Branch: 1 branch
BranchTrueFalseTotal
if substr($string, $position + 1, 1) =~ /\W/ 2 4 6
178 }
179  
180 9 1;
↑ Summary Detrans.pm »