File Coverage

blib/lib/Wiktionary/Parser/Section/Pronunciation.pm
Criterion Covered Total %
statement 71 85 83.5
branch 18 28 64.2
condition 7 12 58.3
subroutine 10 15 66.6
pod 0 11 0.0
total 106 151 70.2


line stmt bran cond sub pod time code
1             package Wiktionary::Parser::Section::Pronunciation;
2              
3 3     3   23 use Wiktionary::Parser::Section;
  3         6  
  3         135  
4 3     3   2101 use Wiktionary::Parser::Section::Pronunciation::Audio;
  3         9  
  3         95  
5 3     3   2810 use Wiktionary::Parser::Section::Pronunciation::Representation;
  3         12  
  3         113  
6              
7 3     3   20 use base qw(Wiktionary::Parser::Section);
  3         9  
  3         5966  
8              
9             sub new {
10 4     4 0 6 my $class = shift;
11 4         16 my %args = @_;
12 4         19 my $self = bless Wiktionary::Parser::Section->new(%args), $class;
13 4         17 return $self;
14             }
15              
16              
17             # add a line of content to this section and parse it into its component parts
18             sub add_content {
19 14     14 0 22 my $self = shift;
20 14         21 my $line = shift;
21              
22 14         16 push @{$self->{content}}, $line;
  14         36  
23              
24 14 50       28 return unless $line;
25 14         56 $line =~ s/^[\*\s]+//i;
26 14         94 my @meta = $line =~ m/\{\{([^\}]+)\}\}/g;
27 14 50       39 if (@meta) {
28 14         16 my @context;
29             my @senses;
30 14         20 for my $meta (@meta) {
31 22         74 my @parts = split(/\|/,$meta);
32              
33 22 50       136 if ($parts[0] eq 'sense') {
    100          
    100          
    100          
34 0 0       0 if (@parts > 1) {
35 0 0       0 push @senses, grep {$_ && $_ ne 'sense'} @parts
  0         0  
36             }
37             } elsif ($parts[0] eq 'a') {
38 3 50       9 if (@parts > 1) {
39 3 50       7 push @context, grep {$_ && $_ ne 'a'} @parts
  6         35  
40             }
41             } elsif ($parts[0] eq 'audio') {
42 5         18 my $lang = $self->get_language();
43            
44 5         23 $self->add_audio(
45             language => $lang,
46             file => $parts[1],
47             text => $parts[2],
48             context => \@context,
49             senses => \@senses,
50             );
51             } elsif ($parts[0] =~ m/(rhyme|homophone|hyphenation)/) {
52            
53 4         10 my $meta = $self->parse_template(@parts);
54 4   66     18 $meta->{lang} ||= $self->get_language();
55              
56 4         23 $self->add_category(
57             category => $1,
58             language => $meta->{lang},
59             representation => $meta->{representation},
60             pronunciation => $meta->{pronunciation},
61             context => \@context,
62             senses => \@senses,
63             );
64              
65             } else {
66 10         31 my $meta = $self->parse_template(@parts);
67 10   66     60 $meta->{lang} ||= $self->get_language();
68              
69 10         45 $self->add_pronunciation(
70             language => $meta->{lang},
71             representation => $meta->{representation},
72             pronunciation => $meta->{pronunciation},
73             context => \@context,
74             senses => \@senses,
75             );
76             }
77             }
78             }
79             }
80              
81             sub parse_template {
82 14     14 0 20 my $self = shift;
83 14         29 my @parts = @_;
84              
85 14         17 my %meta;
86             my $representation;
87 0         0 my $lang;
88 14         27 my @pronunciation = [];
89 14         35 for my $i (0..$#parts) {
90 37 100       236 if ($parts[$i] =~ m/rhyme|homophone|hyphenation/) {
    100          
    100          
91            
92             } elsif ($parts[$i] =~ m/(IPA|enPR|AHD|SAMPA)/) {
93 10         25 $meta{representation} = $parts[$i];
94             } elsif ($parts[$i] =~ m/lang=(.+)/) {
95 4         17 $meta{lang} = $1;
96             } else {
97 19         20 push @{$meta{pronunciation}}, $parts[$i];
  19         70  
98             }
99             }
100 14         49 return \%meta;
101             }
102              
103             sub add_pronunciation {
104 10     10 0 16 my $self = shift;
105 10         40 my %args = @_;
106              
107 10         19 my $meta = {};
108 10   50     29 my $lang = $args{language} || '__language_undefined__';
109              
110 10         65 my $pronunciation = Wiktionary::Parser::Section::Pronunciation::Representation->new(
111             representation => $args{representation},
112             pronunciation => $args{pronunciation},
113             context => $args{context},
114             );
115 10         15 push @{$self->{pronunciation}{$lang}},$pronunciation;
  10         90  
116             }
117              
118              
119             # rhyme, homophone, hyphenation entries
120             sub add_category {
121 4     4 0 17 my $self = shift;
122 4         30 my %args = @_;
123 4 50       14 my $category = $args{category} or die 'category not defined';
124              
125 4   50     11 my $lang = $args{language} || '__language_undefined__';
126 4         8 my $meta = {};
127 4         19 my $item = Wiktionary::Parser::Section::Pronunciation::Representation->new(
128             representation => $args{representation},
129             pronunciation => $args{pronunciation},
130             context => $args{context},
131             senses => $args{senses},
132             );
133 4         6 push @{$self->{$category}{$lang}},$item;
  4         54  
134             }
135              
136              
137             sub add_audio {
138 5     5 0 9 my $self = shift;
139 5         25 my %args = @_;
140 5   50     16 my $lang = $args{language} || '__language_undefined__';
141              
142 5         38 my $audio = Wiktionary::Parser::Section::Pronunciation::Audio->new(
143             file => $args{file},
144             text => $args{text},
145             context => $args{context},
146             );
147              
148 5         8 push @{$self->{audio}{$lang}}, $audio;
  5         49  
149             }
150              
151             sub get_pronunciations {
152 0     0 0   my $self = shift;
153 0           return $self->{pronunciation};
154             }
155              
156             sub get_audio {
157 0     0 0   my $self = shift;
158 0           return $self->{audio};
159             }
160              
161             sub get_rhymes {
162 0     0 0   my $self = shift;
163 0           return $self->{rhyme}
164             }
165              
166             sub get_homophones{
167 0     0 0   my $self = shift;
168 0           return $self->{homophone}
169             }
170              
171             sub get_hyphenations{
172 0     0 0   my $self = shift;
173 0           return $self->{hyphenation}
174             }
175              
176             1;