/[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 572 by dpavlin, Mon Jul 3 14:32:40 2006 UTC revision 589 by dpavlin, Fri Jul 7 21:48:09 2006 UTC
# Line 8  use Exporter 'import'; Line 8  use Exporter 'import';
8          tag search display          tag search display
9          marc marc_indicators marc_repeatable_subfield          marc marc_indicators marc_repeatable_subfield
10          marc_compose marc_leader          marc_compose marc_leader
11            marc_duplicate marc_remove
12    
13          rec1 rec2 rec          rec1 rec2 rec
14          regex prefix suffix surround          regex prefix suffix surround
# Line 22  use strict; Line 23  use strict;
23  #use base qw/WebPAC::Common/;  #use base qw/WebPAC::Common/;
24  use Data::Dump qw/dump/;  use Data::Dump qw/dump/;
25  use Encode qw/from_to/;  use Encode qw/from_to/;
26    use Storable qw/dclone/;
27    
28  # debugging warn(s)  # debugging warn(s)
29  my $debug = 0;  my $debug = 0;
# Line 33  WebPAC::Normalize - describe normalisato Line 35  WebPAC::Normalize - describe normalisato
35    
36  =head1 VERSION  =head1 VERSION
37    
38  Version 0.09  Version 0.12
39    
40  =cut  =cut
41    
42  our $VERSION = '0.09';  our $VERSION = '0.12';
43    
44  =head1 SYNOPSIS  =head1 SYNOPSIS
45    
# Line 117  Return hash formatted as data structure Line 119  Return hash formatted as data structure
119    
120  =cut  =cut
121    
122  my ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators);  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators);
123    my ($marc_record_offset, $marc_fetch_offset) = (0, 0);
124    
125  sub _get_ds {  sub _get_ds {
126          return $out;          return $out;
# Line 134  Clean data structure hash for next recor Line 137  Clean data structure hash for next recor
137  sub _clean_ds {  sub _clean_ds {
138          my $a = {@_};          my $a = {@_};
139          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = ();          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = ();
140            ($marc_record_offset, $marc_fetch_offset) = (0,0);
141          $marc_encoding = $a->{marc_encoding};          $marc_encoding = $a->{marc_encoding};
142  }  }
143    
# Line 182  and following rules: Line 186  and following rules:
186  which might not be what you have in mind. If you need repeatable subfield,  which might not be what you have in mind. If you need repeatable subfield,
187  define it using C<marc_repeatable_subfield> like this:  define it using C<marc_repeatable_subfield> like this:
188    
189  ....    marc_repeatable_subfield('900','a');
190      marc('900','a', rec('200','a') );
191      marc('900','b', rec('200','b') );
192      marc('900','c', rec('200','c') );
193    
194    will create:
195    
196      900a-1 900a-2 900a-3 900b-1 900c-1
197      900b-2
198    
199    There is also support for returning next or specific using:
200    
201      while (my $mf = WebPAC::Normalize:_get_marc_fields( fetch_next => 1 ) ) {
202            # do something with $mf
203      }
204    
205    will always return fields from next MARC record or
206    
207      my $mf = WebPAC::Normalize::_get_marc_fields( offset => 42 );
208    
209    will return 42th copy record (if it exists).
210    
211  =cut  =cut
212    
213  sub _get_marc_fields {  sub _get_marc_fields {
214    
215          return if (! $marc_record || ref($marc_record) ne 'ARRAY' || $#{ $marc_record } < 0);          my $arg = {@_};
216            warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2);
217            my $offset = $marc_fetch_offset;
218            if ($arg->{offset}) {
219                    $offset = $arg->{offset};
220            } elsif($arg->{fetch_next}) {
221                    $marc_fetch_offset++;
222            }
223    
224            return if (! $marc_record || ref($marc_record) ne 'ARRAY');
225    
226            warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2);
227    
228            my $marc_rec = $marc_record->[ $offset ];
229    
230            warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1);
231    
232            return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0);
233    
234          # first, sort all existing fields          # first, sort all existing fields
235          # XXX might not be needed, but modern perl might randomize elements in hash          # XXX might not be needed, but modern perl might randomize elements in hash
236          my @sorted_marc_record = sort {          my @sorted_marc_record = sort {
237                  $a->[0] . ( $a->[3] || '' ) cmp $b->[0] . ( $b->[3] || '')                  $a->[0] . ( $a->[3] || '' ) cmp $b->[0] . ( $b->[3] || '')
238          } @{ $marc_record };          } @{ $marc_rec };
239    
240          @sorted_marc_record = @{ $marc_record };        ### FIXME disable sorting          @sorted_marc_record = @{ $marc_rec };   ### FIXME disable sorting
241                    
242          # output marc fields          # output marc fields
243          my @m;          my @m;
# Line 206  sub _get_marc_fields { Line 247  sub _get_marc_fields {
247          map { $u->{ $_->[0] . ( $_->[3] || '')  }++ } @sorted_marc_record;          map { $u->{ $_->[0] . ( $_->[3] || '')  }++ } @sorted_marc_record;
248    
249          if ($debug) {          if ($debug) {
250                  warn "## marc_repeatable_subfield ", dump( $marc_repeatable_subfield ), $/;                  warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield );
251                  warn "## marc_record ", dump( $marc_record ), $/;                  warn "## marc_record[$offset] = ", dump( $marc_rec ), $/;
252                  warn "## sorted_marc_record ", dump( \@sorted_marc_record ), $/;                  warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/;
253                  warn "## subfield count ", dump( $u ), $/;                  warn "## subfield count = ", dump( $u ), $/;
254          }          }
255    
256          my $len = $#sorted_marc_record;          my $len = $#sorted_marc_record;
# Line 227  sub _get_marc_fields { Line 268  sub _get_marc_fields {
268                  # mark it visited                  # mark it visited
269                  $visited->{$i}++;                  $visited->{$i}++;
270    
271                  my $row = $sorted_marc_record[$i];                  my $row = dclone( $sorted_marc_record[$i] );
272    
273                  # field and subfield which is key for                  # field and subfield which is key for
274                  # marc_repeatable_subfield and u                  # marc_repeatable_subfield and u
# Line 285  sub _get_marc_fields { Line 326  sub _get_marc_fields {
326                  warn "## saved/3 ", dump( $field ),$/ if ($debug);                  warn "## saved/3 ", dump( $field ),$/ if ($debug);
327          }          }
328    
329          return @m;          return \@m;
330  }  }
331    
332  =head2 _debug  =head2 _debug
# Line 400  sub marc { Line 441  sub marc {
441                  from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding);                  from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding);
442                  my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' ');                  my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' ');
443                  if (defined $sf) {                  if (defined $sf) {
444                          push @{ $marc_record }, [ $f, $i1, $i2, $sf => $v ];                          push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $i1, $i2, $sf => $v ];
445                  } else {                  } else {
446                          push @{ $marc_record }, [ $f, $v ];                          push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ];
447                  }                  }
448          }          }
449  }  }
# Line 463  sub marc_compose { Line 504  sub marc_compose {
504          my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' ');          my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' ');
505          my $m = [ $f, $i1, $i2 ];          my $m = [ $f, $i1, $i2 ];
506    
507            warn "### marc_compose input subfields = ", dump(@_),$/ if ($debug > 2);
508    
509          while (@_) {          while (@_) {
510                  my $sf = shift or die "marc_compose $f needs subfield";                  my $sf = shift or die "marc_compose $f needs subfield";
511                  my $v = shift;                  my $v = shift;
# Line 473  sub marc_compose { Line 516  sub marc_compose {
516                  warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1);                  warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1);
517          }          }
518    
519          warn "## marc_compose(d) ", dump( $m ),$/ if ($debug > 1);          warn "## marc_compose current marc = ", dump( $m ),$/ if ($debug > 1);
520    
521            push @{ $marc_record->[ $marc_record_offset ] }, $m if ($#{$m} > 2);
522    }
523    
524    =head2 marc_duplicate
525    
526    Generate copy of current MARC record and continue working on copy
527    
528      marc_duplicate();
529    
530    Copies can be accessed using C<< _get_marc_fields( fetch_next => 1 ) >> or
531    C<< _get_marc_fields( offset => 42 ) >>.
532    
533    =cut
534    
535          push @{ $marc_record }, $m if ($#{$m} > 2);  sub marc_duplicate {
536             my $m = $marc_record->[ -1 ];
537             die "can't duplicate record which isn't defined" unless ($m);
538             push @{ $marc_record }, dclone( $m );
539             warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1);
540             $marc_record_offset = $#{ $marc_record };
541             warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1);
542  }  }
543    
544    =head2 marc_remove
545    
546    Remove some field or subfield from MARC record.
547    
548      marc_remove('200');
549      marc_remove('200','a');
550    
551    This will erase field C<200> or C<200^a> from current MARC record.
552    
553    This is useful after calling C<marc_duplicate> or on it's own (but, you
554    should probably just remove that subfield definition if you are not
555    using C<marc_duplicate>).
556    
557    FIXME: support fields < 10.
558    
559    =cut
560    
561    sub marc_remove {
562            my ($f, $sf) = @_;
563    
564            die "marc_remove needs record number" unless defined($f);
565    
566            my $marc = $marc_record->[ $marc_record_offset ];
567    
568            warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2);
569    
570            my $i = 0;
571            foreach ( 0 .. $#{ $marc } ) {
572                    last unless (defined $marc->[$i]);
573                    warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3);
574                    if ($marc->[$i]->[0] eq $f) {
575                            if (! defined $sf) {
576                                    # remove whole field
577                                    splice @$marc, $i, 1;
578                                    warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3);
579                                    $i--;
580                            } else {
581                                    foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) {
582                                            my $o = ($j * 2) + 3;
583                                            if ($marc->[$i]->[$o] eq $sf) {
584                                                    # remove subfield
585                                                    splice @{$marc->[$i]}, $o, 2;
586                                                    warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3);
587                                                    # is record now empty?
588                                                    if ($#{ $marc->[$i] } == 2) {
589                                                            splice @$marc, $i, 1;
590                                                            warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3);
591                                                            $i--;
592                                                    };
593                                            }
594                                    }
595                            }
596                    }
597                    $i++;
598            }
599    
600            warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2);
601    
602            $marc_record->[ $marc_record_offset ] = $marc;
603    
604            warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1);
605    }
606    
607  =head1 Functions to extract data from input  =head1 Functions to extract data from input
608    
# Line 524  sub rec2 { Line 649  sub rec2 {
649          my $f = shift;          my $f = shift;
650          return unless (defined($rec && $rec->{$f}));          return unless (defined($rec && $rec->{$f}));
651          my $sf = shift;          my $sf = shift;
652          return map { $_->{$sf} } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} };          return map {
653                    if (ref($_->{$sf}) eq 'ARRAY') {
654                            @{ $_->{$sf} };
655                    } else {
656                            $_->{$sf};
657                    }
658            } grep { ref($_) eq 'HASH' && $_->{$sf} } @{ $rec->{$f} };
659  }  }
660    
661  =head2 rec  =head2 rec
# Line 537  syntaxtic sugar for Line 668  syntaxtic sugar for
668  =cut  =cut
669    
670  sub rec {  sub rec {
671            my @out;
672          if ($#_ == 0) {          if ($#_ == 0) {
673                  return rec1(@_);                  @out = rec1(@_);
674          } elsif ($#_ == 1) {          } elsif ($#_ == 1) {
675                  return rec2(@_);                  @out = rec2(@_);
676            }
677            if (@out) {
678                    return @out;
679            } else {
680                    return '';
681          }          }
682  }  }
683    
# Line 646  Joins walues with some delimiter Line 783  Joins walues with some delimiter
783    
784  sub join_with {  sub join_with {
785          my $d = shift;          my $d = shift;
786          return join($d, grep { defined($_) && $_ ne '' } @_);          warn "### join_with('$d',",dump(@_),")\n" if ($debug > 2);
787            my $v = join($d, grep { defined($_) && $_ ne '' } @_);
788            return '' unless defined($v);
789            return $v;
790  }  }
791    
792  =head2 split_rec_on  =head2 split_rec_on

Legend:
Removed from v.572  
changed lines
  Added in v.589

  ViewVC Help
Powered by ViewVC 1.1.26