r1579@llin: dpavlin | 2007-11-10 11:59:27 +0100
[webpac2] / lib / WebPAC / Normalize.pm
index 3f089fb..1d2eb65 100644 (file)
@@ -39,16 +39,20 @@ use Carp qw/confess/;
 # debugging warn(s)
 my $debug = 0;
 
+# FIXME
 use WebPAC::Normalize::ISBN;
 push @EXPORT, ( 'isbn_10', 'isbn_13' );
 
+use WebPAC::Normalize::MARC;
+push @EXPORT, ( 'marc_template' );
+
 =head1 NAME
 
 WebPAC::Normalize - describe normalisaton rules using sets
 
 =cut
 
-our $VERSION = '0.34';
+our $VERSION = '0.35';
 
 =head1 SYNOPSIS
 
@@ -134,6 +138,14 @@ sub _set_ds {
        $rec = shift or die "no record hash";
 }
 
+=head2
+
+  my $rec = _get_rec();
+
+=cut
+
+sub _get_rec { $rec };
+
 =head2 _set_config
 
 Set current config hash
@@ -860,150 +872,6 @@ sub marc_original_order {
        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 ",dump( $rec ) 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} ) {
-                       # skip everything which isn't one char subfield (e.g. 'subfields')
-                       next unless $sf =~ m/^\w$/;
-                       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 = {};
-
-               my @templates = split(/\|/, $template );
-               @templates = ( $template );
-
-               foreach my $sf ( @templates ) {
-                       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 );
-
-       foreach my $marc ( @marc_out ) {
-               warn "+++ ",dump( $marc );
-               push @{ $marc_record->[ $marc_record_offset ] }, $marc;
-       }
-}
 
 =head2 marc_count
 
@@ -1017,6 +885,17 @@ sub marc_count {
        return $#{ $marc_record };
 }
 
+=head2 _marc_push
+
+ _marc_push( $marc );
+
+=cut
+
+sub _marc_push {
+       my $marc = shift || die "no marc?";
+       push @{ $marc_record->[ $marc_record_offset ] }, $marc;
+}
+
 
 =head1 Functions to extract data from input