blib/lib/Lingua/ES/PhT.pm

filter: Statementstmt 28.5% 32 / 112 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 39 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 66.6% 6 / 9 Podpod 100% 1 / 1 totaltotal 16.3% 39 / 238 CC 45 SCAR 59.8
CC
45
Cov
15%
_transcribeSyllabe
702.0
_setStressOnSyllabe
182.0
_setStressOnSyllabedWord
42.0
CRAP 394.0
↑ Summary
1 package Lingua::ES::PhT;
2  
3 1 ▶use 5.008008;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 require Exporter;
8  
9 1 our @ISA = qw(Exporter);
10  
11 # Items to export into callers namespace by default. Note: do not export
12 # names by default without a very good reason. Use EXPORT_OK instead.
13 # Do not simply export all your public functions/methods/constants.
14  
15  
16 1 our %EXPORT_TAGS = ( 'test' => [ qw(
17 transcribe
18 ) ] );
19  
20 1 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'test'} } );
21  
22 1 our @EXPORT = qw(
23
24 );
25  
26 1 our $VERSION = '0.9';
27 1 ▶use Lingua::ES::Syllabify;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use String::Multibyte;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 sub _setStressOnSyllabe;
31 sub _setStressOnSyllabedWord;
32  
33  
34  
35 =item transcribe
36  
37 Phonetically transcribe the given piece of text.
38  
39 If text has several words, the returned list of phonemes has no idications
40 of word boundaries.
41  
42 =cut
43 1 ▶sub transcribe($) {
Subroutine transcribe: called CC=3 SCAR=11.1
Pod documented
44 1 my $text = shift;
45  
46 1 my @phonemes = ();
47  
48 1 my @words = split(/\s+/, $text);
49  
50 1 foreach my $word (@words) {
51 # Some replacement for phonemes depending on suprasyllabical information
52 1 $word =~ s/^r/R/;
53 1 $word =~ s/([bcdfghjklmnpqstvwxyz])r/$1R/g;
54 1 $word =~ s/^ps/s/;
55 1 $word =~ s/^gn/n/;
56  
57 1 $word =~ s/^x/s/g;
58  
59 1 my @syllabes =
60 _setStressOnSyllabedWord(
61 Lingua::ES::Syllabify::getSyllables($word));
62  
63 0 push(@phonemes, _transcribeSyllabe($_)) foreach @syllabes;
64 }
65  
66 0 return @phonemes;
67 }
68  
69 0 ▶sub _transcribeSyllabe($) {
Subroutine _transcribeSyllabe: not called CC=26 SCAR=65.5
70 0 my $syllabe = shift;
71  
72 0 my @phonemesToReturn;
73  
74 0 my %symbolsWithDirectMappings = (
75 "a" => "a",
76 "á" => "a'",
77 "b" => "b",
78 "C" => "tS", # ch
79 "d" => "d",
80 "e" => "e",
81 "é" => "e'",
82 "f" => "f",
83 "G" => "g", # gu
84 "í" => "i'",
85 "j" => "x",
86 "k" => "k",
87 "l" => "l",
88 "L" => "L",
89 "m" => "m",
90 "n" => "n",
91 "ñ" => "J",
92 "o" => "o",
93 "ó" => "o'",
94 "p" => "p",
95 "q" => "k",
96 "Q" => "k", # qu
97 "r" => "r",
98 "R" => "rr", # rr
99 "s" => "s",
100 "t" => "t",
101 "ü" => "w",
102 "ú" => "u'",
103 "v" => "b",
104 "z" => "T"
105 );
106  
107 0 my $vowels_re = "i|í|u|ú|ü|a|á|e|é|o|ó";
108 0 my $consonants_re = "[bcdfghjklmnpqrstvwxyz]";
109  
110 # Some replacement for phonemes expanding several letters
111 0 $syllabe =~ s/rr/R/;
112 0 $syllabe =~ s/ll/L/;
113 0 $syllabe =~ s/ch/C/;
114 0 $syllabe =~ s/tx/C/;
115 0 $syllabe =~ s/qu(e|é|i|í)/Q$1/;
116 0 $syllabe =~ s/gu(e|é|i|í)/G$1/;
117  
118 # Some replacements
119 0 $syllabe =~ s/x/cs/;
120  
121 0 my $utf8 = new String::Multibyte('UTF8');
122 0 my @chars = $utf8->strsplit('', $syllabe);
123 0 my $cInd = 0;
124 0 ▶ while ($cInd < @chars) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cInd < @chars) 0 0 0
125 0 my $char = $chars[$cInd];
126 0 ▶ if ($symbolsWithDirectMappings{$char}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($symbolsWithDirectMappings{$char}) { } 0 0 0
127 0 push(@phonemesToReturn, $symbolsWithDirectMappings{$char});
128 } else {
129 0 ▶ if ($char eq 'c') {
Branch: 7 branches
BranchTrueFalseTotal
if ($char eq 'c') { } 0 0 0
elsif ($char eq 'g') { } 0 0 0
elsif ($char eq 'h') { } 0 0 0
elsif ($char eq 'i') { } 0 0 0
elsif ($char eq 'u') { } 0 0 0
elsif ($char eq 'w') { } 0 0 0
elsif ($char eq 'y') { } 0 0 0
130 0 ▶ if ($cInd < $#chars && $chars[$cInd+1] =~ /e|é|i|í/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cInd < $#chars and $chars[$cInd + 1] =~ /e|\303\251|i|\303\255/) { } 0 0 0
Condition0%0/3
$cInd < $#chars and $chars[$cInd + 1] =~ /e|\303\251|i|\303\255/
!ll&&!rl&&r
000
MC/DC0%0/2
$cInd < $#chars and $chars[$cInd + 1] =~ /e|\303\251|i|\303\255/
$cInd < $#chars $chars[$cInd + 1] =~ /e|\303\251|i|\303\255/
Truth table0%0/3
A and B
A= $cInd < $#charsB= $chars[$cInd + 1] =~ /e|\303\251|i|\303\255/
ABresult
0X0
100
111
131 0 push(@phonemesToReturn, 'T');
132 } else {
133 0 push(@phonemesToReturn, 'k');
134 }
135 } elsif ($char eq 'g') {
136 0 ▶ if ($cInd < $#chars && $chars[$cInd+1] =~ /a|á|o|ó|ü/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cInd < $#chars and $chars[$cInd + 1] =~ /a|\303\241|o|\303\263|\303\274/) { } 0 0 0
Condition0%0/3
$cInd < $#chars and $chars[$cInd + 1] =~ /a|\303\241|o|\303\263|\303\274/
!ll&&!rl&&r
000
MC/DC0%0/2
$cInd < $#chars and $chars[$cInd + 1] =~ /a|\303\241|o|\303\263|\303\274/
$cInd < $#chars $chars[$cInd + 1] =~ /a|\303\241|o|\303\263|\303\274/
Truth table0%0/3
A and B
A= $cInd < $#charsB= $chars[$cInd + 1] =~ /a|\303\241|o|\303\263|\303\274/
ABresult
0X0
100
111
137 0 push(@phonemesToReturn, 'g');
138 } else {
139 0 push(@phonemesToReturn, 'x');
140 }
141 } elsif ($char eq 'h') {
142 # 'h' has no sound in spanish
143 } elsif ($char eq 'i') {
144 0 ▶ if (($cInd == 0 || ($cInd == 1 && $chars[0] eq 'h')) &&
Branch: 2 branches
BranchTrueFalseTotal
if ($cInd == 0 || $cInd == 1 && $chars[0] eq 'h' and ($cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/)) { } 0 0 0
elsif ($cInd > 0 and $chars[$cInd - 1] =~ /$vowels_re/ or $cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/) { } 0 0 0
Condition0%0/12
$cInd == 0 || $cInd == 1 && $chars[0] eq "h" and ($cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/)
!ll&&!rl&&r
000
$cInd == 0 or $cInd == 1 and $chars[0] eq "h"
l!l&&r!l&&!r
000
$cInd == 1 and $chars[0] eq "h"
!ll&&!rl&&r
000
$cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/
!ll&&!rl&&r
000
MC/DC0%0/5
$cInd == 0 || $cInd == 1 && $chars[0] eq "h" and ($cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/)
$cInd == 0 $cInd == 1 $chars[0] eq "h" $cInd < $#chars $chars[$cInd + 1] =~ /$vowels_re/
Truth table0%0/8
A or B and C and D and E
A= $cInd == 0B= $cInd == 1C= $chars[0] eq "h"D= $cInd < $#charsE= $chars[$cInd + 1] =~ /$vowels_re/
ABCDEresult
00XXX0
010XX0
1XX0X0
1XX100
0110X0
011100
1XX111
011111
145 ($cInd < $#chars && $chars[$cInd+1] =~ /$vowels_re/)) {
146 0 push(@phonemesToReturn, 'L');
147 ▶ } elsif (($cInd > 0 && $chars[$cInd-1] =~ /$vowels_re/) ||
Condition0%0/9
$cInd > 0 and $chars[$cInd - 1] =~ /$vowels_re/ or $cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/
l!l&&r!l&&!r
000
$cInd > 0 and $chars[$cInd - 1] =~ /$vowels_re/
!ll&&!rl&&r
000
$cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/
!ll&&!rl&&r
000
MC/DC0%0/4
$cInd > 0 and $chars[$cInd - 1] =~ /$vowels_re/ or $cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/
$cInd > 0 $chars[$cInd - 1] =~ /$vowels_re/ $cInd < $#chars $chars[$cInd + 1] =~ /$vowels_re/
Truth table0%0/7
A and B or C and D
A= $cInd > 0B= $chars[$cInd - 1] =~ /$vowels_re/C= $cInd < $#charsD= $chars[$cInd + 1] =~ /$vowels_re/
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
148 ($cInd < $#chars && $chars[$cInd+1] =~ /$vowels_re/)) {
149 0 push(@phonemesToReturn, 'j');
150 } else {
151 0 push(@phonemesToReturn, 'i');
152 }
153 } elsif ($char eq 'u') {
154 0 ▶ if (($cInd < $#chars && $chars[$cInd+1] =~ /$vowels_re/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/) { } 0 0 0
Condition0%0/3
$cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/
!ll&&!rl&&r
000
MC/DC0%0/2
$cInd < $#chars and $chars[$cInd + 1] =~ /$vowels_re/
$cInd < $#chars $chars[$cInd + 1] =~ /$vowels_re/
Truth table0%0/3
A and B
A= $cInd < $#charsB= $chars[$cInd + 1] =~ /$vowels_re/
ABresult
0X0
100
111
155 0 push(@phonemesToReturn, 'w');
156 } else {
157 0 push(@phonemesToReturn, 'u');
158 }
159 } elsif ($char eq 'w') {
160 0 push(@phonemesToReturn, 'g', 'u');
161 } elsif ($char eq 'y') {
162 0 ▶ if ($cInd == $#chars) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cInd == $#chars) { } 0 0 0
163 0 push(@phonemesToReturn, 'i');
164 } else {
165 0 push(@phonemesToReturn, 'L');
166 }
167 } else {
168 0 warn "'$char' can not be translated. Ignoring it\n";
169 }
170 }
171 0 $cInd++;
172 }
173 0 return @phonemesToReturn;
174 }
175  
176 0 ▶sub _setStressOnSyllabedWord($) {
Subroutine _setStressOnSyllabedWord: not called CC=6 SCAR=37.4
177 0 my @syllabes = @_;
178 0 my $word = join("", @syllabes);
179 0 ▶ if ($word !~ /á|é|í|ó|ú/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($word =~ /\303\241|\303\251|\303\255|\303\263|\303\272/) 0 0 0
180 0 ▶ if ($word =~ /.mente$/ && @syllabes > 2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($word =~ /.mente$/ and @syllabes > 2) { } 0 0 0
elsif ($syllabes[-1] =~ /[aeiouns]$/) { } 0 0 0
Condition0%0/3
$word =~ /.mente$/ and @syllabes > 2
!ll&&!rl&&r
000
MC/DC0%0/2
$word =~ /.mente$/ and @syllabes > 2
$word =~ /.mente$/ @syllabes > 2
Truth table0%0/3
A and B
A= $word =~ /.mente$/B= @syllabes > 2
ABresult
0X0
100
111
181 0 my $maxIndex = $#syllabes - 2;
182 0 @syllabes[0..$maxIndex] = _setStressOnSyllabedWord(
183 @syllabes[0..$maxIndex]);
184 } elsif ($syllabes[-1] =~ /[aeiouns]$/ ) { # word is 'llana' or monosyllabic
185 0 ▶ if (@syllabes == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@syllabes == 1) { } 0 0 0
186 0 $syllabes[0] = _setStressOnSyllabe($syllabes[0]);
187 } else {
188 0 $syllabes[-2] = _setStressOnSyllabe($syllabes[-2]);
189 }
190 } else { # word is 'aguda'
191 0 $syllabes[-1] = _setStressOnSyllabe($syllabes[-1]);
192 }
193 }
194 0 return @syllabes;
195 }
196  
197 0 ▶sub _setStressOnSyllabe($) {
Subroutine _setStressOnSyllabe: not called CC=13 SCAR=52.0
198 0 my $syllabe = shift;
199  
200 0 my $strongVowels_re = "a|á|e|é|o|ó";
201 0 my $softVowels_re = "i|í|u|ú|ü";
202  
203 0 my $utf8 = new String::Multibyte('UTF8');
204 0 my @chars = $utf8->strsplit('', $syllabe);
205  
206 0 my @vowelsPositions;
207  
208 0 ▶ for (my $index = 0; $index < @chars; $index++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < @chars) 0 0 0
209 0 ▶ push(@vowelsPositions, $index) if ($chars[$index] =~ /[aeiou]/);
Branch: 1 branch
BranchTrueFalseTotal
if $chars[$index] =~ /[aeiou]/ 0 0 0
210 }
211  
212 0 ▶ return $syllabe unless @vowelsPositions;
Branch: 1 branch
BranchTrueFalseTotal
unless @vowelsPositions 0 0 0
213  
214 0 my $stressPosition;
215  
216 0 ▶ if (@vowelsPositions == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@vowelsPositions == 1) { } 0 0 0
elsif (@vowelsPositions == 2) { } 0 0 0
217 0 $stressPosition = $vowelsPositions[0];
218 } elsif (@vowelsPositions == 2) { #diphthong
219 0 ▶ if ($chars[$vowelsPositions[0]] =~ /$strongVowels_re/ &&
Branch: 3 branches
BranchTrueFalseTotal
if ($chars[$vowelsPositions[0]] =~ /$strongVowels_re/ and $chars[$vowelsPositions[1]] =~ /$softVowels_re/) { } 0 0 0
elsif ($chars[$vowelsPositions[0]] =~ /$softVowels_re/) { } 0 0 0
elsif ($chars[$vowelsPositions[0]] =~ /$strongVowels_re/ and $chars[$vowelsPositions[1]] =~ /$strongVowels_re/) { } 0 0 0
Condition0%0/3
$chars[$vowelsPositions[0]] =~ /$strongVowels_re/ and $chars[$vowelsPositions[1]] =~ /$softVowels_re/
!ll&&!rl&&r
000
MC/DC0%0/2
$chars[$vowelsPositions[0]] =~ /$strongVowels_re/ and $chars[$vowelsPositions[1]] =~ /$softVowels_re/
$chars[$vowelsPositions[0]] =~ /$strongVowels_re/ $chars[$vowelsPositions[1]] =~ /$softVowels_re/
Truth table0%0/3
A and B
A= $chars[$vowelsPositions[0]] =~ /$strongVowels_re/B= $chars[$vowelsPositions[1]] =~ /$softVowels_re/
ABresult
0X0
100
111
220 $chars[$vowelsPositions[1]] =~ /$softVowels_re/) {
221 0 $stressPosition = $vowelsPositions[0];
222 } elsif ($chars[$vowelsPositions[0]] =~ /$softVowels_re/) {
223 0 $stressPosition = $vowelsPositions[1];
224 ▶ } elsif ($chars[$vowelsPositions[0]] =~ /$strongVowels_re/ &&
Condition0%0/3
$chars[$vowelsPositions[0]] =~ /$strongVowels_re/ and $chars[$vowelsPositions[1]] =~ /$strongVowels_re/
!ll&&!rl&&r
000
MC/DC0%0/2
$chars[$vowelsPositions[0]] =~ /$strongVowels_re/ and $chars[$vowelsPositions[1]] =~ /$strongVowels_re/
$chars[$vowelsPositions[0]] =~ /$strongVowels_re/ $chars[$vowelsPositions[1]] =~ /$strongVowels_re/
Truth table0%0/3
A and B
A= $chars[$vowelsPositions[0]] =~ /$strongVowels_re/B= $chars[$vowelsPositions[1]] =~ /$strongVowels_re/
ABresult
0X0
100
111
225 $chars[$vowelsPositions[1]] =~ /$strongVowels_re/) {
226 0 $stressPosition = $vowelsPositions[1];
227 } else {
228 0 warn "Can not determine stressed vowel for '$syllabe'\n";
229 }
230 } else {
231 0 foreach (@vowelsPositions) {
232 0 ▶ if ($chars[$_] =~ /$strongVowels_re/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chars[$_] =~ /$strongVowels_re/) 0 0 0
233 0 $stressPosition = $_;
234 0 last;
235 }
236 }
237 }
238  
239 0 $chars[$stressPosition] =~ s/a/á/;
240 0 $chars[$stressPosition] =~ s/e/é/;
241 0 $chars[$stressPosition] =~ s/i/í/;
242 0 $chars[$stressPosition] =~ s/o/ó/;
243 0 $chars[$stressPosition] =~ s/u/ú/;
244  
245 0 return join("", @chars);
246 }
247  
248 # Preloaded methods go here.
249 1 1;
250 __END__
↑ Summary