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 |
_set_load_row |
_set_load_row |
6 |
_get_ds _clean_ds |
_get_ds _clean_ds |
7 |
_debug |
_debug |
8 |
_pack_subfields_hash |
_pack_subfields_hash |
9 |
|
|
10 |
tag search display |
search_display search display sorted |
11 |
|
|
12 |
marc marc_indicators marc_repeatable_subfield |
marc marc_indicators marc_repeatable_subfield |
13 |
marc_compose marc_leader marc_fixed |
marc_compose marc_leader marc_fixed |
14 |
marc_duplicate marc_remove marc_count |
marc_duplicate marc_remove marc_count |
15 |
marc_original_order |
marc_original_order |
16 |
|
|
17 |
rec1 rec2 rec |
rec1 rec2 rec |
18 |
|
frec |
19 |
regex prefix suffix surround |
regex prefix suffix surround |
20 |
first lookup join_with |
first lookup join_with |
21 |
save_into_lookup |
save_into_lookup |
24 |
|
|
25 |
get set |
get set |
26 |
count |
count |
27 |
|
|
28 |
/; |
/; |
29 |
|
|
30 |
use warnings; |
use warnings; |
38 |
# debugging warn(s) |
# debugging warn(s) |
39 |
my $debug = 0; |
my $debug = 0; |
40 |
|
|
41 |
|
use WebPAC::Normalize::ISBN; |
42 |
|
push @EXPORT, ( 'isbn_10', 'isbn_13' ); |
43 |
|
|
44 |
=head1 NAME |
=head1 NAME |
45 |
|
|
46 |
WebPAC::Normalize - describe normalisaton rules using sets |
WebPAC::Normalize - describe normalisaton rules using sets |
47 |
|
|
|
=head1 VERSION |
|
|
|
|
|
Version 0.29 |
|
|
|
|
48 |
=cut |
=cut |
49 |
|
|
50 |
our $VERSION = '0.29'; |
our $VERSION = '0.32'; |
51 |
|
|
52 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
53 |
|
|
60 |
C<perl -c normalize.pl>. |
C<perl -c normalize.pl>. |
61 |
|
|
62 |
Normalisation can generate multiple output normalized data. For now, supported output |
Normalisation can generate multiple output normalized data. For now, supported output |
63 |
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 |
64 |
C<marc>. |
C<marc>. |
65 |
|
|
66 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
79 |
marc_encoding => 'utf-8', |
marc_encoding => 'utf-8', |
80 |
config => $config, |
config => $config, |
81 |
load_row_coderef => sub { |
load_row_coderef => sub { |
82 |
my ($database,$input,$mfn) = shift; |
my ($database,$input,$mfn) = @_; |
83 |
$store->load_row( database => $database, input => $input, id => $mfn ); |
$store->load_row( database => $database, input => $input, id => $mfn ); |
84 |
}, |
}, |
85 |
); |
); |
105 |
die "need row argument" unless ($arg->{row}); |
die "need row argument" unless ($arg->{row}); |
106 |
die "need normalisation argument" unless ($arg->{rules}); |
die "need normalisation argument" unless ($arg->{rules}); |
107 |
|
|
|
no strict 'subs'; |
|
108 |
_set_lookup( $arg->{lookup} ) if defined($arg->{lookup}); |
_set_lookup( $arg->{lookup} ) if defined($arg->{lookup}); |
109 |
_set_rec( $arg->{row} ); |
_set_ds( $arg->{row} ); |
110 |
_set_config( $arg->{config} ) if defined($arg->{config}); |
_set_config( $arg->{config} ) if defined($arg->{config}); |
111 |
_clean_ds( %{ $arg } ); |
_clean_ds( %{ $arg } ); |
112 |
$load_row_coderef = $arg->{load_row_coderef}; |
$load_row_coderef = $arg->{load_row_coderef}; |
113 |
|
|
114 |
eval "$arg->{rules}"; |
no strict 'subs'; |
115 |
|
no warnings 'redefine'; |
116 |
|
eval "$arg->{rules};"; |
117 |
die "error evaling $arg->{rules}: $@\n" if ($@); |
die "error evaling $arg->{rules}: $@\n" if ($@); |
118 |
|
|
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 |
|
|
173 |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
174 |
|
|
175 |
sub _get_ds { |
sub _get_ds { |
176 |
|
#warn "## out = ",dump($out); |
177 |
return $out; |
return $out; |
178 |
} |
} |
179 |
|
|
445 |
|
|
446 |
Those functions generally have to first in your normalization file. |
Those functions generally have to first in your normalization file. |
447 |
|
|
448 |
=head2 tag |
=head2 search_display |
449 |
|
|
450 |
Define new tag for I<search> and I<display>. |
Define output for L<search> and L<display> at the same time |
451 |
|
|
452 |
tag('Title', rec('200','a') ); |
search_display('Title', rec('200','a') ); |
453 |
|
|
454 |
|
|
455 |
=cut |
=cut |
456 |
|
|
457 |
sub tag { |
sub search_display { |
458 |
my $name = shift or die "tag needs name as first argument"; |
my $name = shift or die "search_display needs name as first argument"; |
459 |
my @o = grep { defined($_) && $_ ne '' } @_; |
my @o = grep { defined($_) && $_ ne '' } @_; |
460 |
return unless (@o); |
return unless (@o); |
|
$out->{$name}->{tag} = $name; |
|
461 |
$out->{$name}->{search} = \@o; |
$out->{$name}->{search} = \@o; |
462 |
$out->{$name}->{display} = \@o; |
$out->{$name}->{display} = \@o; |
463 |
} |
} |
464 |
|
|
465 |
|
=head2 tag |
466 |
|
|
467 |
|
Old name for L<search_display>, but supported |
468 |
|
|
469 |
|
=cut |
470 |
|
|
471 |
|
sub tag { |
472 |
|
search_display( @_ ); |
473 |
|
} |
474 |
|
|
475 |
=head2 display |
=head2 display |
476 |
|
|
477 |
Define tag just for I<display> |
Define output just for I<display> |
478 |
|
|
479 |
@v = display('Title', rec('200','a') ); |
@v = display('Title', rec('200','a') ); |
480 |
|
|
481 |
=cut |
=cut |
482 |
|
|
483 |
sub display { |
sub _field { |
484 |
my $name = shift or die "display needs name as first argument"; |
my $type = shift or confess "need type -- BUG?"; |
485 |
|
my $name = shift or confess "needs name as first argument"; |
486 |
my @o = grep { defined($_) && $_ ne '' } @_; |
my @o = grep { defined($_) && $_ ne '' } @_; |
487 |
return unless (@o); |
return unless (@o); |
488 |
$out->{$name}->{tag} = $name; |
$out->{$name}->{$type} = \@o; |
|
$out->{$name}->{display} = \@o; |
|
489 |
} |
} |
490 |
|
|
491 |
|
sub display { _field( 'display', @_ ) } |
492 |
|
|
493 |
=head2 search |
=head2 search |
494 |
|
|
495 |
Prepare values just for I<search> |
Prepare values just for I<search> |
498 |
|
|
499 |
=cut |
=cut |
500 |
|
|
501 |
sub search { |
sub search { _field( 'search', @_ ) } |
502 |
my $name = shift or die "search needs name as first argument"; |
|
503 |
my @o = grep { defined($_) && $_ ne '' } @_; |
=head2 sorted |
504 |
return unless (@o); |
|
505 |
$out->{$name}->{tag} = $name; |
Insert into lists which will be automatically sorted |
506 |
$out->{$name}->{search} = \@o; |
|
507 |
} |
sorted('Title', rec('200','a') ); |
508 |
|
|
509 |
|
=cut |
510 |
|
|
511 |
|
sub sorted { _field( 'sorted', @_ ) } |
512 |
|
|
513 |
|
|
514 |
=head2 marc_leader |
=head2 marc_leader |
515 |
|
|
554 |
my ($f, $pos, $val) = @_; |
my ($f, $pos, $val) = @_; |
555 |
die "need marc(field, position, value)" unless defined($f) && defined($pos); |
die "need marc(field, position, value)" unless defined($f) && defined($pos); |
556 |
|
|
557 |
|
confess "need val" unless defined $val; |
558 |
|
|
559 |
my $update = 0; |
my $update = 0; |
560 |
|
|
561 |
map { |
map { |
562 |
if ($_->[0] eq $f) { |
if ($_->[0] eq $f) { |
563 |
my $old = $_->[1]; |
my $old = $_->[1]; |
564 |
if (length($old) < $pos) { |
if (length($old) <= $pos) { |
565 |
$_->[1] .= ' ' x ( $pos - length($old) ) . $val; |
$_->[1] .= ' ' x ( $pos - length($old) ) . $val; |
566 |
warn "## marc_fixed($f,$pos,'$val') append '$old' -> '$_->[1]'\n" if ($debug > 1); |
warn "## marc_fixed($f,$pos,'$val') append '$old' -> '$_->[1]'\n" if ($debug > 1); |
567 |
} else { |
} else { |
1016 |
} |
} |
1017 |
} |
} |
1018 |
|
|
1019 |
|
=head2 frec |
1020 |
|
|
1021 |
|
Returns first value from field |
1022 |
|
|
1023 |
|
$v = frec('200'); |
1024 |
|
$v = frec('200','a'); |
1025 |
|
|
1026 |
|
=cut |
1027 |
|
|
1028 |
|
sub frec { |
1029 |
|
my @out = rec(@_); |
1030 |
|
warn "rec(",dump(@_),") has more than one return value, ignoring\n" if $#out > 0; |
1031 |
|
return shift @out; |
1032 |
|
} |
1033 |
|
|
1034 |
=head2 regex |
=head2 regex |
1035 |
|
|
1036 |
Apply regex to some or all values |
Apply regex to some or all values |
1266 |
$database_code = config(); # use _ from hash |
$database_code = config(); # use _ from hash |
1267 |
$database_name = config('name'); |
$database_name = config('name'); |
1268 |
$database_input_name = config('input name'); |
$database_input_name = config('input name'); |
|
$tag = config('input normalize tag'); |
|
1269 |
|
|
1270 |
Up to three levels are supported. |
Up to three levels are supported. |
1271 |
|
|