Parser/Document.pm

filter: Statementstmt 42.6% 174 / 408 Branchbran 41.6% 40 / 96 Conditioncond 23.6% 22 / 93 MC/DCmcdc 7.2% 4 / 55 Subroutinesub 58.1% 32 / 55 Podpod 0.0% 0 / 38 totaltotal 36.5% 272 / 745 CC 116 SCAR 40.2
CC
116
Cov
40%
get_classifications
210.0
get_pronunciations
182.0
get_word_senses
72.0
CRAP 55.7
« Parser/Section/Classifications.pm ↑ Summary Parser/Section/Pronunciation/Audio.pm »
1 package Wiktionary::Parser::Document;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Wiktionary::Parser::Section;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Wiktionary::Parser::Section::Translations;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Wiktionary::Parser::Section::PartofSpeech;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Wiktionary::Parser::Section::Etymology;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Wiktionary::Parser::Section::Synonym;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Wiktionary::Parser::Section::Hyponym;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Wiktionary::Parser::Section::Hypernym;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Wiktionary::Parser::Section::Antonym;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Wiktionary::Parser::Section::Etymology;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Wiktionary::Parser::Section::Pronunciation;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Wiktionary::Parser::Section::DerivedTerms;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Wiktionary::Parser::Section::AlternativeForms;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Wiktionary::Parser::Section::WikisaurusSection;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Wiktionary::Parser::Language;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 21 ▶sub new {
Subroutine new: called CC=5 SCAR=16.2
Pod undocumented
22 21 my $class = shift;
23 21 my %args = @_;
24
25 21 my $sections = delete $args{sections};
26  
27 21 my $self = bless \%args, $class;
28  
29 21 ▶ $self->{verbose} ||= 0;
Condition50%1/2
$self->{"verbose"} ||= 0
l!l
021
MC/DC0%0/1
$self->{"verbose"} ||= 0
$self->{"verbose"}
Truth table50%1/2
A
A= $self->{"verbose"}
Aresult
11
00
30  
31 # if a document is instantiated with existing section objects
32 # add them one by one so that they get indexed
33 21 ▶ if ($sections && @$sections) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sections and @$sections) 20 1 21
Condition66%2/3
$sections and @$sections
!ll&&!rl&&r
1020
MC/DC50%1/2
$sections and @$sections
$sections @$sections
Truth table66%2/3
A and B
A= $sectionsB= @$sections
ABresult
0X0
100
111
34 20 for my $section (@$sections) {
35 40 $self->add_section($section);
36 }
37 }
38  
39 21 return $self;
40 }
41  
42 # return the title of this document
43 41 ▶sub get_title {
Subroutine get_title: called CC=1 SCAR=0.0
Pod undocumented
44 41 my $self = shift;
45 41 return $self->{title};
46 }
47  
48 # add a section object to the document
49 81 ▶sub add_section {
Subroutine add_section: called CC=3 SCAR=11.1
Pod undocumented
50 81 my $self = shift;
51 81 my $section = shift;
52  
53 81 ▶ unless ($section->isa('Wiktionary::Parser::Section')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($section->isa("Wiktionary::Parser::Section")) 0 81 81
54 0 die sprintf(
55 'given value (%s) is not of type Wiktionary::Parser::Section',
56 ref($section)
57 );
58 }
59  
60 # link section to document
61 81 ▶ unless ($section->get_document()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($section->get_document) 40 41 81
62 40 $section->set_document($self);
63 }
64  
65  
66 81 my $section_number = $section->get_section_number();
67 81 $self->{sections}{$section_number} = $section;
68 }
69  
70 # by default return a list of all sections
71 # if title is given, return all sections matching that title
72 # title may be a string or regex
73 0 ▶sub get_sections {
Subroutine get_sections: not called CC=4 SCAR=30.0
Pod undocumented
74 0 my $self = shift;
75 0 my %args = @_;
76 0 my $title = $args{title};
77  
78 0 ▶ if ($title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title) 0 0 0
79 0 my @sections;
80 0 for my $number ($self->get_section_numbers()) {
81
82 0 ▶ next unless $self->get_section(number => $number)->get_header() =~ m/$title/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_section("number", $number)->get_header =~ /$title/i 0 0 0
83 0 push @sections, $self->get_section(number => $number);
84 }
85 0 return \@sections;
86 }
87  
88 0 return $self->{sections};
89 }
90  
91 # given some criteria to select a set of sections
92 # return a document object encompassing only those sections
93 0 ▶sub get_sub_document {
Subroutine get_sub_document: not called CC=6 SCAR=37.4
Pod undocumented
94 0 my $self = shift;
95 0 my %args = @_;
96 0 my $title = $args{title};
97  
98 # if no section name pattern was passed in, just return the whole document
99 0 ▶ return $self unless $title;
Branch: 1 branch
BranchTrueFalseTotal
unless $title 0 0 0
100  
101 0 my $sections = $self->get_sections(title => $title);
102
103 0 ▶ return unless $sections && @$sections;
Branch: 1 branch
BranchTrueFalseTotal
unless $sections and @$sections 0 0 0
Condition0%0/3
$sections and @$sections
!ll&&!rl&&r
000
MC/DC0%0/2
$sections and @$sections
$sections @$sections
Truth table0%0/3
A and B
A= $sectionsB= @$sections
ABresult
0X0
100
111
104
105 0 my @children;
106 0 for my $section (@$sections) {
107 0 ▶ push @children, @{$section->get_child_sections() || []};
Condition0%0/2
$section->get_child_sections || []
l!l
00
MC/DC0%0/1
$section->get_child_sections || []
$section->get_child_sections
Truth table0%0/2
A
A= $section->get_child_sections
Aresult
11
00
108 }
109  
110 0 push @$sections, @children;
111 0 my $sub_document = $self->create_sub_document(
112 sections => $sections,
113 );
114  
115 0 return $sub_document;
116 }
117  
118  
119 # return a document object consisting of just the given language section and its children
120 0 ▶sub get_language_section {
Subroutine get_language_section: not called CC=5 SCAR=34.0
Pod undocumented
121 0 my $self = shift;
122 0 my %args = @_;
123 0 ▶ my $language = $args{language} or die 'language needs to be specified';
Branch: 1 branch
BranchTrueFalseTotal
my $language = $args{"language"} or die "language needs to be specified" 0 0 0
124  
125 # go through the document top to bottom and return the first matching section
126 0 my $section;
127 0 for my $number ($self->get_section_numbers()) {
128 0 ▶ next unless $self->get_section(number => $number)->get_header() =~ m/^$language$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_section("number", $number)->get_header =~ /^$language$/i 0 0 0
129 0 $section = $self->get_section(number => $number);
130 0 last;
131 }
132 0 ▶ if ($section) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section) 0 0 0
133 0 return $section->get_child_document();
134 }
135  
136 0 return;
137 }
138  
139 298 ▶sub get_section {
Subroutine get_section: called CC=2 SCAR=7.2
Pod undocumented
140 298 my $self = shift;
141 298 my %args = @_;
142 298 my $number = $args{number}; # lookup section by number
143  
144 298 ▶ if ($number) {
Branch: 1 branch
BranchTrueFalseTotal
if ($number) 298 0 298
145 298 return $self->{sections}{$number};
146 }
147 0 return;
148 }
149  
150 # act as a section factory
151 41 ▶sub create_section {
Subroutine create_section: called CC=12 SCAR=27.2
Pod undocumented
152 41 my $self = shift;
153 41 my %args = @_;
154 41 my $section_number = $args{section_number};
155 41 my $header = $args{header};
156  
157 41 my $section;
158 my $class;
159 41 ▶ if ($self->get_title() =~ m/^Wikisaurus\:/) {
Branch: 11 branches
BranchTrueFalseTotal
if ($self->get_title =~ /^Wikisaurus\:/) { } 0 41 41
elsif ($header =~ /translation/i) { } 2 39 41
elsif ($header =~ /etymology/i) { } 6 33 39
elsif ($header =~ /synonym/i) { } 0 33 33
elsif ($header =~ /hypernym/i) { } 0 33 33
elsif ($header =~ /hyponym/i) { } 0 33 33
elsif ($header =~ /antonym/i) { } 0 33 33
elsif ($header =~ /pronunciation/i) { } 4 29 33
elsif ($header =~ /alternat\w+ form/i) { } 0 29 29
elsif ($header =~ /derived\sterm/i) { } 2 27 29
elsif ($self->is_part_of_speech($header)) { } 12 15 27
160 0 $class = 'Wiktionary::Parser::Section::WikisaurusSection';
161 } elsif ($header =~ m/translation/i) {
162 2 $class = 'Wiktionary::Parser::Section::Translations';
163 } elsif ($header =~ m/etymology/i) {
164 6 $class = 'Wiktionary::Parser::Section::Etymology';
165 } elsif ($header =~ m/synonym/i) {
166 0 $class = 'Wiktionary::Parser::Section::Synonym';
167 } elsif ($header =~ m/hypernym/i) {
168 0 $class = 'Wiktionary::Parser::Section::Hypernym';
169 } elsif ($header =~ m/hyponym/i) {
170 0 $class = 'Wiktionary::Parser::Section::Hyponym';
171 } elsif ($header =~ m/antonym/i) {
172 0 $class = 'Wiktionary::Parser::Section::Antonym';
173 } elsif ($header =~ m/pronunciation/i) {
174 4 $class = 'Wiktionary::Parser::Section::Pronunciation';
175 } elsif ($header =~ m/alternat\w+ form/i) {
176 0 $class = 'Wiktionary::Parser::Section::AlternativeForms';
177 } elsif ($header =~ m/derived\sterm/i) {
178 2 $class = 'Wiktionary::Parser::Section::DerivedTerms';
179 } elsif ($self->is_part_of_speech($header)) {
180 12 $class = 'Wiktionary::Parser::Section::PartofSpeech'
181 } else {
182 15 $class = 'Wiktionary::Parser::Section';
183 }
184  
185 41 $section = $class->new(
186 section_number => $section_number,
187 header => $header,
188 document => $self,
189 );
190  
191 41 $self->add_section($section);
192  
193 41 return $section;
194 }
195  
196 1 ▶sub get_table_of_contents {
Subroutine get_table_of_contents: called CC=2 SCAR=6.9
Pod undocumented
197 1 my $self = shift;
198 1 my @contents;
199 1 for my $number ($self->get_section_numbers()) {
200 40 push @contents, sprintf("%s,%s",$number,$self->get_section(number => $number)->get_header());
201 }
202 1 return \@contents;
203 }
204  
205  
206 # grab all translation sections
207 20 ▶sub get_translation_sections {
Subroutine get_translation_sections: called CC=1 SCAR=0.0
Pod undocumented
208 20 my $self = shift;
209 20 return $self->get_sections_of_type('Wiktionary::Parser::Section::Translations');
210 }
211  
212 # grab all part of speech sections
213 0 ▶sub get_part_of_speech_sections {
Subroutine get_part_of_speech_sections: not called CC=1 SCAR=6.9
Pod undocumented
214 0 my $self = shift;
215 0 return $self->get_sections_of_type('Wiktionary::Parser::Section::PartofSpeech');
216 }
217  
218 0 ▶sub get_synonym_sections {
Subroutine get_synonym_sections: not called CC=1 SCAR=6.9
Pod undocumented
219 0 my $self = shift;
220 0 return $self->get_sections_of_type('Wiktionary::Parser::Section::Synonym');
221 }
222  
223 0 ▶sub get_hypernym_sections {
Subroutine get_hypernym_sections: not called CC=1 SCAR=6.9
Pod undocumented
224 0 my $self = shift;
225 0 return $self->get_sections_of_type('Wiktionary::Parser::Section::Hypernym');
226 }
227  
228 0 ▶sub get_hyponym_sections {
Subroutine get_hyponym_sections: not called CC=1 SCAR=6.9
Pod undocumented
229 0 my $self = shift;
230 0 return $self->get_sections_of_type('Wiktionary::Parser::Section::Hyponym');
231 }
232  
233 0 ▶sub get_pronunciation_sections {
Subroutine get_pronunciation_sections: not called CC=1 SCAR=6.9
Pod undocumented
234 0 my $self = shift;
235 0 return $self->get_sections_of_type('Wiktionary::Parser::Section::Pronunciation');
236 }
237  
238  
239 20 ▶sub get_sections_of_type {
Subroutine get_sections_of_type: called CC=3 SCAR=11.0
Pod undocumented
240 20 my $self = shift;
241 20 my $type = shift;
242 20 my @sections;
243 20 for my $number ($self->get_section_numbers()) {
244 40 ▶ next unless $self->get_section(number => $number)->isa($type);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->get_section("number", $number)->isa($type) 0 40 40
245 40 push @sections, $self->get_section(number => $number);
246 }
247 20 return \@sections;
248 }
249  
250  
251 22 ▶sub get_section_numbers {
Subroutine get_section_numbers: called CC=2 SCAR=7.0
Pod undocumented
252 22 my $self = shift;
253 22 ▶ return (sort {$a cmp $b} grep {$_} keys %{$self->{sections} || {}});
Condition50%1/2
$self->{'sections'} || {}
l!l
220
MC/DC0%0/1
$self->{'sections'} || {}
$self->{'sections'}
Truth table50%1/2
A
A= $self->{'sections'}
Aresult
11
00
254 }
255  
256  
257 0 ▶sub get_word_senses {
Subroutine get_word_senses: not called CC=8 SCAR=42.8
Pod undocumented
258 0 my $self = shift;
259 0 my %args = @_;
260  
261 # follow links to other wiktionary pages
262 0 ▶ my $_meta_follow_links = defined($args{_meta_follow_links}) ? $args{_meta_follow_links} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'_meta_follow_links'} ? : 0 0 0
263  
264  
265 0 my $sections = $self->get_translation_sections();
266 0 my @word_senses;
267 0 ▶ for my $section (@{$sections || []}) {
Condition0%0/2
$sections || []
l!l
00
MC/DC0%0/1
$sections || []
$sections
Truth table0%0/2
A
A= $sections
Aresult
11
00
268 0 my $word_senses = $section->get_word_senses();
269  
270 0 ▶ for my $word_sense (@{$word_senses || []}) {
Condition0%0/2
$word_senses || []
l!l
00
MC/DC0%0/1
$word_senses || []
$word_senses
Truth table0%0/2
A
A= $word_senses
Aresult
11
00
271  
272 0 ▶ if (my ($title) = $word_sense->get_word() =~ m/^wiktionary\:(.+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($title) = $word_sense->get_word =~ /^wiktionary\:(.+)$/i) 0 0 0
273 0 ▶ if ($_meta_follow_links) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_meta_follow_links) 0 0 0
274 # get titles to linked pages
275 # get translations from the linked document
276 0 my $linked_document = $self->get_parser()->get_document(title => $title);
277 # set _meta_follow_links to 0, to ensure we don't end up in
278 # an endless loop if pages link back to each other
279 0 my $linked_word_senses = $linked_document->get_word_senses(_meta_follow_links => 0);
280 0 push @word_senses, @{$linked_word_senses};
281 }
282  
283 0 next;
284 }
285  
286  
287 0 push @word_senses, $word_sense->get_word();
288 }
289 }
290  
291 0 return \@word_senses;
292 }
293  
294 0 ▶sub get_synonyms {
Subroutine get_synonyms: not called CC=1 SCAR=6.9
Pod undocumented
295 0 my $self = shift;
296 0 my %args = @_;
297 0 return $self->get_classifications(
298 class => 'Wiktionary::Parser::Section::Synonym',
299 );
300 }
301  
302 0 ▶sub get_hyponyms {
Subroutine get_hyponyms: not called CC=1 SCAR=6.9
Pod undocumented
303 0 my $self = shift;
304 0 my %args = @_;
305 0 return $self->get_classifications(
306 class => 'Wiktionary::Parser::Section::Hyponym',
307 );
308 }
309  
310 0 ▶sub get_hypernyms {
Subroutine get_hypernyms: not called CC=1 SCAR=6.9
Pod undocumented
311 0 my $self = shift;
312 0 my %args = @_;
313 0 return $self->get_classifications(
314 class => 'Wiktionary::Parser::Section::Hypernym',
315 );
316 }
317  
318 0 ▶sub get_antonyms {
Subroutine get_antonyms: not called CC=1 SCAR=6.9
Pod undocumented
319 0 my $self = shift;
320 0 my %args = @_;
321 0 return $self->get_classifications(
322 class => 'Wiktionary::Parser::Section::Antonym',
323 );
324 }
325  
326  
327 0 ▶sub get_classifications {
Subroutine get_classifications: not called CC=14 SCAR=53.5
Pod undocumented
328 0 my $self = shift;
329 0 my %args = @_;
330 0 my $class = $args{class};
331  
332 0 ▶ if ($self->{"__get_${class}__"}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$self{"__get_${class}__"}) 0 0 0
333 0 return $self->{"__get_${class}__"};
334 }
335  
336 0 my $sections = $self->get_sections_of_type($class);
337 0 my %x_nyms;
338  
339 0 ▶ for my $section (@{$sections || []}) {
Condition0%0/2
$sections || []
l!l
00
MC/DC0%0/1
$sections || []
$sections
Truth table0%0/2
A
A= $sections
Aresult
11
00
340 0 my $x_nyms = $section->get_groups();
341 0 ▶ for my $x_nym (@{$x_nyms || []}) {
Condition0%0/2
$x_nyms || []
l!l
00
MC/DC0%0/1
$x_nyms || []
$x_nyms
Truth table0%0/2
A
A= $x_nyms
Aresult
11
00
342  
343 0 my $lang = $x_nym->{language};
344 0 my $sense = $x_nym->{sense};
345  
346 0 ▶ my @lexemes = @{$x_nym->{lexemes} || []};
Condition0%0/2
$x_nym->{'lexemes'} || []
l!l
00
MC/DC0%0/1
$x_nym->{'lexemes'} || []
$x_nym->{'lexemes'}
Truth table0%0/2
A
A= $x_nym->{'lexemes'}
Aresult
11
00
347  
348  
349 0 my @full_word_list;
350 0 ▶ while (my $lexeme = shift @lexemes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $lexeme = shift @lexemes) 0 0 0
351  
352  
353 # look for links to wikisaurus entries
354 # and include content from those documents
355
356 0 ▶ if ($lexeme =~ m/^Wikisaurus:/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lexeme =~ /^Wikisaurus:/) { } 0 0 0
357 0 my $wikisaurus_document = $self->get_linked_document(title => $lexeme);
358 0 my $ws_sections = $wikisaurus_document->get_sections(title => $section->get_header());
359  
360 0 ▶ for my $ws_section (@{$ws_sections || []}) {
Condition0%0/2
$ws_sections || []
l!l
00
MC/DC0%0/1
$ws_sections || []
$ws_sections
Truth table0%0/2
A
A= $ws_sections
Aresult
11
00
361 0 my $word_list = $ws_section->get_words();
362 0 ▶ for my $word (@{$word_list || []}) {
Condition0%0/2
$word_list || []
l!l
00
MC/DC0%0/1
$word_list || []
$word_list
Truth table0%0/2
A
A= $word_list
Aresult
11
00
363 0 push @full_word_list, $word->{word};
364 }
365 }
366 } else {
367 0 push @full_word_list, $lexeme;
368 }
369 }
370  
371 0 push @{$x_nyms{$lang}{sense}{$sense}}, @full_word_list;
372 0 ▶ $x_nyms{$lang}{language} ||= $self->get_language_mapper()->code2language($lang);
Condition0%0/3
$x_nyms{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
$x_nyms{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
$x_nyms{$lang}{"language"} $self->get_language_mapper->code2language($lang)
Truth table0%0/3
A ||= B
A= $x_nyms{$lang}{"language"}B= $self->get_language_mapper->code2language($lang)
ABresult
1X1
011
000
373  
374 }
375 }
376  
377 0 $self->{"__get_${class}__"} = \%x_nyms;
378  
379 0 return \%x_nyms;
380 }
381  
382 # return lists of words from the Derived Terms sections broken down by language
383 0 ▶sub get_derived_terms {
Subroutine get_derived_terms: not called CC=5 SCAR=34.0
Pod undocumented
384 0 my $self = shift;
385 0 my $class = 'Wiktionary::Parser::Section::DerivedTerms';
386 0 my $sections = $self->get_sections_of_type($class);
387 0 my %terms;
388 0 ▶ for my $section (@{$sections || []}) {
Condition0%0/2
$sections || []
l!l
00
MC/DC0%0/1
$sections || []
$sections
Truth table0%0/2
A
A= $sections
Aresult
11
00
389 0 my $hr = $section->get_derived_terms();
390 0 for my $language (keys %{$hr}) {
391 0 ▶ push @{$terms{$language}}, @{$hr->{$language} || []}
Condition0%0/2
$hr->{$language} || []
l!l
00
MC/DC0%0/1
$hr->{$language} || []
$hr->{$language}
Truth table0%0/2
A
A= $hr->{$language}
Aresult
11
00
392 }
393 }
394 0 return \%terms;
395 }
396  
397 # return all pronunciation metadata broken down by language
398 0 ▶sub get_pronunciations {
Subroutine get_pronunciations: not called CC=13 SCAR=52.0
Pod undocumented
399 0 my $self = shift;
400 0 my %args = @_;
401 0 my $class = 'Wiktionary::Parser::Section::Pronunciation';
402  
403 0 my $sections = $self->get_sections_of_type($class);
404 0 my %meta;
405 my %seen;
406  
407 0 ▶ for my $section (@{$sections || []}) {
Condition0%0/2
$sections || []
l!l
00
MC/DC0%0/1
$sections || []
$sections
Truth table0%0/2
A
A= $sections
Aresult
11
00
408  
409 {
410 0 my $hr = $section->get_pronunciations();
411 0 for my $lang (keys %{$hr}) {
412 0 push @{$meta{$lang}{pronunciation}}, @{$hr->{$lang}};
413 0 ▶ $meta{$lang}{language} ||= $self->get_language_mapper()->code2language($lang);
Condition0%0/3
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
$meta{$lang}{"language"} $self->get_language_mapper->code2language($lang)
Truth table0%0/3
A ||= B
A= $meta{$lang}{"language"}B= $self->get_language_mapper->code2language($lang)
ABresult
1X1
011
000
414 }
415 }
416  
417 {
418 0 my $hr = $section->get_audio();
419 0 for my $lang (keys %{$hr}) {
420 # remove duplicates
421 0 push @{$meta{$lang}{audio}}, grep {!$seen{audio}{$lang}{ $_->{file} }++} @{$hr->{$lang}};
422 0 ▶ $meta{$lang}{language} ||= $self->get_language_mapper()->code2language($lang);
Condition0%0/3
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
$meta{$lang}{"language"} $self->get_language_mapper->code2language($lang)
Truth table0%0/3
A ||= B
A= $meta{$lang}{"language"}B= $self->get_language_mapper->code2language($lang)
ABresult
1X1
011
000
423 }
424 }
425  
426 {
427 0 my $hr = $section->get_rhymes();
428 0 for my $lang (keys %{$hr}) {
429 0 push @{$meta{$lang}{rhyme}}, @{$hr->{$lang}};
430 0 ▶ $meta{$lang}{language} ||= $self->get_language_mapper()->code2language($lang);
Condition0%0/3
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
$meta{$lang}{"language"} $self->get_language_mapper->code2language($lang)
Truth table0%0/3
A ||= B
A= $meta{$lang}{"language"}B= $self->get_language_mapper->code2language($lang)
ABresult
1X1
011
000
431 }
432 }
433  
434 {
435 0 my $hr = $section->get_homophones();
436 0 for my $lang (keys %{$hr}) {
437 0 push @{$meta{$lang}{homophone}}, @{$hr->{$lang}};
438 0 ▶ $meta{$lang}{language} ||= $self->get_language_mapper()->code2language($lang);
Condition0%0/3
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
$meta{$lang}{"language"} $self->get_language_mapper->code2language($lang)
Truth table0%0/3
A ||= B
A= $meta{$lang}{"language"}B= $self->get_language_mapper->code2language($lang)
ABresult
1X1
011
000
439 }
440 }
441  
442 {
443 0 my $hr = $section->get_hyphenations();
444 0 for my $lang (keys %{$hr}) {
445 0 push @{$meta{$lang}{hyphenation}}, @{$hr->{$lang}};
446 0 ▶ $meta{$lang}{language} ||= $self->get_language_mapper()->code2language($lang);
Condition0%0/3
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
l!l&&r!l&&!r
000
MC/DC0%0/2
$meta{$lang}{"language"} ||= $self->get_language_mapper->code2language($lang)
$meta{$lang}{"language"} $self->get_language_mapper->code2language($lang)
Truth table0%0/3
A ||= B
A= $meta{$lang}{"language"}B= $self->get_language_mapper->code2language($lang)
ABresult
1X1
011
000
447 }
448 }
449  
450  
451 }
452  
453 0 return \%meta;
454 }
455  
456  
457  
458 0 ▶sub get_parts_of_speech {
Subroutine get_parts_of_speech: not called CC=7 SCAR=40.3
Pod undocumented
459 0 my $self = shift;
460 0 my %args = @_;
461  
462 0 ▶ if ($self->{__get_parts_of_speech__}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"__get_parts_of_speech__"}) 0 0 0
463 0 return $self->{__get_parts_of_speech__};
464 }
465  
466 0 my $sections = $self->get_part_of_speech_sections();
467  
468 0 my %parts_of_speech;
469 0 ▶ for my $section (@{$sections || []}) {
Condition0%0/2
$sections || []
l!l
00
MC/DC0%0/1
$sections || []
$sections
Truth table0%0/2
A
A= $sections
Aresult
11
00
470 0 my $pos = $section->get_part_of_speech();
471 0 my $lang_code = $section->get_language_code();
472 0 ▶ next unless $pos && $lang_code;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos and $lang_code 0 0 0
Condition0%0/3
$pos and $lang_code
!ll&&!rl&&r
000
MC/DC0%0/2
$pos and $lang_code
$pos $lang_code
Truth table0%0/3
A and B
A= $posB= $lang_code
ABresult
0X0
100
111
473 0 push @{$parts_of_speech{$lang_code}{part_of_speech}}, $pos;
474 0 ▶ $parts_of_speech{$lang_code}{language} ||= get_language_mapper()->code2language($lang_code);
Condition0%0/3
$parts_of_speech{$lang_code}{"language"} ||= get_language_mapper()->code2language($lang_code)
l!l&&r!l&&!r
000
MC/DC0%0/2
$parts_of_speech{$lang_code}{"language"} ||= get_language_mapper()->code2language($lang_code)
$parts_of_speech{$lang_code}{"language"} get_language_mapper()->code2language($lang_code)
Truth table0%0/3
A ||= B
A= $parts_of_speech{$lang_code}{"language"}B= get_language_mapper()->code2language($lang_code)
ABresult
1X1
011
000
475  
476 }
477  
478 0 $self->{__get_parts_of_speech__} = \%parts_of_speech;
479 0 return \%parts_of_speech;
480 }
481  
482  
483 20 ▶sub get_translations {
Subroutine get_translations: called CC=31 SCAR=42.4
Pod undocumented
484 20 my $self = shift;
485 20 my %args = @_;
486 20 ▶ my $include_transliterations = defined($args{include_transliterations}) ? $args{include_transliterations} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'include_transliterations'} ? : 1 19 20
487 20 ▶ my $include_alternate_translations = defined($args{include_alternate_translations}) ? $args{include_alternate_translations} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'include_alternate_translations'} ? : 0 20 20
488  
489 # follow links to other wiktionary pages
490 20 ▶ my $_meta_follow_links = defined($args{_meta_follow_links}) ? $args{_meta_follow_links} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'_meta_follow_links'} ? : 0 20 20
491  
492 20 my $sections = $self->get_translation_sections();
493 20 my @word_senses;
494 my %translations;
495  
496 20 ▶ for my $section (@{$sections || []}) {
Condition50%1/2
$sections || []
l!l
200
MC/DC0%0/1
$sections || []
$sections
Truth table50%1/2
A
A= $sections
Aresult
11
00
497 40 my $word_senses = $section->get_word_senses();
498  
499 40 ▶ for my $word_sense (@{$word_senses || []}) {
Condition50%1/2
$word_senses || []
l!l
400
MC/DC0%0/1
$word_senses || []
$word_senses
Truth table50%1/2
A
A= $word_senses
Aresult
11
00
500 20 my $word_sense_lexeme = $word_sense->get_word();
501 20 my $translations = $word_sense->get_translations();
502  
503 # if we have a link to another page, download that page and merge its translation data
504 20 ▶ if (my ($title) = $word_sense->get_word() =~ m/^wiktionary\:(.+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($title) = $word_sense->get_word =~ /^wiktionary\:(.+)$/i) 0 20 20
505
506 0 ▶ if ($_meta_follow_links) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_meta_follow_links) 0 0 0
507 # get titles to linked pages
508 # get translations from the linked document
509  
510 0 my $linked_document = $self->get_parser()->get_document(title => $title);
511 # set _meta_follow_links to 0, to ensure we don't end up in
512 # an endless loop if pages link back to each other
513  
514 0 my $linked_translations = {};
515 0 ▶ if ($linked_document) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linked_document) 0 0 0
516 0 $linked_translations = $linked_document->get_translations(_meta_follow_links => 0);
517 }
518  
519 0 for my $linked_word_sense (keys %$linked_translations) {
520 0 ▶ for my $linked_lang_code (keys %{ $linked_translations->{$linked_word_sense} || {} }) {
Condition0%0/2
$linked_translations->{$linked_word_sense} || {}
l!l
00
MC/DC0%0/1
$linked_translations->{$linked_word_sense} || {}
$linked_translations->{$linked_word_sense}
Truth table0%0/2
A
A= $linked_translations->{$linked_word_sense}
Aresult
11
00
521 0 $translations{$linked_word_sense}{$linked_lang_code}{language} = $linked_translations->{$linked_word_sense}{$linked_lang_code}{language};
522 0 ▶ push @{ $translations{$linked_word_sense}{$linked_lang_code}{translations} }, @{ $linked_translations->{$linked_word_sense}{$linked_lang_code}{translations} || []};
Condition0%0/2
$linked_translations->{$linked_word_sense}{$linked_lang_code}{'translations'} || []
l!l
00
MC/DC0%0/1
$linked_translations->{$linked_word_sense}{$linked_lang_code}{'translations'} || []
$linked_translations->{$linked_word_sense}{$linked_lang_code}{'translations'}
Truth table0%0/2
A
A= $linked_translations->{$linked_word_sense}{$linked_lang_code}{'translations'}
Aresult
11
00
523 }
524 }
525 }
526  
527 0 next;
528 }
529  
530  
531  
532 20 ▶ for my $language (keys %{$translations || {}}) {
Condition50%1/2
$translations || {}
l!l
200
MC/DC0%0/1
$translations || {}
$translations
Truth table50%1/2
A
A= $translations
Aresult
11
00
533  
534 20 my $language_code = $self->get_language_mapper()->language2code($language);
535 20 my $normalized_language = $self->get_language_mapper()->code2language($language_code);
536  
537 20 my $lexemes = $translations->{$language};
538 20 my %seen;
539 20 for my $lexeme (@{$lexemes}) {
540 37 my @translations = $lexeme->get_translations();
541  
542 # if the lexeme has a language code, use that to determine language
543 37 my $tagged_language_code = $lexeme->get_language_code();
544 37 ▶ if ($tagged_language_code) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagged_language_code) 25 12 37
545 25 ▶ $normalized_language = $self->get_language_mapper()->code2language($tagged_language_code) || $normalized_language;
Condition66%2/3
$self->get_language_mapper->code2language($tagged_language_code) || $normalized_language
l!l&&r!l&&!r
2230
MC/DC0%0/2
$self->get_language_mapper->code2language($tagged_language_code) || $normalized_language
$self->get_language_mapper->code2language($tagged_language_code) $normalized_language
Truth table66%2/3
A || B
A= $self->get_language_mapper->code2language($tagged_language_code)B= $normalized_language
ABresult
1X1
011
000
546 25 ▶ $language_code = $self->get_language_mapper()->language2code($normalized_language) || $tagged_language_code;
Condition33%1/3
$self->get_language_mapper->language2code($normalized_language) || $tagged_language_code
l!l&&r!l&&!r
2500
MC/DC0%0/2
$self->get_language_mapper->language2code($normalized_language) || $tagged_language_code
$self->get_language_mapper->language2code($normalized_language) $tagged_language_code
Truth table33%1/3
A || B
A= $self->get_language_mapper->language2code($normalized_language)B= $tagged_language_code
ABresult
1X1
011
000
547 }
548  
549  
550 37 my $part_of_speech = $section->get_part_of_speech();
551
552  
553 37 ▶ if ($include_transliterations && $lexeme->get_transliteration()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_transliterations and $lexeme->get_transliteration) 14 23 37
Condition100%3/3
$include_transliterations and $lexeme->get_transliteration
!ll&&!rl&&r
41914
MC/DC100%2/2
$include_transliterations and $lexeme->get_transliteration
$include_transliterations $lexeme->get_transliteration
Truth table100%3/3
A and B
A= $include_transliterationsB= $lexeme->get_transliteration
ABresult
0X0
100
111
554 14 push @translations, $lexeme->get_transliteration();
555 }
556 37 ▶ if ($include_alternate_translations && $lexeme->get_alternate()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include_alternate_translations and $lexeme->get_alternate) 0 37 37
Condition33%1/3
$include_alternate_translations and $lexeme->get_alternate
!ll&&!rl&&r
0370
MC/DC0%0/2
$include_alternate_translations and $lexeme->get_alternate
$include_alternate_translations $lexeme->get_alternate
Truth table33%1/3
A and B
A= $include_alternate_translationsB= $lexeme->get_alternate
ABresult
0X0
100
111
557 0 push @translations, $lexeme->get_alternate();
558 }
559  
560 37 for my $lex (sort @translations) {
561  
562 107 ▶ next unless defined $lex;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lex 0 107 107
563 107 ▶ unless (grep {$_ eq $lex} @{$translations{$word_sense_lexeme}{$language_code}{translations} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $lex;} @{$translations{$word_sense_lexeme}{$language_code}{"translations"} or [];}) 48 59 107
Condition100%2/2
$translations{$word_sense_lexeme}{$language_code}{'translations'} || []
l!l
8819
MC/DC100%1/1
$translations{$word_sense_lexeme}{$language_code}{'translations'} || []
$translations{$word_sense_lexeme}{$language_code}{'translations'}
Truth table100%2/2
A
A= $translations{$word_sense_lexeme}{$language_code}{'translations'}
Aresult
11
00
564 48 push @{$translations{$word_sense_lexeme}{$language_code}{translations}},$lex;
565 48 ▶ $translations{$word_sense_lexeme}{$language_code}{language} ||= $normalized_language;
Condition66%2/3
$translations{$word_sense_lexeme}{$language_code}{"language"} ||= $normalized_language
l!l&&r!l&&!r
29190
MC/DC0%0/2
$translations{$word_sense_lexeme}{$language_code}{"language"} ||= $normalized_language
$translations{$word_sense_lexeme}{$language_code}{"language"} $normalized_language
Truth table66%2/3
A ||= B
A= $translations{$word_sense_lexeme}{$language_code}{"language"}B= $normalized_language
ABresult
1X1
011
000
566 48 ▶ $translations{$word_sense_lexeme}{$language_code}{part_of_speech} ||= $part_of_speech;
Condition66%2/3
$translations{$word_sense_lexeme}{$language_code}{"part_of_speech"} ||= $part_of_speech
l!l&&r!l&&!r
29190
MC/DC0%0/2
$translations{$word_sense_lexeme}{$language_code}{"part_of_speech"} ||= $part_of_speech
$translations{$word_sense_lexeme}{$language_code}{"part_of_speech"} $part_of_speech
Truth table66%2/3
A ||= B
A= $translations{$word_sense_lexeme}{$language_code}{"part_of_speech"}B= $part_of_speech
ABresult
1X1
011
000
567  
568 }
569 }
570 }
571 }
572 }
573 }
574  
575 20 return \%translations;
576 }
577  
578 67 ▶sub is_part_of_speech {
Subroutine is_part_of_speech: called CC=2 SCAR=6.9
Pod undocumented
579 67 my $self = shift;
580 67 my $header = shift;
581 67 ▶ return 1 if grep { $header =~ m/^$_$/i } qw(
Branch: 1 branch
BranchTrueFalseTotal
if grep {$header =~ /^$_$/i;} "noun", "verb", "adjective", "adverb", "pronoun", "preposition", "article", "conjunction", "determiner", "interjection", "symbol" 32 35 67
582 noun
583 verb
584 adjective
585 adverb
586 pronoun
587 preposition
588 article
589 conjunction
590 determiner
591 interjection
592 symbol
593 );
594  
595 35 return 0;
596 }
597  
598  
599 # call the parser to download a page for a term in this document
600 0 ▶sub get_linked_document {
Subroutine get_linked_document: not called CC=4 SCAR=30.0
Pod undocumented
601 0 my $self = shift;
602 0 my %args = @_;
603 0 my $title = $args{title};
604  
605 0 ▶ $self->{linked_documents} ||= {};
Condition0%0/2
$self->{"linked_documents"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"linked_documents"} ||= {}
$self->{"linked_documents"}
Truth table0%0/2
A
A= $self->{"linked_documents"}
Aresult
11
00
606 0 ▶ if ($self->{linked_documents}{$title}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"linked_documents"}{$title}) 0 0 0
607 0 return $self->{linked_documents}{$title};
608 }
609  
610 0 $self->debug("Getting Linked Page: $title");
611  
612 0 my $parser = $self->get_parser();
613 0 ▶ return unless $parser;
Branch: 1 branch
BranchTrueFalseTotal
unless $parser 0 0 0
614  
615 0 $self->{linked_documents}{$title} = $parser->get_document(title => $title);
616 0 return $self->{linked_documents}{$title};
617 }
618  
619 0 ▶sub get_parser {
Subroutine get_parser: not called CC=1 SCAR=6.9
Pod undocumented
620 0 my $self = shift;
621 0 return $self->{parser};
622 }
623  
624 90 ▶sub get_language_mapper {
Subroutine get_language_mapper: called CC=2 SCAR=7.1
Pod undocumented
625 90 my $self = shift;
626 90 ▶ return $self->{language_map} ||= Wiktionary::Parser::Language->new();
Condition66%2/3
$self->{"language_map"} ||= "Wiktionary::Parser::Language"->new
l!l&&r!l&&!r
70200
MC/DC0%0/2
$self->{"language_map"} ||= "Wiktionary::Parser::Language"->new
$self->{"language_map"} "Wiktionary::Parser::Language"->new
Truth table66%2/3
A ||= B
A= $self->{"language_map"}B= "Wiktionary::Parser::Language"->new
ABresult
1X1
011
000
627 }
628  
629  
630 # create a document object with a subset of sections
631 0 ▶sub create_sub_document {
Subroutine create_sub_document: not called CC=2 SCAR=17.9
Pod undocumented
632 0 my $self = shift;
633 0 my %args = @_;
634 0 ▶ my $sections = $args{sections} or die 'sections must be defined';
Branch: 1 branch
BranchTrueFalseTotal
my $sections = $args{"sections"} or die "sections must be defined" 0 0 0
635 0 return __PACKAGE__->new( sections => $sections, title => $self->get_title() );
636  
637 }
638  
639 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
640 0 my $self = shift;
641 0 ▶ return unless $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"verbose"} 0 0 0
642 0 local $\ = "\n";
643 0 local $, = ' ';
644 0 print 'DEBUG:',@_;
645 }
646  
647  
648 17 ▶sub add_category {
Subroutine add_category: called CC=1 SCAR=0.0
Pod undocumented
649 17 my $self = shift;
650 17 my %args = @_;
651 17 my $category = $args{category};
652 17 push @{$self->{categories}},$category;
653 }
654  
655 51 ▶sub add_language_link {
Subroutine add_language_link: called CC=1 SCAR=0.0
Pod undocumented
656 51 my $self = shift;
657 51 my %args = @_;
658 51 my $tag = $args{tag};
659 51 push @{$self->{language_links}},$tag;
660 }
661  
662 0 ▶sub get_language_links {
Subroutine get_language_links: not called CC=1 SCAR=6.9
Pod undocumented
663 0 my $self = shift;
664 0 return $self->{language_links};
665 }
666  
667 0 ▶sub get_categories {
Subroutine get_categories: not called CC=1 SCAR=6.9
Pod undocumented
668 0 my $self = shift;
669 0 return $self->{categories};
670 }
671  
672 # get the languages represented by sections in this document
673 1 ▶sub get_section_languages {
Subroutine get_section_languages: called CC=3 SCAR=11.0
Pod undocumented
674 1 my $self = shift;
675 1 my @languages;
676 # get top level sections
677 1 for my $number ($self->get_section_numbers()) {
678 40 ▶ next if $number =~ m/\./;
Branch: 1 branch
BranchTrueFalseTotal
if $number =~ /\./ 33 7 40
679 7 push @languages, $self->get_section(number => $number)->get_header();
680 }
681 1 return \@languages;
682 }
683  
684 3 1;
« Parser/Section/Classifications.pm ↑ Summary Parser/Section/Pronunciation/Audio.pm »