/[webpac2]/trunk/lib/WebPAC/Normalize.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/lib/WebPAC/Normalize.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1017 by dpavlin, Fri Nov 9 23:41:39 2007 UTC revision 1021 by dpavlin, Sat Nov 10 11:11:16 2007 UTC
# Line 39  use Carp qw/confess/; Line 39  use Carp qw/confess/;
39  # debugging warn(s)  # debugging warn(s)
40  my $debug = 0;  my $debug = 0;
41    
42    # FIXME
43  use WebPAC::Normalize::ISBN;  use WebPAC::Normalize::ISBN;
44  push @EXPORT, ( 'isbn_10', 'isbn_13' );  push @EXPORT, ( 'isbn_10', 'isbn_13' );
45    
46    use WebPAC::Normalize::MARC;
47    push @EXPORT, ( 'marc_template' );
48    
49  =head1 NAME  =head1 NAME
50    
51  WebPAC::Normalize - describe normalisaton rules using sets  WebPAC::Normalize - describe normalisaton rules using sets
52    
53  =cut  =cut
54    
55  our $VERSION = '0.34';  our $VERSION = '0.35';
56    
57  =head1 SYNOPSIS  =head1 SYNOPSIS
58    
# Line 134  sub _set_ds { Line 138  sub _set_ds {
138          $rec = shift or die "no record hash";          $rec = shift or die "no record hash";
139  }  }
140    
141    =head2
142    
143      my $rec = _get_rec();
144    
145    =cut
146    
147    sub _get_rec { $rec };
148    
149  =head2 _set_config  =head2 _set_config
150    
151  Set current config hash  Set current config hash
# Line 860  sub marc_original_order { Line 872  sub marc_original_order {
872          warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1);          warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1);
873  }  }
874    
 =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} ) {  
                         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 );  
   
         foreach my $marc ( @marc_out ) {  
                 warn "+++ ",dump( $marc );  
                 push @{ $marc_record->[ $marc_record_offset ] }, $marc;  
         }  
 }  
875    
876  =head2 marc_count  =head2 marc_count
877    
# Line 1012  sub marc_count { Line 885  sub marc_count {
885          return $#{ $marc_record };          return $#{ $marc_record };
886  }  }
887    
888    =head2 _marc_push
889    
890     _marc_push( $marc );
891    
892    =cut
893    
894    sub _marc_push {
895            my $marc = shift || die "no marc?";
896            push @{ $marc_record->[ $marc_record_offset ] }, $marc;
897    }
898    
899    
900  =head1 Functions to extract data from input  =head1 Functions to extract data from input
901    

Legend:
Removed from v.1017  
changed lines
  Added in v.1021

  ViewVC Help
Powered by ViewVC 1.1.26