--- trunk/lib/WebPAC/Normalize.pm 2007/11/07 09:19:29 1012 +++ trunk/lib/WebPAC/Normalize.pm 2007/11/07 11:54:34 1013 @@ -13,6 +13,7 @@ marc_compose marc_leader marc_fixed marc_duplicate marc_remove marc_count marc_original_order + marc_template rec1 rec2 rec frec @@ -47,7 +48,7 @@ =cut -our $VERSION = '0.32'; +our $VERSION = '0.33'; =head1 SYNOPSIS @@ -859,6 +860,142 @@ warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); } +=head2 marc_template + +=cut + +sub marc_template { + my $args = {@_}; + warn "## marc_template(",dump($args),")"; + + foreach ( qw/subfields_rename marc_template/ ) { +# warn "ref($_) = ",ref($args->{$_}); + die "$_ not ARRAY" if ref($args->{$_}) ne 'ARRAY'; + } + + my $r = $rec->{ $args->{from} }; # || return; + die "record field ", $args->{from}, " isn't array\n" unless (ref($r) eq 'ARRAY'); + + my @subfields_rename = @{ $args->{subfields_rename} }; +# warn "### subfields_rename [$#subfields_rename] = ",dump( @subfields_rename ); + + confess "need mapping in pairs for subfields_rename" + if $#subfields_rename % 2 != 1; + + my ( $subfields_rename, $from_subfields, $to_subfields ); + while ( my ( $from, $to ) = splice(@subfields_rename, 0, 2) ) { + my ( $f, $t ) = ( + $from_subfields->{ $from }++, + $to_subfields->{ $to }++ + ); + $subfields_rename->{ $from }->[ $f ] = [ $to => $t ]; + } + warn "### subfields_rename = ",dump( $subfields_rename ),$/; + warn "### from_subfields = ", dump( $from_subfields ),$/; + warn "### to_subfields = ", dump( $to_subfields ),$/; + + my $fields_re = join('|', keys %$to_subfields ); + + my $pos_templates; + my $count; + my @marc_order; + my $marc_template_order; + my $fill_in; + my @marc_out; + + foreach my $template ( @{ $args->{marc_template} } ) { + $count = {}; + @marc_order = (); + sub my_count { + my $sf = shift; + my $nr = $count->{$sf}++; + push @marc_order, [ $sf, $nr ]; + return $sf . $nr; + } + my $pos_template = $template; + $pos_template =~ s/($fields_re)/my_count($1)/ge; + my $count_key = dump( $count ); + warn "### template: |$template| -> |$pos_template| count = $count_key marc_order = ",dump( @marc_order ),$/; + $pos_templates->{ $count_key } = $pos_template; + $marc_template_order->{ $pos_template } = [ @marc_order ]; + } + warn "### from ",dump( $args->{marc_template} ), " created ", dump( $pos_templates ), " and ", dump( $marc_template_order ); + + my $m; + + foreach my $r ( @{ $rec->{ $args->{from} } } ) { + + my $i1 = $r->{i1} || ' '; + my $i2 = $r->{i2} || ' '; + $m = [ $args->{to}, $i1, $i2 ]; + + warn "### r = ",dump( $r ); + + my ( $new_r, $from_count, $to_count ); + foreach my $sf ( keys %{$r} ) { + my $nr = $from_count->{$sf}++; + my $rename_to = $subfields_rename->{ $sf }; # || +# die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename ); + warn "### rename $sf/$nr to ", dump( $rename_to->[$nr] ), $/; + my ( $to_sf, $to_nr ) = @{ $rename_to->[$nr] }; + $new_r->{ $to_sf }->[ $to_nr ] = [ $sf => $nr ]; + + $to_count->{ $to_sf }++; + } + + warn "### new_r = ",dump( $new_r ); + + my $from_count_key = dump( $to_count ); + + warn "### from_count = ",dump( $from_count ), $/; + warn "### to_count = ",dump( $to_count ), $/; + + my $template = $pos_templates->{ $from_count_key } || + die "I don't have template for:\n$from_count_key\n## available templates\n", dump( $pos_templates ); + + warn "### selected template: |$template|\n"; + + $fill_in = {}; + + foreach my $sf ( split(/\|/, $template ) ) { + sub fill_in { + my ( $r, $sf, $nr ) = @_; + my ( $from_sf, $from_nr ) = @{ $new_r->{ $sf }->[ $nr ] }; + my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr"; + warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ), $/; + if ( ref( $v ) eq 'ARRAY' ) { + $fill_in->{$sf}->[$nr] = $v->[$from_nr]; + return $v->[$from_nr]; + } elsif ( $from_nr == 0 ) { + $fill_in->{$sf}->[$nr] = $v; + return $v; + } else { + die "requested subfield $from_sf/$from_nr but it's ",dump( $v ); + } + } + warn "#### $sf <<<< $fields_re\n"; + $sf =~ s/($fields_re)(\d+)/fill_in($r,$1,$2)/ge; + warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/; + } + + warn "## template: |$template|\n## marc_template_order = ",dump( $marc_template_order ); + + foreach my $sf ( @{ $marc_template_order->{$template} } ) { + my ( $sf, $nr ) = @$sf; + my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr"; + warn "++ $sf/$nr |$v|\n"; + push @$m, [ $sf, $v ]; + } + + warn "#### >>>> created marc: ", dump( $m ); + + push @marc_out, $m; + } + + warn "### marc_template produced: ",dump( @marc_out ); + return @marc_out; +} + =head2 marc_count Return number of MARC records created using L.