Pronounce/Pronouncer.pm

filter: Statementstmt 82.1% 60 / 73 Branchbran 41.6% 10 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 4 totaltotal 64.5% 82 / 127 SCAR 32.5
CC
17
Cov
69%
pronounce_phrase
33.8
new
3.5
lookup
2.0
CRAP 25.7
« Sense.pm ↑ Summary Serialise/Simple.pm »
1 package WordLists::Pronounce::Pronouncer;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use WordLists::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 our $VERSION = $WordLists::Base::VERSION;
7
8 sub new
9 {
10 1 ▶ my ($class, $args) = @_;
Subroutine new: called CC=3 SCAR=12.6
Pod undocumented
11 1 ▶ $args = {} unless defined $args;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args 0 1 1
12 my $self = {
13 cleaner => \&_default_clean,
14 cb_word => undef,
15 cb_phrase => undef,
16 0 ▶ cb_fail => sub{warn "$_[1] not found\n"; return 'X';},
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 1 %{$args}
18 };
19 1 ▶ unless (defined $self->{'lookup'})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"lookup"}) 0 1 1
20 {
21 0 warn ('No lookup set for '.$class);
22
23 #
24 }
25 1 bless $self, $class;
26 }
27 sub lookup
28 {
29 4 ▶ my ($self, $new) = @_;
Subroutine lookup: called CC=2 SCAR=7.1
Pod undocumented
30 4 ▶ $self->{'lookup'} = $new if defined $new;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new 0 4 4
31 4 return $self->{'lookup'};
32 }
33 sub clean
34 {
35 4 ▶ my ($self, $word, $args) = @_;
Subroutine clean: called CC=1 SCAR=0.0
Pod undocumented
36 4 &{$self->{'cleaner'}}($self,$word,$args);
37 }
38 sub _default_clean
39 {
40 4 ▶ my ($self, $word, $args) = @_;
Subroutine _default_clean: called CC=1 SCAR=0.0
41 4 $word =~ s/\([^)]*\)//g;
42 4 $word =~ s/^\*//;
43 4 $word =~ s/^\s+//;
44 4 $word =~ s/\s+$//;
45 4 $word =~ s/[’']s\b//s;
46 4 $word =~ s/s'\b/s/;
47 4 $word =~ tr{/’.?",…+;:’‘}{ }d;
48 4 $word =~ s/\s+/ /g;
49 4 $word =~ s/^'//;
50 4 $word =~ s/'$//;
51 4 $word = lc $word;
52 4 return $word;
53 }
54 sub pronounce_phrase
55 {
56 4 ▶ my ($self, $word, $args) = @_;
Subroutine pronounce_phrase: called CC=14 SCAR=35.2
Pod undocumented
57 4 my $field = $args->{'field'};
58 4 ▶ $field ||= $self->{'field'};
Condition33%1/3
$field ||= $self->{"field"}
l!l&&r!l&&!r
400
MC/DC0%0/2
$field ||= $self->{"field"}
$field $self->{"field"}
Truth table33%1/3
A ||= B
A= $fieldB= $self->{"field"}
ABresult
1X1
011
000
59 4 ▶ warn ('Source field must be defined!') unless ($field);
Branch: 1 branch
BranchTrueFalseTotal
unless $field 0 4 4
60 4 my $lookup = $self->lookup;
61 4 my $ipa="";
62 4 $word = $self->clean($word, $args);
63 4 my @senses = grep {$_->get($field)} $lookup->get_senses_for($word);
64 4 ▶ return "" if ($word eq "");
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "" 0 4 4
65 4 my @ipaout = ();
66 4 ▶ if (@senses) # if the whole input string matches
Branch: 1 branch
BranchTrueFalseTotal
if (@senses) { } 3 1 4
67 {
68 3 $ipa = $senses[0]->get($field);
69 3 push @ipaout, $ipa;
70 }
71 else # if the whole input string does not match, try splitting and pronouncing each component
72 {
73 1 my @words = split(/[- ?]+/, $word);
74 1 ▶ if ($#words > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($#words > 0) { } 1 0 1
75 {
76 1 foreach (@words)
77 {
78 2 push @ipaout, $self->pronounce_phrase($_, {%$args,(as_word=>1)});
79 }
80 }
81 else # if non-matching input string is a single word, fail.
82 {
83 0 ▶ if (defined $self->{'cb_fail'} )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"cb_fail"}) 0 0 0
84 {
85 0 ▶ if (ref ($self->{'cb_fail'}) eq ref (sub {}))
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'cb_fail'} eq ref sub { } ) { } 0 0 0
elsif (ref $self->{'cb_fail'} eq ref '') { } 0 0 0
86 {
87 0 return &{$self->{'cb_fail'}}($self, $word, $args);
88 }
89 elsif (ref ($self->{'cb_fail'}) eq ref (''))
90 {
91 0 return $self->{'cb_fail'};
92 }
93 else
94 {
95 0 return '';
96 # Don't return undef, as this will probably break string concatenation.
97 }
98 }
99 }
100 }
101 4 my $out = join(" ", @ipaout) ;
102 4 ▶ if (defined $self->{'cb_word'} and $args->{'as_word'}) # Callback for processing string as a word
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'cb_word'} and $args->{'as_word'}) { } 0 4 4
elsif (defined $self->{'cb_phrase'} and not $args->{'as_word'}) { } 0 8 8
Condition33%1/3
defined $self->{"cb_word"} and $args->{"as_word"}
!ll&&!rl&&r
400
MC/DC0%0/2
defined $self->{"cb_word"} and $args->{"as_word"}
defined $self->{"cb_word"} $args->{"as_word"}
Truth table33%1/3
A and B
A= defined $self->{"cb_word"}B= $args->{"as_word"}
ABresult
0X0
100
111
103 {
104 0 $out = &{$self->{'cb_word'}}($self, $out, $args);
105 }
106 ▶ elsif (defined $self->{'cb_phrase'} and !$args->{'as_word'}) # Callback for processing string as a phrase
Condition33%1/3
defined $self->{"cb_phrase"} and not $args->{"as_word"}
!ll&&!rl&&r
400
MC/DC0%0/2
defined $self->{"cb_phrase"} and not $args->{"as_word"}
defined $self->{"cb_phrase"} not $args->{"as_word"}
Truth table33%1/3
A and not(B)
A= defined $self->{"cb_phrase"}B= not $args->{"as_word"}
ABresult
0X0
100
111
107 {
108 0 $out = &{$self->{'cb_phrase'}}($self, $out, $args) ;
109 }
110 4 return $out;
111 }
112
113 1 return 1;
114
115 =pod
116
117 =head1 NAME
118
119 WordLists::Pronounce::Pronouncer
120
121 =head1 SYNOPSIS
122
123 my $pronouncer = WordLists::Pronounce::Pronouncer->new({ lookup => $here_is_one_i_prepared_earlier });
124 $ipa = $pronouncer->pronounce_phrase('tomato stew', {field=>'uspron'});
125
126 =head1 DESCRIPTION
127
128 Allows the user to create and configure a pronouncing object which will accept strings and have a guess at their IPA transcription, based on a pre-generated list of pronunciations.
129
130 The user must specify a lookup object containing this list.
131
132 =head1 TODO
133
134 Write and implement an API for reading several pronunciations and comparing them (possibly once the whole phrase has been put together).
135
136 Try to guess stress for compounds based on a list of compounds and/or at least making sure each word has stress. (This might be impossible to do meaningfully)
137
138 =head1 BUGS
139
140 Please use the Github issues tracker.
141
142 =head1 LICENSE
143
144 Copyright 2011-2012 © Cambridge University Press. This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
145
146 =cut
« Sense.pm ↑ Summary Serialise/Simple.pm »