1 |
package WebPAC::Normalize; |
package WebPAC::Normalize; |
2 |
use Exporter 'import'; |
use Exporter 'import'; |
3 |
@EXPORT = qw/ |
our @EXPORT = qw/ |
4 |
_set_rec _set_lookup |
_set_ds _set_lookup |
5 |
|
get_ds |
6 |
_set_load_row |
_set_load_row |
7 |
_get_ds _clean_ds |
_get_ds _clean_ds |
8 |
_debug |
_debug |
9 |
_pack_subfields_hash |
_pack_subfields_hash |
10 |
|
|
11 |
tag search display |
search_display search display sorted |
12 |
|
|
13 |
marc marc_indicators marc_repeatable_subfield |
marc marc_indicators marc_repeatable_subfield |
14 |
marc_compose marc_leader |
marc_compose marc_leader marc_fixed |
15 |
marc_duplicate marc_remove |
marc_duplicate marc_remove marc_count |
16 |
marc_original_order |
marc_original_order |
17 |
|
|
18 |
rec1 rec2 rec |
rec1 rec2 rec |
19 |
|
frec |
20 |
regex prefix suffix surround |
regex prefix suffix surround |
21 |
first lookup join_with |
first lookup join_with |
22 |
save_into_lookup |
save_into_lookup |
24 |
split_rec_on |
split_rec_on |
25 |
|
|
26 |
get set |
get set |
27 |
|
count |
28 |
|
|
29 |
/; |
/; |
30 |
|
|
31 |
use warnings; |
use warnings; |
39 |
# debugging warn(s) |
# debugging warn(s) |
40 |
my $debug = 0; |
my $debug = 0; |
41 |
|
|
42 |
|
use WebPAC::Normalize::ISBN; |
43 |
|
push @EXPORT, ( 'isbn_10', 'isbn_13' ); |
44 |
|
|
45 |
=head1 NAME |
=head1 NAME |
46 |
|
|
47 |
WebPAC::Normalize - describe normalisaton rules using sets |
WebPAC::Normalize - describe normalisaton rules using sets |
48 |
|
|
|
=head1 VERSION |
|
|
|
|
|
Version 0.25 |
|
|
|
|
49 |
=cut |
=cut |
50 |
|
|
51 |
our $VERSION = '0.25'; |
our $VERSION = '0.32'; |
52 |
|
|
53 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
54 |
|
|
61 |
C<perl -c normalize.pl>. |
C<perl -c normalize.pl>. |
62 |
|
|
63 |
Normalisation can generate multiple output normalized data. For now, supported output |
Normalisation can generate multiple output normalized data. For now, supported output |
64 |
types (on the left side of definition) are: C<tag>, C<display>, C<search> and |
types (on the left side of definition) are: C<search_display>, C<display>, C<search> and |
65 |
C<marc>. |
C<marc>. |
66 |
|
|
67 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
80 |
marc_encoding => 'utf-8', |
marc_encoding => 'utf-8', |
81 |
config => $config, |
config => $config, |
82 |
load_row_coderef => sub { |
load_row_coderef => sub { |
83 |
my ($database,$input,$mfn) = shift; |
my ($database,$input,$mfn) = @_; |
84 |
$store->load_row( database => $database, input => $input, id => $mfn ); |
$store->load_row( database => $database, input => $input, id => $mfn ); |
85 |
}, |
}, |
86 |
); |
); |
108 |
|
|
109 |
no strict 'subs'; |
no strict 'subs'; |
110 |
_set_lookup( $arg->{lookup} ) if defined($arg->{lookup}); |
_set_lookup( $arg->{lookup} ) if defined($arg->{lookup}); |
111 |
_set_rec( $arg->{row} ); |
_set_ds( $arg->{row} ); |
112 |
_set_config( $arg->{config} ) if defined($arg->{config}); |
_set_config( $arg->{config} ) if defined($arg->{config}); |
113 |
_clean_ds( %{ $arg } ); |
_clean_ds( %{ $arg } ); |
114 |
$load_row_coderef = $arg->{load_row_coderef}; |
$load_row_coderef = $arg->{load_row_coderef}; |
119 |
return _get_ds(); |
return _get_ds(); |
120 |
} |
} |
121 |
|
|
122 |
=head2 _set_rec |
=head2 _set_ds |
123 |
|
|
124 |
Set current record hash |
Set current record hash |
125 |
|
|
126 |
_set_rec( $rec ); |
_set_ds( $rec ); |
127 |
|
|
128 |
=cut |
=cut |
129 |
|
|
130 |
my $rec; |
my $rec; |
131 |
|
|
132 |
sub _set_rec { |
sub _set_ds { |
133 |
$rec = shift or die "no record hash"; |
$rec = shift or die "no record hash"; |
134 |
} |
} |
135 |
|
|
136 |
|
=head2 get_ds |
137 |
|
|
138 |
|
Access to original record from input module |
139 |
|
|
140 |
|
my $ds = get_rec; |
141 |
|
|
142 |
|
=cut |
143 |
|
|
144 |
|
sub get_ds { |
145 |
|
return $rec; |
146 |
|
} |
147 |
|
|
148 |
=head2 _set_config |
=head2 _set_config |
149 |
|
|
150 |
Set current config hash |
Set current config hash |
181 |
|
|
182 |
=cut |
=cut |
183 |
|
|
184 |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader); |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader); |
185 |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
186 |
|
|
187 |
sub _get_ds { |
sub _get_ds { |
188 |
|
#warn "## out = ",dump($out); |
189 |
return $out; |
return $out; |
190 |
} |
} |
191 |
|
|
199 |
|
|
200 |
sub _clean_ds { |
sub _clean_ds { |
201 |
my $a = {@_}; |
my $a = {@_}; |
202 |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader) = (); |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader) = (); |
203 |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
($marc_record_offset, $marc_fetch_offset) = (0,0); |
204 |
$marc_encoding = $a->{marc_encoding}; |
$marc_encoding = $a->{marc_encoding}; |
205 |
} |
} |
304 |
|
|
305 |
=cut |
=cut |
306 |
|
|
307 |
|
my $fetch_pos; |
308 |
|
|
309 |
sub _get_marc_fields { |
sub _get_marc_fields { |
310 |
|
|
311 |
my $arg = {@_}; |
my $arg = {@_}; |
312 |
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
313 |
my $offset = $marc_fetch_offset; |
$fetch_pos = $marc_fetch_offset; |
314 |
if ($arg->{offset}) { |
if ($arg->{offset}) { |
315 |
$offset = $arg->{offset}; |
$fetch_pos = $arg->{offset}; |
316 |
} elsif($arg->{fetch_next}) { |
} elsif($arg->{fetch_next}) { |
317 |
$marc_fetch_offset++; |
$marc_fetch_offset++; |
318 |
} |
} |
321 |
|
|
322 |
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
323 |
|
|
324 |
my $marc_rec = $marc_record->[ $offset ]; |
my $marc_rec = $marc_record->[ $fetch_pos ]; |
325 |
|
|
326 |
warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
warn "## _get_marc_fields (at offset: $fetch_pos) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
327 |
|
|
328 |
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
329 |
|
|
344 |
|
|
345 |
if ($debug) { |
if ($debug) { |
346 |
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
347 |
warn "## marc_record[$offset] = ", dump( $marc_rec ), $/; |
warn "## marc_record[$fetch_pos] = ", dump( $marc_rec ), $/; |
348 |
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
349 |
warn "## subfield count = ", dump( $u ), $/; |
warn "## subfield count = ", dump( $u ), $/; |
350 |
} |
} |
425 |
return \@m; |
return \@m; |
426 |
} |
} |
427 |
|
|
428 |
|
=head2 _get_marc_leader |
429 |
|
|
430 |
|
Return leader from currently fetched record by L</_get_marc_fields> |
431 |
|
|
432 |
|
print WebPAC::Normalize::_get_marc_leader(); |
433 |
|
|
434 |
|
=cut |
435 |
|
|
436 |
|
sub _get_marc_leader { |
437 |
|
die "no fetch_pos, did you called _get_marc_fields first?" unless ( defined( $fetch_pos ) ); |
438 |
|
return $marc_leader->[ $fetch_pos ]; |
439 |
|
} |
440 |
|
|
441 |
=head2 _debug |
=head2 _debug |
442 |
|
|
443 |
Change level of debug warnings |
Change level of debug warnings |
457 |
|
|
458 |
Those functions generally have to first in your normalization file. |
Those functions generally have to first in your normalization file. |
459 |
|
|
460 |
=head2 tag |
=head2 search_display |
461 |
|
|
462 |
Define new tag for I<search> and I<display>. |
Define output for L<search> and L<display> at the same time |
463 |
|
|
464 |
tag('Title', rec('200','a') ); |
search_display('Title', rec('200','a') ); |
465 |
|
|
466 |
|
|
467 |
=cut |
=cut |
468 |
|
|
469 |
sub tag { |
sub search_display { |
470 |
my $name = shift or die "tag needs name as first argument"; |
my $name = shift or die "search_display needs name as first argument"; |
471 |
my @o = grep { defined($_) && $_ ne '' } @_; |
my @o = grep { defined($_) && $_ ne '' } @_; |
472 |
return unless (@o); |
return unless (@o); |
|
$out->{$name}->{tag} = $name; |
|
473 |
$out->{$name}->{search} = \@o; |
$out->{$name}->{search} = \@o; |
474 |
$out->{$name}->{display} = \@o; |
$out->{$name}->{display} = \@o; |
475 |
} |
} |
476 |
|
|
477 |
|
=head2 tag |
478 |
|
|
479 |
|
Old name for L<search_display>, but supported |
480 |
|
|
481 |
|
=cut |
482 |
|
|
483 |
|
sub tag { |
484 |
|
search_display( @_ ); |
485 |
|
} |
486 |
|
|
487 |
=head2 display |
=head2 display |
488 |
|
|
489 |
Define tag just for I<display> |
Define output just for I<display> |
490 |
|
|
491 |
@v = display('Title', rec('200','a') ); |
@v = display('Title', rec('200','a') ); |
492 |
|
|
493 |
=cut |
=cut |
494 |
|
|
495 |
sub display { |
sub _field { |
496 |
my $name = shift or die "display needs name as first argument"; |
my $type = shift or confess "need type -- BUG?"; |
497 |
|
my $name = shift or confess "needs name as first argument"; |
498 |
my @o = grep { defined($_) && $_ ne '' } @_; |
my @o = grep { defined($_) && $_ ne '' } @_; |
499 |
return unless (@o); |
return unless (@o); |
500 |
$out->{$name}->{tag} = $name; |
$out->{$name}->{$type} = \@o; |
|
$out->{$name}->{display} = \@o; |
|
501 |
} |
} |
502 |
|
|
503 |
|
sub display { _field( 'display', @_ ) } |
504 |
|
|
505 |
=head2 search |
=head2 search |
506 |
|
|
507 |
Prepare values just for I<search> |
Prepare values just for I<search> |
510 |
|
|
511 |
=cut |
=cut |
512 |
|
|
513 |
sub search { |
sub search { _field( 'search', @_ ) } |
514 |
my $name = shift or die "search needs name as first argument"; |
|
515 |
my @o = grep { defined($_) && $_ ne '' } @_; |
=head2 sorted |
516 |
return unless (@o); |
|
517 |
$out->{$name}->{tag} = $name; |
Insert into lists which will be automatically sorted |
518 |
$out->{$name}->{search} = \@o; |
|
519 |
} |
sorted('Title', rec('200','a') ); |
520 |
|
|
521 |
|
=cut |
522 |
|
|
523 |
|
sub sorted { _field( 'sorted', @_ ) } |
524 |
|
|
525 |
|
|
526 |
=head2 marc_leader |
=head2 marc_leader |
527 |
|
|
536 |
my ($offset,$value) = @_; |
my ($offset,$value) = @_; |
537 |
|
|
538 |
if ($offset) { |
if ($offset) { |
539 |
$leader->{ $offset } = $value; |
$marc_leader->[ $marc_record_offset ]->{ $offset } = $value; |
540 |
} else { |
} else { |
541 |
return $leader; |
|
542 |
|
if (defined($marc_leader)) { |
543 |
|
die "marc_leader not array = ", dump( $marc_leader ) unless (ref($marc_leader) eq 'ARRAY'); |
544 |
|
return $marc_leader->[ $marc_record_offset ]; |
545 |
|
} else { |
546 |
|
return; |
547 |
|
} |
548 |
|
} |
549 |
|
} |
550 |
|
|
551 |
|
=head2 marc_fixed |
552 |
|
|
553 |
|
Create control/indentifier fields with values in fixed positions |
554 |
|
|
555 |
|
marc_fixed('008', 00, '070402'); |
556 |
|
marc_fixed('008', 39, '|'); |
557 |
|
|
558 |
|
Positions not specified will be filled with spaces (C<0x20>). |
559 |
|
|
560 |
|
There will be no effort to extend last specified value to full length of |
561 |
|
field in standard. |
562 |
|
|
563 |
|
=cut |
564 |
|
|
565 |
|
sub marc_fixed { |
566 |
|
my ($f, $pos, $val) = @_; |
567 |
|
die "need marc(field, position, value)" unless defined($f) && defined($pos); |
568 |
|
|
569 |
|
confess "need val" unless defined $val; |
570 |
|
|
571 |
|
my $update = 0; |
572 |
|
|
573 |
|
map { |
574 |
|
if ($_->[0] eq $f) { |
575 |
|
my $old = $_->[1]; |
576 |
|
if (length($old) <= $pos) { |
577 |
|
$_->[1] .= ' ' x ( $pos - length($old) ) . $val; |
578 |
|
warn "## marc_fixed($f,$pos,'$val') append '$old' -> '$_->[1]'\n" if ($debug > 1); |
579 |
|
} else { |
580 |
|
$_->[1] = substr($old, 0, $pos) . $val . substr($old, $pos + length($val)); |
581 |
|
warn "## marc_fixed($f,$pos,'$val') update '$old' -> '$_->[1]'\n" if ($debug > 1); |
582 |
|
} |
583 |
|
$update++; |
584 |
|
} |
585 |
|
} @{ $marc_record->[ $marc_record_offset ] }; |
586 |
|
|
587 |
|
if (! $update) { |
588 |
|
my $v = ' ' x $pos . $val; |
589 |
|
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ]; |
590 |
|
warn "## marc_fixed($f,$pos,'val') created '$v'\n" if ($debug > 1); |
591 |
} |
} |
592 |
} |
} |
593 |
|
|
719 |
my $m = $marc_record->[ -1 ]; |
my $m = $marc_record->[ -1 ]; |
720 |
die "can't duplicate record which isn't defined" unless ($m); |
die "can't duplicate record which isn't defined" unless ($m); |
721 |
push @{ $marc_record }, dclone( $m ); |
push @{ $marc_record }, dclone( $m ); |
722 |
warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1); |
push @{ $marc_leader }, dclone( marc_leader() ); |
723 |
|
warn "## marc_duplicate = ", dump(@$marc_leader, @$marc_record), $/ if ($debug > 1); |
724 |
$marc_record_offset = $#{ $marc_record }; |
$marc_record_offset = $#{ $marc_record }; |
725 |
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
726 |
|
|
727 |
} |
} |
728 |
|
|
729 |
=head2 marc_remove |
=head2 marc_remove |
871 |
warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); |
warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); |
872 |
} |
} |
873 |
|
|
874 |
|
=head2 marc_count |
875 |
|
|
876 |
|
Return number of MARC records created using L</marc_duplicate>. |
877 |
|
|
878 |
|
print "created ", marc_count(), " records"; |
879 |
|
|
880 |
|
=cut |
881 |
|
|
882 |
|
sub marc_count { |
883 |
|
return $#{ $marc_record }; |
884 |
|
} |
885 |
|
|
886 |
|
|
887 |
=head1 Functions to extract data from input |
=head1 Functions to extract data from input |
888 |
|
|
905 |
|
|
906 |
my ($h,$include_subfields) = @_; |
my ($h,$include_subfields) = @_; |
907 |
|
|
908 |
|
# sanity and ease of use |
909 |
|
return $h if (ref($h) ne 'HASH'); |
910 |
|
|
911 |
if ( defined($h->{subfields}) ) { |
if ( defined($h->{subfields}) ) { |
912 |
my $sfs = delete $h->{subfields} || die "no subfields?"; |
my $sfs = delete $h->{subfields} || die "no subfields?"; |
913 |
my @out; |
my @out; |
1012 |
|
|
1013 |
=cut |
=cut |
1014 |
|
|
1015 |
|
sub frec { |
1016 |
|
my @out = rec(@_); |
1017 |
|
warn "rec(",dump(@_),") has more than one return value, ignoring\n" if $#out > 0; |
1018 |
|
return shift @out; |
1019 |
|
} |
1020 |
|
|
1021 |
sub rec { |
sub rec { |
1022 |
my @out; |
my @out; |
1023 |
if ($#_ == 0) { |
if ($#_ == 0) { |
1063 |
=cut |
=cut |
1064 |
|
|
1065 |
sub prefix { |
sub prefix { |
1066 |
my $p = shift or return; |
my $p = shift; |
1067 |
|
return @_ unless defined( $p ); |
1068 |
return map { $p . $_ } grep { defined($_) } @_; |
return map { $p . $_ } grep { defined($_) } @_; |
1069 |
} |
} |
1070 |
|
|
1077 |
=cut |
=cut |
1078 |
|
|
1079 |
sub suffix { |
sub suffix { |
1080 |
my $s = shift or die "suffix needs string as first argument"; |
my $s = shift; |
1081 |
|
return @_ unless defined( $s ); |
1082 |
return map { $_ . $s } grep { defined($_) } @_; |
return map { $_ . $s } grep { defined($_) } @_; |
1083 |
} |
} |
1084 |
|
|
1091 |
=cut |
=cut |
1092 |
|
|
1093 |
sub surround { |
sub surround { |
1094 |
my $p = shift or die "surround need prefix as first argument"; |
my $p = shift; |
1095 |
my $s = shift or die "surround needs suffix as second argument"; |
my $s = shift; |
1096 |
|
$p = '' unless defined( $p ); |
1097 |
|
$s = '' unless defined( $s ); |
1098 |
return map { $p . $_ . $s } grep { defined($_) } @_; |
return map { $p . $_ . $s } grep { defined($_) } @_; |
1099 |
} |
} |
1100 |
|
|
1269 |
$database_code = config(); # use _ from hash |
$database_code = config(); # use _ from hash |
1270 |
$database_name = config('name'); |
$database_name = config('name'); |
1271 |
$database_input_name = config('input name'); |
$database_input_name = config('input name'); |
|
$tag = config('input normalize tag'); |
|
1272 |
|
|
1273 |
Up to three levels are supported. |
Up to three levels are supported. |
1274 |
|
|
1394 |
|
|
1395 |
sub set { |
sub set { |
1396 |
my ($k,$v) = @_; |
my ($k,$v) = @_; |
1397 |
warn "## set ( $k => ", dump($v), " )", $/; |
warn "## set ( $k => ", dump($v), " )", $/ if ( $debug ); |
1398 |
$hash->{$k} = $v; |
$hash->{$k} = $v; |
1399 |
}; |
}; |
1400 |
|
|
1407 |
sub get { |
sub get { |
1408 |
my $k = shift || return; |
my $k = shift || return; |
1409 |
my $v = $hash->{$k}; |
my $v = $hash->{$k}; |
1410 |
warn "## get $k = ", dump( $v ), $/; |
warn "## get $k = ", dump( $v ), $/ if ( $debug ); |
1411 |
return $v; |
return $v; |
1412 |
} |
} |
1413 |
|
|
1414 |
|
=head2 count |
1415 |
|
|
1416 |
|
if ( count( @result ) == 1 ) { |
1417 |
|
# do something if only 1 result is there |
1418 |
|
} |
1419 |
|
|
1420 |
|
=cut |
1421 |
|
|
1422 |
|
sub count { |
1423 |
|
warn "## count ",dump(@_),$/ if ( $debug ); |
1424 |
|
return @_ . ''; |
1425 |
|
} |
1426 |
|
|
1427 |
# END |
# END |
1428 |
1; |
1; |