Parser/Section/Translations.pm

filter: Statementstmt 84.7% 139 / 164 Branchbran 77.5% 45 / 58 Conditioncond 65.0% 13 / 20 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 92.3% 24 / 26 Podpod 0.0% 0 / 18 totaltotal 76.1% 227 / 298 CC 41 SCAR 25.8
CC
41
Cov
81%
template_trans_see
42.0
add_content
22.3
template_t
9.1
CRAP 13.2
« Parser/Section/WikisaurusSection.pm ↑ Summary Parser/Section/Pronunciation.pm »
1 package Wiktionary::Parser::Section::Translations;
2  
3 3 ▶use Wiktionary::Parser::Section;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Wiktionary::Parser::Section::Translations::WordSense;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Wiktionary::Parser::Section::Translations::WordSense::Lexeme;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use base qw(Wiktionary::Parser::Section);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 42 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
10 42 my $class = shift;
11 42 my %args = @_;
12 42 my $self = bless Wiktionary::Parser::Section->new(%args), $class;
13 42 return $self;
14 }
15  
16  
17 1551 ▶sub get_templates {
Subroutine get_templates: called CC=1 SCAR=0.0
Pod undocumented
18 return [
19 {
20 # e.g. {{trans-bottom}}
21 pattern => qr/{{trans-bottom}}/,
22 5 ▶ parser => sub { return { meta => 'end_word_sense' } }
Subroutine __ANON__: called CC=1 SCAR=0.0
23 },
24 {
25 # comma - separates different blocks of words+metadata
26 pattern => qr/[,;\.]/,
27 97 ▶ parser => sub { return {meta => 'end_segment' } },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 },
29 {
30 # e.g. * Swedish: ...
31 pattern => qr/\*\:?\s([^\:]+)\:\s*/,
32 parser => \&template_language,
33 },
34 {
35 # e.g. {{trans-top|colour}}
36 pattern => qr/{{trans-top\|([^}]+)/,
37 24 ▶ parser => sub { return { word_sense => $_[-1] } }
Subroutine __ANON__: called CC=1 SCAR=0.0
38 },
39  
40 {
41 # e.g. {{trans-see|apples and pears (''Cockney rhyming slang'')|apples and pears}}
42 pattern => qr/{{trans-see\|([^}]+)}}/,
43 parser =>\&template_trans_see,
44 },
45  
46 {
47 # e.g. {{tø|arz|برتقان|tr=burtuʕaan|sc=Arab|xs=Egyptian Arabic}}
48 pattern => qr/{{t[^\|]*\|([^}]+)}}/,
49 parser => \&template_t,
50 },
51 {
52 # e.g. {{ku-Arab|[[پرته‌قاڵ]]
53 pattern => qr/{{(\w{2}\-Arab\|[^}]+)}/,
54 parser => \&template_t,
55 },
56 {
57 # e.g. {{qualifier|Dominican Republic|Puerto Rico}
58 pattern => qr/{{qualifier\|([^\}]+)}}/,
59 parser => \&template_qualifier,
60 },
61 {
62 # e.g. [[כלבא]] (kalbā’) {{m}}
63 pattern => qr/\[\[([^\]]+)\]\]\s+\(([^\)]+)\)\s+{{(\w)}}/,
64 parser => \&template_4,
65 },
66 {
67 # e.g. [[oranževec]] {{m}}
68 pattern => qr/\[\[([^\]]+)\]\]\s*{{(\w)}}/,
69 parser => \&template_2
70 },
71 {
72 # e.g. (narangi) {{f}}
73 pattern => qr/\(([^\]]+)\)\s*{{(\w)}}/,
74 parser => \&template_2
75 },
76 {
77 # e.g. [[colore de aranzu]], [[ruggiu]], [[ruiu]], [[arrubiu]]
78 pattern => qr/\[\[([^\]]+)\]\]/,
79 parser => \&template_3
80 },
81 {
82 # e.g. ''(collective)''
83 pattern => qr/\'\'([^\']+)\'\'/,
84 parser => \&template_q,
85 },
86  
87 # run this last - just chop a character off if nothing else matches;
88 {
89 pattern => qr/./,
90 345 ▶ parser => sub { return {meta => 'unparsable'} }
Subroutine __ANON__: called CC=1 SCAR=0.0
91 }
92  
93 1551 ];
94 }
95  
96 3 ▶sub template_q {
Subroutine template_q: called CC=1 SCAR=0.0
Pod undocumented
97 3 my $self = shift;
98 3 my $template = shift;
99 3 $template =~ s/[\[\]\(\)]//g;
100 3 return { qualifier => [$template] };
101 }
102  
103 10 ▶sub template_qualifier {
Subroutine template_qualifier: called CC=1 SCAR=0.0
Pod undocumented
104 10 my $self = shift;
105 10 my $template = shift;
106 10 my @parts = split(/\|/,$template);
107 10 $self->clean_tokens(@parts);
108 10 return {qualifier => \@parts}
109 }
110  
111 310 ▶sub template_language {
Subroutine template_language: called CC=1 SCAR=0.0
Pod undocumented
112 310 my $self = shift;
113 310 my $language = shift;
114 310 $self->clean_tokens($language);
115 310 return { language => $language }
116 }
117  
118 # parse the trans-see template into its parts
119 # http://en.wiktionary.org/wiki/Template:trans-see
120 0 ▶sub template_trans_see {
Subroutine template_trans_see: not called CC=6 SCAR=37.4
Pod undocumented
121 0 my $self = shift;
122 0 my ($title) = shift;
123
124 0 my @params = split(/\|/,$title,2);
125  
126 # {{trans-see|that}}
127 0 ▶ if ($params[0] && scalar @params == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params[0] and scalar @params == 1) 0 0 0
Condition0%0/3
$params[0] and scalar @params == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$params[0] and scalar @params == 1
$params[0] scalar @params == 1
Truth table0%0/3
A and B
A= $params[0]B= scalar @params == 1
ABresult
0X0
100
111
128 0 $self->clean_tokens(@params);
129 return {
130 0 word_sense => "wiktionary:$params[0]",
131 meta => 'wiktionary_link',
132 }
133 }
134  
135 # {{trans-see|rally|[[rally#Etymology 2|rally]]}}
136 0 ▶ if (my $link_meta = $params[1] =~ m/\[\[([^\]]+)\]\]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $link_meta = $params[1] =~ /\[\[([^\]]+)\]\]/) 0 0 0
137 0 my @link_params = split(/\|/, $1);
138 0 my $link;
139 # grab the last value if there's more than one entry
140 0 ▶ if (scalar @link_params > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @link_params > 1) { } 0 0 0
141 0 $link = $link_params[-1];
142 } else {
143 0 $link = shift @link_params;
144 }
145  
146 0 $link =~ s/\#.+$//;
147  
148 return {
149 0 word_sense => "wiktionary:$link",
150 meta => 'wiktionary_link',
151 }
152 }
153  
154 # {{trans-see|Nahuatl language|Nahuatl}}
155 0 ▶ if ($params[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params[1]) 0 0 0
156 0 my $link = $params[1];
157 0 $link =~ s/\#.+$//;
158 return {
159 0 word_sense => "wiktionary:$link",
160 meta => 'wiktionary_link',
161 }
162 }
163  
164 0 return;
165 }
166  
167 327 ▶sub template_t {
Subroutine template_t: called CC=9 SCAR=22.1
Pod undocumented
168 327 my $self = shift;
169 327 my $template = shift;
170 327 my @parts = split(/\|/,$template);
171 327 my %meta;
172 327 $meta{language_code} = shift @parts;
173 327 my $translation = shift @parts;
174 327 ▶ $translation ||= '';
Condition50%1/2
$translation ||= ""
l!l
3270
MC/DC0%0/1
$translation ||= ""
$translation
Truth table50%1/2
A
A= $translation
Aresult
11
00
175 327 my @tokens = $translation =~ m/\[\[([^\]]+)\]\]/g;
176 327 $translation =~ s/\[\[([^\]]+)\]\]//g;
177 327 push @tokens, $translation;
178
179 327 $meta{translations} = [ grep { length($_) }@tokens ];
180 327 for my $token (@parts) {
181 376 ▶ if ($token =~ m/^[fmcn]$/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($token =~ /^[fmcn]$/) { } 106 270 376
elsif ($token =~ /^[sp]$/) { } 0 270 270
elsif ($token =~ /^tr=(.+)/) { } 104 166 270
elsif ($token =~ /^sc=(.+)/) { } 75 91 166
elsif ($token =~ /^alt=(.+)/) { } 15 76 91
elsif ($token =~ /^xs=(.+)/) { } 76 0 76
182 # f: fem.
183 # m: masc.
184 # c: common
185 # n: neut.
186 106 $meta{gender} = $token;
187 } elsif ($token =~ m/^[sp]$/) {
188 # s: singular
189 # p: plural
190 0 $meta{number} = $token;
191 } elsif ($token =~ m/^tr=(.+)/) {
192 104 $meta{transliteration} = $1;
193 } elsif ($token =~ m/^sc=(.+)/) {
194 75 $meta{script_code_template} = $1;
195 } elsif ($token =~ m/^alt=(.+)/) {
196 15 $meta{alternate} = $1;
197 } elsif ($token =~ m/^xs=(.+)/) {
198 76 $meta{xs} = $1;
199 }
200  
201  
202 }
203  
204 327 return \%meta;
205 }
206  
207 25 ▶sub template_2 {
Subroutine template_2: called CC=1 SCAR=0.0
Pod undocumented
208 25 my $self = shift;
209 25 my @parts = @_;
210 25 my %meta;
211 25 my $translation = shift @parts;
212  
213 25 my @tokens = $translation =~ m/\[\[([^\]]+)\]\]/g;
214 25 $translation =~ s/\[\[([^\]]+)\]\]//g;
215 25 push @tokens, $translation;
216 25 $meta{translations} = \@tokens;
217  
218 25 $meta{gender} = shift @parts;
219 25 return \%meta;
220 }
221  
222 56 ▶sub template_3 {
Subroutine template_3: called CC=1 SCAR=0.0
Pod undocumented
223 56 my $self = shift;
224 56 my @parts = @_;
225 56 $self->clean_tokens(@parts);
226 56 my %meta;
227 56 $meta{translations} = \@parts;
228 56 return \%meta;
229 }
230  
231  
232 4 ▶sub template_4 {
Subroutine template_4: called CC=3 SCAR=11.0
Pod undocumented
233 4 my $self = shift;
234 4 my @parts = @_;
235 4 $self->clean_tokens(@parts);
236 4 my %meta;
237
238 4 $meta{translations} = [shift @parts];
239 4 $meta{transliteration} = shift @parts;
240 4 my $gp = shift @parts;
241 4 ▶ if ($gp =~ m/[mfcn]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($gp =~ /[mfcn]/) { } 2 2 4
elsif ($gp =~ /[ps]/) { } 2 0 2
242 2 $meta{gender} = $gp;
243 } elsif ($gp =~ m/[ps]/) {
244 2 $meta{number} = $gp;
245 }
246  
247 4 return \%meta;
248 }
249  
250 380 ▶sub clean_tokens {
Subroutine clean_tokens: called CC=2 SCAR=6.9
Pod undocumented
251 380 my $self = shift;
252 380 for (@_) {
253 390 $_ =~ s/[\[\]\(\)]//g;
254 }
255 }
256  
257  
258 # add a line of content to this section and parse it into its meaningful parts
259 345 ▶sub add_content {
Subroutine add_content: called CC=22 SCAR=31.1
Pod undocumented
260 345 my $self = shift;
261 345 my $line = shift;
262  
263 # TODO: template parsing issues
264 # {{qualifier|...}}
265 #
266  
267 345 my $line_copy = $line; # #$line is getting disembowled below
268 345 push @{$self->{content}}, $line_copy;
269 345 my $lexeme = Wiktionary::Parser::Section::Translations::WordSense::Lexeme->new();
270  
271 345 ▶ while (my $meta = $self->get_template_match($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $meta = $self->get_template_match($line)) 1206 345 1551
272 1206 ▶ next unless defined $meta;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $meta 0 1206 1206
273  
274 1206 ▶ if ($meta->{word_sense}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"word_sense"}) 24 1182 1206
275 # start new lexeme
276 24 ▶ if ($self->get_current_word_sense()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_current_word_sense) 0 24 24
277 0 $self->get_current_word_sense()->add_lexeme($lexeme);
278 0 $lexeme = Wiktionary::Parser::Section::Translations::WordSense::Lexeme->new();
279 }
280 my $word_sense = Wiktionary::Parser::Section::Translations::WordSense->new(
281 word_sense => $meta->{word_sense},
282 24 );
283 24 $self->add_word_sense($word_sense);
284 24 $self->set_current_word_sense($word_sense);
285 24 next;
286 }
287  
288 1182 ▶ if ($meta->{meta} && $meta->{meta} eq 'end_word_sense') {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"meta"} and $meta->{"meta"} eq "end_word_sense") 5 1177 1182
Condition100%3/3
$meta->{"meta"} and $meta->{"meta"} eq "end_word_sense"
!ll&&!rl&&r
7354425
MC/DC100%2/2
$meta->{"meta"} and $meta->{"meta"} eq "end_word_sense"
$meta->{"meta"} $meta->{"meta"} eq "end_word_sense"
Truth table100%3/3
A and B
A= $meta->{"meta"}B= $meta->{"meta"} eq "end_word_sense"
ABresult
0X0
100
111
289 5 $self->set_current_word_sense(undef);
290 }
291  
292 1182 ▶ if ($meta->{meta} && $meta->{meta} eq 'end_segment') {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->{"meta"} and $meta->{"meta"} eq "end_segment") 97 1085 1182
Condition100%3/3
$meta->{"meta"} and $meta->{"meta"} eq "end_segment"
!ll&&!rl&&r
73535097
MC/DC100%2/2
$meta->{"meta"} and $meta->{"meta"} eq "end_segment"
$meta->{"meta"} $meta->{"meta"} eq "end_segment"
Truth table100%3/3
A and B
A= $meta->{"meta"}B= $meta->{"meta"} eq "end_segment"
ABresult
0X0
100
111
293 97 ▶ if ($self->get_current_word_sense()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_current_word_sense) 96 1 97
294 96 $self->get_current_word_sense()->add_lexeme($lexeme);
295 }
296 97 my $_lexeme = Wiktionary::Parser::Section::Translations::WordSense::Lexeme->new();
297 }
298  
299  
300 1182 ▶ $lexeme->add_translations(@{$meta->{translations} || []}) if ($meta->{translations});
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"translations"} 412 770 1182
Condition50%1/2
$meta->{'translations'} || []
l!l
4120
MC/DC0%0/1
$meta->{'translations'} || []
$meta->{'translations'}
Truth table50%1/2
A
A= $meta->{'translations'}
Aresult
11
00
301 1182 ▶ $lexeme->set_gender($meta->{gender}) if ($meta->{gender});
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"gender"} 133 1049 1182
302 1182 ▶ $lexeme->set_number($meta->{number}) if ($meta->{number});
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"number"} 2 1180 1182
303 1182 ▶ $lexeme->set_transliteration($meta->{transliteration}) if ($meta->{transliteration});
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"transliteration"} 108 1074 1182
304 1182 ▶ $lexeme->set_alternate($meta->{alternate}) if ($meta->{alternate});
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"alternate"} 15 1167 1182
305  
306 1182 ▶ $lexeme->add_qualifiers(@{$meta->{qualifier} || [] }) if $meta->{qualifier};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"qualifier"} 13 1169 1182
Condition50%1/2
$meta->{'qualifier'} || []
l!l
130
MC/DC0%0/1
$meta->{'qualifier'} || []
$meta->{'qualifier'}
Truth table50%1/2
A
A= $meta->{'qualifier'}
Aresult
11
00
307  
308 1182 ▶ $lexeme->set_language_name($meta->{language}) if $meta->{language};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"language"} 310 872 1182
309 1182 ▶ $lexeme->set_language_code($meta->{language_code}) if $meta->{language_code};
Branch: 1 branch
BranchTrueFalseTotal
if $meta->{"language_code"} 327 855 1182
310  
311 }
312
313 345 ▶ if ($self->get_current_word_sense() && $lexeme) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->get_current_word_sense and $lexeme) 336 9 345
Condition66%2/3
$self->get_current_word_sense and $lexeme
!ll&&!rl&&r
90336
MC/DC50%1/2
$self->get_current_word_sense and $lexeme
$self->get_current_word_sense $lexeme
Truth table66%2/3
A and B
A= $self->get_current_word_senseB= $lexeme
ABresult
0X0
100
111
314 336 $self->get_current_word_sense()->add_lexeme($lexeme);
315 }
316 }
317 # find the template pattern that matches the beginning of the given line
318 1551 ▶sub get_template_match {
Subroutine get_template_match: called CC=3 SCAR=11.0
Pod undocumented
319 1551 my $self = shift;
320  
321 1551 my $templates = $self->get_templates();
322
323 1551 for my $template (@$templates) {
324  
325 13935 my $pattern = $template->{pattern};
326 13935 my $parser = $template->{parser};
327  
328 13935 ▶ if (my @matches = $_[0] =~ m/^$pattern/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@matches) = $_[0] =~ /^$pattern/) 1206 12729 13935
329 # if there's a match parse extracted string into a hash of metadata
330 1206 my $meta = $parser->($self,@matches);
331
332 # remove the matched piece from the line
333 1206 $_[0] =~ s/$pattern//;
334 1206 $_[0] =~ s/^\s*//;
335 1206 return $meta;
336 }
337 }
338 345 return;
339 }
340  
341 24 ▶sub add_word_sense {
Subroutine add_word_sense: called CC=1 SCAR=0.0
Pod undocumented
342 24 my $self = shift;
343 24 my $word_sense = shift;
344 24 my $sense = $word_sense->get_word();
345 24 $self->{word_senses}{$sense} = $word_sense;
346 }
347  
348 0 ▶sub get_word_sense {
Subroutine get_word_sense: not called CC=1 SCAR=6.9
Pod undocumented
349 0 my $self = shift;
350 0 my $sense = shift;
351 0 return $self->{word_senses}{$sense};
352 }
353  
354  
355 40 ▶sub get_word_senses {
Subroutine get_word_senses: called CC=2 SCAR=6.9
Pod undocumented
356 40 my $self = shift;
357 40 ▶ return [values %{$self->{word_senses} || {}}];
Condition100%2/2
$self->{'word_senses'} || {}
l!l
2020
MC/DC100%1/1
$self->{'word_senses'} || {}
$self->{'word_senses'}
Truth table100%2/2
A
A= $self->{'word_senses'}
Aresult
11
00
358 }
359  
360 29 ▶sub set_current_word_sense {
Subroutine set_current_word_sense: called CC=1 SCAR=0.0
Pod undocumented
361 29 my $self = shift;
362 29 $self->{current_word_sense} = shift;
363 }
364  
365 898 ▶sub get_current_word_sense {
Subroutine get_current_word_sense: called CC=1 SCAR=0.0
Pod undocumented
366 898 my $self = shift;
367 898 return $self->{current_word_sense};
368 }
369 3 1;
« Parser/Section/WikisaurusSection.pm ↑ Summary Parser/Section/Pronunciation.pm »