Wikidata/Utils.pm

filter: Statementstmt 91.8% 306 / 333 Branchbran 69.5% 57 / 82 Conditioncond 57.1% 8 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 96.0% 24 / 25 Podpod 100% 15 / 15 totaltotal 86.5% 413 / 477 CC 52 SCAR 21.2
CC
52
Cov
86%
look_for_external_id
42.0
clean_publication_date
11.2
clean_date
9.3
CRAP 8.3
« Wikidata/Item/AudioBook.pm ↑ Summary Wikidata/Transform.pm »
1 package MARC::Convert::Wikidata::Utils;
2  
3 60 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 60 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 60 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 60 ▶use List::Util 1.33 qw(none sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 60 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 60 ▶use Roman;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 60 ▶use Unicode::UTF8 qw(decode_utf8 encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 60 Readonly::Array our @EXPORT_OK => qw(clean_cover clean_date clean_isbn clean_issn
13 clean_edition_number clean_number_of_pages clean_oclc clean_publication_date
14 clean_publisher_name clean_publisher_place clean_series_name clean_series_ordinal
15 clean_subtitle clean_title look_for_external_id);
16 60 Readonly::Array our @COVERS => qw(hardback paperback);
17  
18 60 our $VERSION = 0.33;
19 60 our $DEBUG = 0;
20  
21 sub clean_cover {
22 59 ▶ my $cover = shift;
Subroutine clean_cover: called CC=4 SCAR=13.9
Pod documented
23  
24 59 ▶ if (! defined $cover) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cover) 1 58 59
25 1 return;
26 }
27  
28 58 my $ret_cover = $cover;
29 58 $ret_cover =~ s/\s*:\s*$//ms;
30 58 $ret_cover =~ s/\s*;\s*$//ms;
31 58 $ret_cover =~ s/^\s*//ms;
32 58 $ret_cover =~ s/^\(\s*//ms;
33 58 $ret_cover =~ s/\s*\)$//ms;
34  
35 # Hardback
36 58 my $c = decode_utf8('(v|V)áz');
37 58 $ret_cover =~ s/^$c\.?$/hardback/ms;
38 58 $c = decode_utf8('(v|V)ázáno');
39 58 $ret_cover =~ s/^$c$/hardback/ms;
40  
41 # Paperback
42 58 $c = decode_utf8('(b|B)rož');
43 58 $ret_cover =~ s/^$c\.?$/paperback/ms;
44 58 $c = decode_utf8('(b|B)rožováno');
45 58 $ret_cover =~ s/^$c$/paperback/ms;
46  
47 # Collective.
48 58 $c = decode_utf8('svazků');
49 58 $ret_cover =~ s/soubor\s+\d+\s+$c/collective/ms;
50 58 $ret_cover =~ s/soubor\s*\d*/collective/ms;
51  
52 58 ▶ if (none { $ret_cover eq $_ } (@COVERS, 'collective')) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&none(sub { $ret_cover eq $_; } , @MARC::Convert::Wikidata::Utils::COVERS, "collective")) 6 52 58
53 6 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Convert::Wikidata::Utils::DEBUG) 0 6 6
54 0 warn "Book cover '$ret_cover' couldn't clean.";
55 }
56 6 $ret_cover = undef;
57 }
58  
59 58 return $ret_cover;
60 }
61  
62 sub clean_date {
63 69 ▶ my $date = shift;
Subroutine clean_date: called CC=9 SCAR=22.3
Pod documented
64  
65 69 ▶ if (! defined $date) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $date) 1 68 69
66 1 return;
67 }
68 68 ▶ if (! $date) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($date) 12 56 68
69 12 return;
70 }
71  
72 56 my $options_hr = {};
73  
74 56 my $months_hr = {
75 'leden' => '01',
76 decode_utf8('únor') => '02',
77 decode_utf8('březen') => '03',
78 'duben' => '04',
79 decode_utf8('květen') => '05',
80 decode_utf8('červen') => '06',
81 decode_utf8('červenec') => '07',
82 'srpen' => '08',
83 decode_utf8('září') => '09',
84 decode_utf8('říjen') => '10',
85 'listopad' => '11',
86 'prosinec' => '12',
87 };
88  
89 56 my $ret_date = $date;
90  
91 # Date is circa.
92 56 ▶ if ($ret_date =~ s/^c(.*)$/$1/ms || $ret_date =~ s/^asi (.*)$/$1/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_date =~ s/^c(.*)$/$1/ms or $ret_date =~ s/^asi (.*)$/$1/ms) 3 53 56
Condition100%3/3
$ret_date =~ s/^c(.*)$/$1/ms or $ret_date =~ s/^asi (.*)$/$1/ms
l!l&&r!l&&!r
1253
MC/DC100%2/2
$ret_date =~ s/^c(.*)$/$1/ms or $ret_date =~ s/^asi (.*)$/$1/ms
$ret_date =~ s/^c(.*)$/$1/ms $ret_date =~ s/^asi (.*)$/$1/ms
Truth table100%3/3
A or B
A= $ret_date =~ s/^c(.*)$/$1/msB= $ret_date =~ s/^asi (.*)$/$1/ms
ABresult
1X1
011
000
93 3 $options_hr->{'circa'} = 1;
94 }
95  
96 56 foreach my $month (keys %{$months_hr}) {
97 672 $ret_date =~ s/^(\d{4})\s*$month\s*(\d+)\.$/$1-$months_hr->{$month}-$2/ms;
98 }
99 56 my $bk = decode_utf8('př. Kr.');
100 56 $ret_date =~ s/^(\d+)\s*$bk/-$1/ms;
101 56 my $ak = decode_utf8('po. Kr.');
102 56 $ret_date =~ s/^(\d+)\s*$ak/$1/ms;
103 56 $ret_date =~ s/\s*\.$//ms;
104  
105 56 ▶ if ($ret_date !~ m/^\-?\d+(\-\d+)?(\-\d+)?$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret_date =~ /^\-?\d+(\-\d+)?(\-\d+)?$/ms) 0 56 56
106 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Convert::Wikidata::Utils::DEBUG) 0 0 0
107 0 warn "Date '$date' couldn't clean.";
108 }
109 0 $ret_date = undef;
110 }
111  
112 56 ▶ return wantarray ? ($ret_date, $options_hr) : $ret_date;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 53 56
113 }
114  
115 sub clean_edition_number {
116 90 ▶ my $edition_number = shift;
Subroutine clean_edition_number: called CC=6 SCAR=17.9
Pod documented
117  
118 90 ▶ if (! defined $edition_number) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $edition_number) 1 89 90
119 1 return;
120 }
121  
122 89 my $ret_edition_number = $edition_number;
123  
124 # Remove [] on begin and end.
125 89 $ret_edition_number = _remove_square_brackets($ret_edition_number);
126  
127 # Remove trailing whitespace
128 89 $ret_edition_number = _remove_trailing_whitespace($ret_edition_number);
129  
130 # Remove special meanings.
131 89 $ret_edition_number =~ s/,//msg;
132  
133 # Edition.
134 89 my $v1 = decode_utf8('Vydání');
135 89 my $v2 = decode_utf8('vydání');
136 89 $ret_edition_number =~ s/\s*(Vyd\.|vyd\.|$v1|$v2|Vydanie|vydanie|vyd|published)//gx;
137 89 $ret_edition_number =~ s/English edition//ms;
138  
139 89 $ret_edition_number =~ s/\s*rozmn\.//ms;
140 89 my $re = decode_utf8('souborné');
141 89 $ret_edition_number =~ s/\s*$re//ms;
142  
143 # Authorized.
144 89 $ret_edition_number =~ s/\s*aut\.//ms;
145 89 $ret_edition_number =~ s/\s*autoris\.//ms;
146 89 $ret_edition_number =~ s/\s*autoriz\.//ms;
147 89 $re = decode_utf8('autorisované');
148 89 $ret_edition_number =~ s/\s*$re//ms;
149  
150 # Extended.
151 89 $re = decode_utf8('přeprac');
152 89 $ret_edition_number =~ s/\s*$re\.//ms;
153 89 $re = decode_utf8('nezměněné');
154 89 $ret_edition_number =~ s/\s*$re//ms;
155 89 $re = decode_utf8('nezměn');
156 89 $ret_edition_number =~ s/\s*$re\.//ms;
157 89 $re = decode_utf8('přepracované');
158 89 $ret_edition_number =~ s/\s*$re//ms;
159 89 $ret_edition_number =~ s/\s*aktualiz\.//ms;
160 89 $re = decode_utf8('aktualizované');
161 89 $ret_edition_number =~ s/\s*$re//ms;
162 89 $re = decode_utf8('značně');
163 89 $ret_edition_number =~ s/\s*$re//ms;
164 89 $ret_edition_number =~ s/\s*nezm\.//ms;
165 89 $re = decode_utf8('rozšířené');
166 89 $ret_edition_number =~ s/\s*$re//ms;
167 89 $re = decode_utf8('rozmnožené');
168 89 $ret_edition_number =~ s/\s*$re//ms;
169 89 $re = decode_utf8('rozš');
170 89 $ret_edition_number =~ s/\s*$re\.?//ms;
171 89 $ret_edition_number =~ s/\s*dopl\.//ms;
172 89 $ret_edition_number =~ s/\s*dopln\.//ms;
173 89 $re = decode_utf8('doplněné');
174 89 $ret_edition_number =~ s/\s*$re//ms;
175 89 $re = decode_utf8('upravené');
176 89 $ret_edition_number =~ s/\s*$re//ms;
177 89 $ret_edition_number =~ s/\s*upr\.//ms;
178 89 $ret_edition_number =~ s/\s*opr\.//ms;
179 89 $ret_edition_number =~ s/\s*oprav\.//ms;
180 89 $re = decode_utf8('revidované');
181 89 $ret_edition_number =~ s/\s*$re//ms;
182 89 $ret_edition_number =~ s/\s*zcela//ms;
183 89 $re = decode_utf8('v této');
184 89 $ret_edition_number =~ s/\s*$re//ms;
185 89 $re = decode_utf8('V této');
186 89 $ret_edition_number =~ s/\s*$re//ms;
187 89 $ret_edition_number =~ s/\s*V\stomto\ssouboru//ms;
188 89 $re = decode_utf8('podobě');
189 89 $ret_edition_number =~ s/\s*$re//ms;
190 89 $re = decode_utf8('část');
191 89 $ret_edition_number =~ s/\s*$re\.?//ms;
192  
193 # Czech.
194 89 $re = decode_utf8('(v|V) českém jazyce');
195 89 $ret_edition_number =~ s/\s*$re//ms;
196 89 $re = decode_utf8('(Č|č)eské');
197 89 $ret_edition_number =~ s/\s*$re//ms;
198 89 $re = decode_utf8('(Č|č)es\.');
199 89 $ret_edition_number =~ s/\s*$re//ms;
200 89 $re = decode_utf8('(v|V) češtině\s*');
201 89 $ret_edition_number =~ s/\s*$re//ms;
202  
203 # With illustration.
204 89 $re = decode_utf8('s vyobrazeními');
205 89 $ret_edition_number =~ s/\s*$re//ms;
206  
207 # Remove trailing whitespace
208 89 $ret_edition_number = _remove_trailing_whitespace($ret_edition_number);
209  
210 # Rewrite number in Czech to number.
211 89 my $dict_hr = {
212 decode_utf8('První') => 1,
213 decode_utf8('Prvé') => 1,
214 decode_utf8('první') => 1,
215 'First' => 1,
216 decode_utf8('prvé') => 1,
217 decode_utf8('Druhé') => 2,
218 decode_utf8('druhé') => 2,
219 decode_utf8('Třetí') => 3,
220 decode_utf8('třetí') => 3,
221 decode_utf8('Čtvrté') => 4,
222 decode_utf8('čtvrté') => 4,
223 decode_utf8('Páté') => 5,
224 decode_utf8('páté') => 5,
225 decode_utf8('Šesté') => 6,
226 decode_utf8('šesté') => 6,
227 decode_utf8('Sedmé') => 7,
228 decode_utf8('sedmé') => 7,
229 decode_utf8('Osmé') => 8,
230 decode_utf8('osmé') => 8,
231 decode_utf8('Deváté') => 9,
232 decode_utf8('deváté') => 9,
233 decode_utf8('Desáté') => 10,
234 decode_utf8('desáté') => 10,
235 decode_utf8('Dvacáté') => 20,
236 decode_utf8('dvacáté') => 20,
237 };
238 89 foreach my $origin (keys %{$dict_hr}) {
239 2225 $ret_edition_number =~ s/\s*$origin\s*/$dict_hr->{$origin}/ms;
240 }
241  
242 # Remove dots.
243 89 $ret_edition_number =~ s/\s*\.\s*//ms;
244  
245 # Remove :
246 89 $ret_edition_number =~ s/\s*:\s*//ms;
247  
248 # Detect first number.
249 89 $ret_edition_number =~ s/^(\d+)(?!.*\d).*$/$1/ms;
250  
251 # Rename roman to arabic
252 89 ▶ if (isroman($ret_edition_number)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&isroman($ret_edition_number)) 3 86 89
253 3 $ret_edition_number = arabic($ret_edition_number);
254 }
255  
256 89 ▶ if ($ret_edition_number !~ m/^\d+$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret_edition_number =~ /^\d+$/ms) 7 82 89
257 7 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Convert::Wikidata::Utils::DEBUG) 0 7 7
258 0 warn encode_utf8("Edition number '$edition_number' couldn't clean ($ret_edition_number).");
259 }
260 7 $ret_edition_number = undef;
261 }
262  
263 89 return $ret_edition_number;
264 }
265  
266 sub clean_isbn {
267 16 ▶ my $isbn = shift;
Subroutine clean_isbn: called CC=4 SCAR=17.9
Pod documented
268  
269 16 ▶ if (! defined $isbn) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $isbn) 0 16 16
270 0 return;
271 }
272  
273 16 my $ret_isbn = $isbn;
274 16 $ret_isbn =~ s/\s+:?$//ms;
275  
276 16 ▶ if ($ret_isbn !~ m/^[\d\-]+$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret_isbn =~ /^[\d\-]+$/ms) 0 16 16
277 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Convert::Wikidata::Utils::DEBUG) 0 0 0
278 0 warn "ISBN '$ret_isbn' couldn't clean.";
279 }
280 0 $ret_isbn = undef;
281 }
282  
283 16 return $ret_isbn;
284 }
285  
286 sub clean_issn {
287 13 ▶ my $issn = shift;
Subroutine clean_issn: called CC=4 SCAR=15.8
Pod documented
288  
289 13 ▶ if (! defined $issn) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $issn) 10 3 13
290 10 return;
291 }
292  
293 3 my $ret_issn = $issn;
294 3 $ret_issn =~ s/\s+;?$//ms;
295  
296 3 ▶ if ($ret_issn !~ m/^\d{4}-\d{4}$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret_issn =~ /^\d{4}-\d{4}$/ms) 0 3 3
297 0 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Convert::Wikidata::Utils::DEBUG) 0 0 0
298 0 warn "ISSN '$ret_issn' couldn't clean.";
299 }
300 0 $ret_issn = undef;
301 }
302  
303 3 return $ret_issn;
304 }
305  
306 sub clean_number_of_pages {
307 37 ▶ my $number_of_pages = shift;
Subroutine clean_number_of_pages: called CC=5 SCAR=16.1
Pod documented
308  
309 37 ▶ if (! defined $number_of_pages) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $number_of_pages) 2 35 37
310 2 return;
311 }
312  
313 35 my $ret_number_of_pages = $number_of_pages;
314  
315 # Remove []
316 35 $ret_number_of_pages =~ s/\[(\d+)\]/$1/msg;
317  
318 # Remove text informations.
319 35 my $pril = decode_utf8('příl');
320 35 my $neci = decode_utf8('nečíslovaných');
321 35 $ret_number_of_pages =~ s/\s*(stran|s|$pril|$neci|l|barev|obr)\.*\s*//msg;
322  
323 # Remove other characters like ':' and ';'.
324 35 $ret_number_of_pages =~ s/[:;]//msg;
325  
326 # TODO Support roman numbers.
327 35 ▶ if ($ret_number_of_pages =~ m/^[\ \d,]+$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_number_of_pages =~ /^[\ \d,]+$/ms) 32 3 35
328 32 my @numbers = split m/\s*,\s*/ms, $ret_number_of_pages;
329 32 $ret_number_of_pages = sum(@numbers);
330 }
331  
332 35 ▶ if ($ret_number_of_pages !~ m/^\d+$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret_number_of_pages =~ /^\d+$/ms) 3 32 35
333 3 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Convert::Wikidata::Utils::DEBUG) 0 3 3
334 0 warn "Number of pages '$number_of_pages' couldn't clean.";
335 }
336 3 $ret_number_of_pages = undef;
337 }
338  
339 35 return $ret_number_of_pages;
340 }
341  
342 sub clean_oclc {
343 29 ▶ my $oclc = shift;
Subroutine clean_oclc: called CC=2 SCAR=7.2
Pod documented
344  
345 29 ▶ if (! defined $oclc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $oclc) 0 29 29
346 0 return;
347 }
348  
349 29 my $ret_oclc = $oclc;
350 29 $ret_oclc =~ s/^\(OCoLC\)//ms;
351  
352 29 return $ret_oclc;
353 }
354  
355 sub clean_publication_date {
356 8 ▶ my $publication_date = shift;
Subroutine clean_publication_date: called CC=11 SCAR=24.1
Pod documented
357  
358 8 my $ret_publication_date = $publication_date;
359  
360 8 my ($start_date, $end_date, $dash);
361 8 ▶ if ($ret_publication_date =~ m/^([^-]+)(\-?)(.*)$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_publication_date =~ /^([^-]+)(\-?)(.*)$/ms) 8 0 8
362 8 $start_date = $1;
363 8 $dash = $2;
364 8 $end_date = $3;
365 }
366  
367 # Remove [] on begin and end.
368 # XXX [] is circa
369 8 $start_date = _remove_square_brackets($start_date);
370 8 ▶ if (defined $end_date) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $end_date) 8 0 8
371 8 $end_date = _remove_square_brackets($end_date);
372 }
373  
374 # Detect circa.
375 8 my $option;
376 8 foreach my $date ($start_date, $end_date) {
377 16 ▶ if (defined $date && ($date =~ s/^c(.*)$/$1/ms
Branch: 1 branch
BranchTrueFalseTotal
if (defined $date and $date =~ s/^c(.*)$/$1/ms || $date =~ s/^(.*)\?$/$1/ms) 3 13 16
Condition83%5/6
defined $date and $date =~ s/^c(.*)$/$1/ms || $date =~ s/^(.*)\?$/$1/ms
!ll&&!rl&&r
0142
$date =~ s/^c(.*)$/$1/ms or $date =~ s/^(.*)\?$/$1/ms
l!l&&r!l&&!r
1213
MC/DC33%1/3
defined $date and $date =~ s/^c(.*)$/$1/ms || $date =~ s/^(.*)\?$/$1/ms
defined $date $date =~ s/^c(.*)$/$1/ms $date =~ s/^(.*)\?$/$1/ms
Truth table50%2/4
A and B or C
A= defined $dateB= $date =~ s/^c(.*)$/$1/msC= $date =~ s/^(.*)\?$/$1/ms
ABCresult
0XX0
1000
11X1
1011
378 || $date =~ s/^(.*)\?$/$1/ms)) {
379  
380 # XXX Circa of start and end
381 3 $option = 'circa';
382 }
383 }
384  
385 # Combine back.
386 8 $ret_publication_date = $start_date;
387 8 ▶ if ($dash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dash) 3 5 8
388 3 $ret_publication_date .= $dash;
389 }
390 8 ▶ if ($end_date) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end_date) 2 6 8
391 2 $ret_publication_date .= $end_date;
392 }
393  
394 8 ▶ if ($ret_publication_date !~ m/^(\d+)\-?(.*)$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ret_publication_date =~ /^(\d+)\-?(.*)$/ms) 1 7 8
395  
396 1 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MARC::Convert::Wikidata::Utils::DEBUG) 0 1 1
397 0 warn "Publication date '$publication_date' couldn't clean.";
398 }
399 1 $ret_publication_date = undef;
400 }
401  
402 8 return ($ret_publication_date, $option);
403 }
404  
405 sub clean_publisher_name {
406 51 ▶ my $publisher_name = shift;
Subroutine clean_publisher_name: called CC=2 SCAR=7.0
Pod documented
407  
408 51 ▶ if (! defined $publisher_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $publisher_name) 0 51 51
409 0 return;
410 }
411  
412 51 my $ret_publisher_name = $publisher_name;
413  
414 # Trailing whitespace on begin and end
415 51 $ret_publisher_name = _remove_trailing_whitespace($ret_publisher_name);
416  
417 # Separators on the end.
418 51 $ret_publisher_name =~ s/\s*,$//g;
419 51 $ret_publisher_name =~ s/\s*:$//g;
420 51 $ret_publisher_name =~ s/\s*;$//g;
421  
422 # Remove ( from begin and not ending.
423 51 $ret_publisher_name =~ s/^\(([^\)]+)$/$1/ms;
424  
425 # Remove [] on begin and end.
426 51 $ret_publisher_name = _remove_square_brackets($ret_publisher_name);
427  
428 51 return $ret_publisher_name;
429 }
430  
431 sub clean_publisher_place {
432 81 ▶ my $publisher_place = shift;
Subroutine clean_publisher_place: called CC=3 SCAR=11.0
Pod documented
433  
434 81 ▶ if (! defined $publisher_place) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $publisher_place) 0 81 81
435 0 return;
436 }
437  
438 81 my $dict_hr = {
439 'Blansku' => 'Blansko',
440 decode_utf8('w Cieszynie') => decode_utf8('Cieszyn'),
441 decode_utf8('Č. Budějovice') => decode_utf8('České Budějovice'),
442 'Plzni' => decode_utf8('Plzeň'),
443 'Prag' => 'Praha',
444 'Praze' => 'Praha',
445 'W Praze' => 'Praha',
446 decode_utf8('Pardubicích') => 'Pardubice',
447 decode_utf8('Brně') => 'Brno',
448 decode_utf8('Hradci Králové') => decode_utf8('Hradec Králové'),
449 decode_utf8('Jičíně') => decode_utf8('Jičín'),
450 decode_utf8('Jihlavě') => 'Jihlava',
451 decode_utf8('Jimramově') => 'Jimramov',
452 decode_utf8('Karlových Varech') => 'Karlovy Vary',
453 decode_utf8('Kolíně') => decode_utf8('Kolín'),
454 decode_utf8('Kroměříži') => decode_utf8('Kroměříž'),
455 decode_utf8('Hoře Kutné') => decode_utf8('Kutná Hora'),
456 decode_utf8('Kutné Hoře') => decode_utf8('Kutná Hora'),
457 'Liberci' => 'Liberec',
458 decode_utf8('Litoměřicích') => decode_utf8('Litoměřice'),
459 decode_utf8('Náchodě') => decode_utf8('Náchod'),
460 'Nymburce' => 'Nymburk',
461 'Olomouci' => 'Olomouc',
462 decode_utf8('Ostravě') => 'Ostrava',
463 decode_utf8('Poděbradech') => decode_utf8('Poděbrady'),
464 decode_utf8('Přelouči') => decode_utf8('Přelouč'),
465 decode_utf8('Přerově') => decode_utf8('Přerov'),
466 decode_utf8('Řevnicích') => decode_utf8('Řevnice'),
467 decode_utf8('Stříbře') => decode_utf8('Stříbro'),
468 decode_utf8('Telči') => decode_utf8('Telč'),
469 decode_utf8('Třebíč na Moravě') => decode_utf8('Třebíč'),
470 decode_utf8('Třebíči') => decode_utf8('Třebíč'),
471 decode_utf8('Třebíči na Moravě') => decode_utf8('Třebíč'),
472 decode_utf8('Vyškově') => decode_utf8('Vyškov'),
473 decode_utf8('Zlíně') => decode_utf8('Zlín'),
474 # No place.
475 'S.l.' => 'sine loco',
476 };
477  
478 81 my $ret_publisher_place = $publisher_place;
479  
480 81 $ret_publisher_place =~ s/\s+$//g;
481 81 $ret_publisher_place =~ s/\s*:$//g;
482 81 $ret_publisher_place =~ s/\s*;$//g;
483  
484 # [V Praze]
485 81 $ret_publisher_place =~ s/^\[(.*?)\]?$/$1/ms;
486  
487 81 $ret_publisher_place =~ s/^[vVW]e?\s+//ms;
488  
489 81 foreach my $origin (keys %{$dict_hr}) {
490 2916 $ret_publisher_place =~ s/^$origin$/$dict_hr->{$origin}/ms;
491 }
492  
493 81 $ret_publisher_place =~ s/^[VW]e?\s+([\s\w]+)$/$1/ms;
494 # [Praha]
495 81 $ret_publisher_place =~ s/^\[(.*?)\]$/$1/ms;
496  
497 81 $ret_publisher_place =~ s/^(.*)\?$/$1/ms;
498  
499 81 return $ret_publisher_place;
500 }
501  
502 sub clean_series_name {
503 15 ▶ my $series_name = shift;
Subroutine clean_series_name: called CC=2 SCAR=7.0
Pod documented
504  
505 15 ▶ if (! defined $series_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $series_name) 0 15 15
506 0 return;
507 }
508  
509 15 my $ret_series_name = $series_name;
510  
511 # Trailing whitespace on begin and end
512 15 $ret_series_name = _remove_trailing_whitespace($ret_series_name);
513  
514 15 $ret_series_name =~ s/\s*;$//g;
515 15 $ret_series_name =~ s/\s*:$//g;
516 15 $ret_series_name =~ s/\s*,$//g;
517  
518 # Remove [] on begin and end.
519 15 $ret_series_name = _remove_square_brackets($ret_series_name);
520  
521 15 return $ret_series_name;
522 }
523  
524 sub clean_series_ordinal {
525 33 ▶ my $series_ordinal = shift;
Subroutine clean_series_ordinal: called CC=4 SCAR=13.9
Pod documented
526  
527 33 ▶ if (! defined $series_ordinal) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $series_ordinal) 3 30 33
528 3 return;
529 }
530  
531 30 my $ret_series_ordinal = $series_ordinal;
532  
533 # Trailing whitespace on begin and end
534 30 $ret_series_ordinal = _remove_trailing_whitespace($ret_series_ordinal);
535  
536 30 $ret_series_ordinal =~ s/^(S|s)v\.\s*//g;
537 30 $ret_series_ordinal =~ s/^svazek\s*//g;
538 30 $ret_series_ordinal =~ s/\s*svazek$//g;
539  
540 30 my $c = decode_utf8('(č|Č)');
541 30 $ret_series_ordinal =~ s/^$c\.\s*//ms;
542 30 $c = decode_utf8('seš');
543 30 $ret_series_ordinal =~ s/^$c\.\s*//ms;
544 30 $c = decode_utf8('(č|Č)íslo');
545 30 $ret_series_ordinal =~ s/^$c\s*//ms;
546 30 $c = decode_utf8('(Výstava|Výst)');
547 30 $ret_series_ordinal =~ s/$c\.?\s*//ms;
548 30 $c = decode_utf8('(kniha|kn)');
549 30 $ret_series_ordinal =~ s/$c\.?\s*//ms;
550 30 $c = decode_utf8('(díl)');
551 30 $ret_series_ordinal =~ s/$c\.?\s*//ms;
552 30 $ret_series_ordinal =~ s/(vol)\.?\s*//ms;
553  
554 30 $ret_series_ordinal =~ s/(\d+),\s*(\d+)/$1-$2/ms;
555  
556 30 $ret_series_ordinal =~ s/^(\d+)\.$/$1/ms;
557  
558 30 ▶ if ($ret_series_ordinal =~ m/^(\d+)-(\d+)$/ms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_series_ordinal =~ /^(\d+)-(\d+)$/ms) { } 4 26 30
559 4 my $first = $1;
560 4 my $second = $2;
561 4 ▶ if ($second < $first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($second < $first) 3 1 4
562 3 my $first_len = length $first;
563 3 my $second_len = length $second;
564 3 my $first_addition = substr $first, 0, ($first_len - $second_len);
565 3 $ret_series_ordinal = $first.'-'.$first_addition.$second;
566 }
567 } else {
568 # XXX Remove all other characters.
569 26 $ret_series_ordinal =~ s/^(\d+).*$/$1/ms;
570 }
571  
572 30 return $ret_series_ordinal;
573 }
574  
575 sub clean_subtitle {
576 88 ▶ my $subtitle = shift;
Subroutine clean_subtitle: called CC=2 SCAR=6.9
Pod documented
577  
578 88 ▶ if (! defined $subtitle) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $subtitle) 67 21 88
579 67 return;
580 }
581  
582 21 my $ret_subtitle = $subtitle;
583 21 $ret_subtitle =~ s/\s+$//g;
584 21 $ret_subtitle =~ s/\/$//g;
585 21 $ret_subtitle =~ s/\s+$//g;
586 21 $ret_subtitle =~ s/,$//g;
587  
588 21 return $ret_subtitle;
589 }
590  
591 sub clean_title {
592 31 ▶ my $title = shift;
Subroutine clean_title: called CC=2 SCAR=7.1
Pod documented
593  
594 31 ▶ if (! defined $title) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $title) 0 31 31
595 0 return;
596 }
597  
598 31 my $ret_title = $title;
599 31 $ret_title =~ s/\s+$//g;
600 31 $ret_title =~ s/\s*\/$//g;
601 31 $ret_title =~ s/\s*\:$//g;
602 31 $ret_title =~ s/\.$//g;
603  
604 31 return $ret_title;
605 }
606  
607 sub look_for_external_id {
608 0 ▶ my ($object, $external_id_name, $deprecation_flag) = @_;
Subroutine look_for_external_id: not called CC=6 SCAR=37.4
Pod documented
609  
610 0 ▶ $deprecation_flag ||= 0;
Condition0%0/2
$deprecation_flag ||= 0
l!l
00
MC/DC0%0/1
$deprecation_flag ||= 0
$deprecation_flag
Truth table0%0/2
A
A= $deprecation_flag
Aresult
11
00
611  
612 0 my @ret;
613 0 foreach my $external_id (@{$object->external_ids}) {
614 0 ▶ if ($external_id->name eq $external_id_name
Branch: 1 branch
BranchTrueFalseTotal
if ($external_id->name eq $external_id_name and $external_id->deprecated == $deprecation_flag) 0 0 0
Condition0%0/3
$external_id->name eq $external_id_name and $external_id->deprecated == $deprecation_flag
!ll&&!rl&&r
000
MC/DC0%0/2
$external_id->name eq $external_id_name and $external_id->deprecated == $deprecation_flag
$external_id->name eq $external_id_name $external_id->deprecated == $deprecation_flag
Truth table0%0/3
A and B
A= $external_id->name eq $external_id_nameB= $external_id->deprecated == $deprecation_flag
ABresult
0X0
100
111
615 && $external_id->deprecated == $deprecation_flag) {
616  
617 0 push @ret, $external_id->value;
618 }
619 }
620  
621 0 ▶ return wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
622 }
623  
624 sub _remove_trailing_whitespace {
625 274 ▶ my $string = shift;
Subroutine _remove_trailing_whitespace: called CC=1 SCAR=0.0
626  
627 274 $string =~ s/^\s+//g;
628 274 $string =~ s/\s+$//g;
629  
630 274 return $string;
631 }
632  
633 sub _remove_square_brackets {
634 171 ▶ my $string = shift;
Subroutine _remove_square_brackets: called CC=1 SCAR=0.0
635  
636 171 $string =~ s/^\[\s*(.*?)\s*\]$/$1/ms;
637 171 $string =~ s/^\[\s*([^\]]+)$/$1/ms;
638 171 $string =~ s/^([^\]]+)\s*\]$/$1/ms;
639  
640 171 return $string;
641 }
642  
643 60 1;
644  
645 __END__
646  
647 =pod
648  
649 =encoding utf8
650  
651 =head1 NAME
652  
653 MARC::Convert::Wikidata::Utils - Utilities for MARC::Convert::Wikidata.
654  
655 =head1 SYNOPSIS
656  
657 use MARC::Convert::Wikidata::Utils qw(clean_cover clean_date clean_edition_number clean_issn clean_number_of_pages clean_oclc clean_publication_date clean_publisher_name clean_publisher_place clean_series_name clean_series_ordinal clean_subtitle clean_title look_for_external_id);
658  
659 my $cleaned_cover = clean_cover($cover);
660 my $cleaned_date = clean_date($date);
661 my ($cleaned_date, $options_hr) = clean_date($date);
662 my $cleaned_edition_number = clean_edition_number($edition_number);
663 my $cleaned_isbn = clean_isbn($isbn);
664 my $cleaned_issn = clean_issn($issn);
665 my $cleaned_number_of_pages = clean_number_of_pages($number_of_pages);
666 my $cleaned_oclc = clean_oclc($oclc);
667 my ($cleaned_publication_date, $option) = clean_publication_date($publication_date);
668 my $cleaned_publisher_name = clean_publisher_name($publisher);
669 my $cleaned_publisher_place = clean_publisher_place($publisher_place);
670 my $cleaned_series_name = clean_series_name($series_name);
671 my $cleaned_series_ordinal = clean_series_ordinal($series_ordinal);
672 my $cleaned_subtitle = clean_subitle($subtitle);
673 my $cleaned_title = clean_title($title);
674 my $value = look_for_external_id($object, $external_id_name, $deprecation_flag);
675 my @values = look_for_external_id($object, $external_id_name, $deprecation_flag);
676  
677 =head1 SUBROUTINES
678  
679 =head2 C<clean_cover>
680  
681 my $cleaned_cover = clean_cover($cover);
682  
683 Clean book cover in Czech language.
684  
685 Returns string or undef.
686  
687 =head2 C<clean_date>
688  
689 my $cleaned_date = clean_date($date);
690 my ($cleaned_date, $options_hr) = clean_date($date);
691  
692 Clean date in Czech language.
693  
694 Returns string or undef in scalar context.
695 Returns string or undef of date and hash reference with options in array context.
696  
697 =head2 C<clean_edition_number>
698  
699 my $cleaned_edition_number = clean_edition_number($edition_number);
700  
701 Clean edition number in Czech language.
702  
703 Returns string or undef.
704  
705 =head2 C<clean_isbn>
706  
707 my $cleaned_isbn = clean_isbn($isbn);
708  
709 Clean ISBN.
710  
711 Returns string or undef.
712  
713 =head2 C<clean_issn>
714  
715 my $cleaned_issn = clean_issn($issn);
716  
717 Clean ISSN.
718  
719 Returns string or undef.
720  
721 =head2 C<clean_number_of_pages>
722  
723 my $cleaned_number_of_pages = clean_number_of_pages($number_of_pages);
724  
725 Clean number of pages in Czech language.
726  
727 Returns string or undef.
728  
729 =head2 C<clean_oclc>
730  
731 my $cleaned_oclc = clean_oclc($oclc);
732  
733 Clean OCLC number.
734  
735 Returns string or undef.
736  
737 =head2 C<clean_publication_date>
738  
739 my ($cleaned_publication_date, $option) = clean_publication_date($publication_date);
740  
741 Clean publication date. Returned options could be 'circa' string in case that
742 publication date is not precise.
743  
744 Returns array with string or undef and string.
745  
746 =head2 C<clean_publisher_name>
747  
748 my $cleaned_publisher_name = clean_publisher_name($publisher);
749  
750 Clean publishing house.
751  
752 Returns string or undef.
753  
754 =head2 C<clean_publisher_place>
755  
756 my $cleaned_publisher_place = clean_publisher_place($publisher_place);
757  
758 Clean place of publication in Czech language.
759  
760 Returns string or undef.
761  
762 =head2 C<clean_series_name>
763  
764 my $cleaned_series_name = clean_series_name($series_name);
765  
766 Clean series name.
767  
768 Returns string or undef.
769  
770 =head2 C<clean_series_ordinal>
771  
772 my $cleaned_series_ordinal = clean_series_ordinal($series_ordinal);
773  
774 Clean series ordinal in Czech language.
775  
776 Returns string or undef.
777  
778 =head2 C<clean_subtitle>
779  
780 my $cleaned_subtitle = clean_subtitle($subtitle);
781  
782 Clean subtitle.
783  
784 Returns string or undef.
785  
786 =head2 C<clean_title>
787  
788 my $cleaned_title = clean_title($title);
789  
790 Clean title.
791  
792 Returns string or undef.
793  
794 =head2 C<look_for_external_id>
795  
796 my $value = look_for_external_id($object, $external_id_name, $deprecation_flag);
797 my @values = look_for_external_id($object, $external_id_name, $deprecation_flag);
798  
799 Look for external id values defined by name and deprecation flag.
800  
801 Returns strings.
802  
803 =head1 EXAMPLE1
804  
805 =for comment filename=clean_cover.pl
806  
807 use strict;
808 use warnings;
809  
810 use MARC::Convert::Wikidata::Utils qw(clean_cover);
811 use Unicode::UTF8 qw(decode_utf8 encode_utf8);
812  
813 my $cover = decode_utf8('(Vázáno) :');;
814 my $cleaned_cover = clean_cover($cover);
815  
816 # Print out.
817 print encode_utf8("Cover: $cover\n");
818 print "Cleaned cover: $cleaned_cover\n";
819  
820 # Output:
821 # Cover: (Vázáno) :
822 # Cleaned cover: hardback
823  
824 =head1 EXAMPLE2
825  
826 =for comment filename=clean_date.pl
827  
828 use strict;
829 use warnings;
830  
831 use MARC::Convert::Wikidata::Utils qw(clean_date);
832 use Unicode::UTF8 qw(decode_utf8 encode_utf8);
833  
834 my $date = decode_utf8('2020 březen 03.');
835 my $cleaned_date = clean_date($date);
836  
837 # Print out.
838 print encode_utf8("Date: $date\n");
839 print "Cleaned date: $cleaned_date\n";
840  
841 # Output:
842 # Date: 2020 březen 03.
843 # Cleaned date: 2020-03-03
844  
845 =head1 EXAMPLE3
846  
847 =for comment filename=clean_edition_number.pl
848  
849 use strict;
850 use warnings;
851  
852 use MARC::Convert::Wikidata::Utils qw(clean_edition_number);
853 use Unicode::UTF8 qw(decode_utf8 encode_utf8);
854  
855 my $edition_number = decode_utf8('Druhé vydání');
856 my $cleaned_edition_number = clean_edition_number($edition_number);
857  
858 # Print out.
859 print encode_utf8("Edition number: $edition_number\n");
860 print "Cleaned edition number: $cleaned_edition_number\n";
861  
862 # Output:
863 # Edition number: Druhé vydání
864 # Cleaned edition number: 2
865  
866 =head1 EXAMPLE4
867  
868 =for comment filename=clean_issn.pl
869  
870 use strict;
871 use warnings;
872  
873 use MARC::Convert::Wikidata::Utils qw(clean_issn);
874  
875 my $issn = '0585-5675 ;';
876 my $cleaned_issn = clean_issn($issn);
877  
878 # Print out.
879 print "ISSN: $issn\n";
880 print "Cleaned ISSN: $cleaned_issn\n";
881  
882 # Output:
883 # ISSN: 0585-5675 ;
884 # Cleaned ISSN: 0585-5675
885  
886 =head1 EXAMPLE5
887  
888 =for comment filename=clean_number_of_pages.pl
889  
890 use strict;
891 use warnings;
892  
893 use MARC::Convert::Wikidata::Utils qw(clean_number_of_pages);
894  
895 my $number_of_pages = '575 s. ;';
896 my $cleaned_number_of_pages = clean_number_of_pages($number_of_pages);
897  
898 # Print out.
899 print "Number of pages: $number_of_pages\n";
900 print "Cleaned number of pages: $cleaned_number_of_pages\n";
901  
902 # Output:
903 # Number of pages: 575 s. ;
904 # Cleaned number of pages: 575
905  
906 =head1 DEPENDENCIES
907  
908 L<Exporter>,
909 L<List::Util>,
910 L<Readonly>,
911 L<Roman>,
912 L<Unicode::UTF8>.
913  
914 =head1 REPOSITORY
915  
916 L<https://github.com/michal-josef-spacek/MARC-Convert-Wikidata>
917  
918 =head1 AUTHOR
919  
920 Michal Josef Špaček L<mailto:skim@cpan.org>
921  
922 L<http://skim.cz>
923  
924 =head1 LICENSE AND COPYRIGHT
925  
926 © 2021-2026 Michal Josef Špaček
927  
928 BSD 2-Clause License
929  
930 =head1 VERSION
931  
932 0.33
933  
934 =cut
« Wikidata/Item/AudioBook.pm ↑ Summary Wikidata/Transform.pm »