Wikidata/Utils.pm

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