/[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 810 by dpavlin, Sun Apr 1 21:47:38 2007 UTC revision 923 by dpavlin, Wed Oct 31 00:26:43 2007 UTC
# Line 7  use Exporter 'import'; Line 7  use Exporter 'import';
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_compose marc_leader marc_fixed
14          marc_duplicate marc_remove          marc_duplicate marc_remove marc_count
15          marc_original_order          marc_original_order
16    
17          rec1 rec2 rec          rec1 rec2 rec
# Line 40  my $debug = 0; Line 41  my $debug = 0;
41    
42  WebPAC::Normalize - describe normalisaton rules using sets  WebPAC::Normalize - describe normalisaton rules using sets
43    
 =head1 VERSION  
   
 Version 0.26  
   
44  =cut  =cut
45    
46  our $VERSION = '0.26';  our $VERSION = '0.31';
47    
48  =head1 SYNOPSIS  =head1 SYNOPSIS
49    
# Line 59  means that you check it's validity befor Line 56  means that you check it's validity befor
56  C<perl -c normalize.pl>.  C<perl -c normalize.pl>.
57    
58  Normalisation can generate multiple output normalized data. For now, supported output  Normalisation can generate multiple output normalized data. For now, supported output
59  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
60  C<marc>.  C<marc>.
61    
62  =head1 FUNCTIONS  =head1 FUNCTIONS
# Line 167  Return hash formatted as data structure Line 164  Return hash formatted as data structure
164    
165  =cut  =cut
166    
167  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader);  my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader);
168  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);
169    
170  sub _get_ds {  sub _get_ds {
# Line 184  Clean data structure hash for next recor Line 181  Clean data structure hash for next recor
181    
182  sub _clean_ds {  sub _clean_ds {
183          my $a = {@_};          my $a = {@_};
184          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $leader) = ();          ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader) = ();
185          ($marc_record_offset, $marc_fetch_offset) = (0,0);          ($marc_record_offset, $marc_fetch_offset) = (0,0);
186          $marc_encoding = $a->{marc_encoding};          $marc_encoding = $a->{marc_encoding};
187  }  }
# Line 289  will return 42th copy record (if it exis Line 286  will return 42th copy record (if it exis
286    
287  =cut  =cut
288    
289    my $fetch_pos;
290    
291  sub _get_marc_fields {  sub _get_marc_fields {
292    
293          my $arg = {@_};          my $arg = {@_};
294          warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2);          warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2);
295          my $offset = $marc_fetch_offset;          $fetch_pos = $marc_fetch_offset;
296          if ($arg->{offset}) {          if ($arg->{offset}) {
297                  $offset = $arg->{offset};                  $fetch_pos = $arg->{offset};
298          } elsif($arg->{fetch_next}) {          } elsif($arg->{fetch_next}) {
299                  $marc_fetch_offset++;                  $marc_fetch_offset++;
300          }          }
# Line 304  sub _get_marc_fields { Line 303  sub _get_marc_fields {
303    
304          warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2);          warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2);
305    
306          my $marc_rec = $marc_record->[ $offset ];          my $marc_rec = $marc_record->[ $fetch_pos ];
307    
308          warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1);          warn "## _get_marc_fields (at offset: $fetch_pos) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1);
309    
310          return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0);          return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0);
311    
# Line 327  sub _get_marc_fields { Line 326  sub _get_marc_fields {
326    
327          if ($debug) {          if ($debug) {
328                  warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield );                  warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield );
329                  warn "## marc_record[$offset] = ", dump( $marc_rec ), $/;                  warn "## marc_record[$fetch_pos] = ", dump( $marc_rec ), $/;
330                  warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/;                  warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/;
331                  warn "## subfield count = ", dump( $u ), $/;                  warn "## subfield count = ", dump( $u ), $/;
332          }          }
# Line 408  sub _get_marc_fields { Line 407  sub _get_marc_fields {
407          return \@m;          return \@m;
408  }  }
409    
410    =head2 _get_marc_leader
411    
412    Return leader from currently fetched record by L</_get_marc_fields>
413    
414      print WebPAC::Normalize::_get_marc_leader();
415    
416    =cut
417    
418    sub _get_marc_leader {
419            die "no fetch_pos, did you called _get_marc_fields first?" unless ( defined( $fetch_pos ) );
420            return $marc_leader->[ $fetch_pos ];
421    }
422    
423  =head2 _debug  =head2 _debug
424    
425  Change level of debug warnings  Change level of debug warnings
# Line 427  sub _debug { Line 439  sub _debug {
439    
440  Those functions generally have to first in your normalization file.  Those functions generally have to first in your normalization file.
441    
442  =head2 tag  =head2 search_display
443    
444  Define new tag for I<search> and I<display>.  Define output for L<search> and L<display> at the same time
445    
446    tag('Title', rec('200','a') );    search_display('Title', rec('200','a') );
447    
448    
449  =cut  =cut
450    
451  sub tag {  sub search_display {
452          my $name = shift or die "tag needs name as first argument";          my $name = shift or die "search_display needs name as first argument";
453          my @o = grep { defined($_) && $_ ne '' } @_;          my @o = grep { defined($_) && $_ ne '' } @_;
454          return unless (@o);          return unless (@o);
         $out->{$name}->{tag} = $name;  
455          $out->{$name}->{search} = \@o;          $out->{$name}->{search} = \@o;
456          $out->{$name}->{display} = \@o;          $out->{$name}->{display} = \@o;
457  }  }
458    
459    =head2 tag
460    
461    Old name for L<search_display>, but supported
462    
463    =cut
464    
465    sub tag {
466            search_display( @_ );
467    }
468    
469  =head2 display  =head2 display
470    
471  Define tag just for I<display>  Define output just for I<display>
472    
473    @v = display('Title', rec('200','a') );    @v = display('Title', rec('200','a') );
474    
475  =cut  =cut
476    
477  sub display {  sub _field {
478          my $name = shift or die "display needs name as first argument";          my $type = shift or confess "need type -- BUG?";
479            my $name = shift or confess "needs name as first argument";
480          my @o = grep { defined($_) && $_ ne '' } @_;          my @o = grep { defined($_) && $_ ne '' } @_;
481          return unless (@o);          return unless (@o);
482          $out->{$name}->{tag} = $name;          $out->{$name}->{$type} = \@o;
         $out->{$name}->{display} = \@o;  
483  }  }
484    
485    sub display { _field( 'display', @_ ) }
486    
487  =head2 search  =head2 search
488    
489  Prepare values just for I<search>  Prepare values just for I<search>
# Line 469  Prepare values just for I<search> Line 492  Prepare values just for I<search>
492    
493  =cut  =cut
494    
495  sub search {  sub search { _field( 'search', @_ ) }
496          my $name = shift or die "search needs name as first argument";  
497          my @o = grep { defined($_) && $_ ne '' } @_;  =head2 sorted
498          return unless (@o);  
499          $out->{$name}->{tag} = $name;  Insert into lists which will be automatically sorted
500          $out->{$name}->{search} = \@o;  
501  }   sorted('Title', rec('200','a') );
502    
503    =cut
504    
505    sub sorted { _field( 'sorted', @_ ) }
506    
507    
508  =head2 marc_leader  =head2 marc_leader
509    
# Line 490  sub marc_leader { Line 518  sub marc_leader {
518          my ($offset,$value) = @_;          my ($offset,$value) = @_;
519    
520          if ($offset) {          if ($offset) {
521                  $leader->{ $offset } = $value;                  $marc_leader->[ $marc_record_offset ]->{ $offset } = $value;
522          } else {          } else {
523                  return $leader;                  
524                    if (defined($marc_leader)) {
525                            die "marc_leader not array = ", dump( $marc_leader ) unless (ref($marc_leader) eq 'ARRAY');
526                            return $marc_leader->[ $marc_record_offset ];
527                    } else {
528                            return;
529                    }
530            }
531    }
532    
533    =head2 marc_fixed
534    
535    Create control/indentifier fields with values in fixed positions
536    
537      marc_fixed('008', 00, '070402');
538      marc_fixed('008', 39, '|');
539    
540    Positions not specified will be filled with spaces (C<0x20>).
541    
542    There will be no effort to extend last specified value to full length of
543    field in standard.
544    
545    =cut
546    
547    sub marc_fixed {
548            my ($f, $pos, $val) = @_;
549            die "need marc(field, position, value)" unless defined($f) && defined($pos);
550    
551            confess "need val" unless defined $val;
552    
553            my $update = 0;
554    
555            map {
556                    if ($_->[0] eq $f) {
557                            my $old = $_->[1];
558                            if (length($old) <= $pos) {
559                                    $_->[1] .= ' ' x ( $pos - length($old) ) . $val;
560                                    warn "## marc_fixed($f,$pos,'$val') append '$old' -> '$_->[1]'\n" if ($debug > 1);
561                            } else {
562                                    $_->[1] = substr($old, 0, $pos) . $val . substr($old, $pos + length($val));
563                                    warn "## marc_fixed($f,$pos,'$val') update '$old' -> '$_->[1]'\n" if ($debug > 1);
564                            }
565                            $update++;
566                    }
567            } @{ $marc_record->[ $marc_record_offset ] };
568    
569            if (! $update) {
570                    my $v = ' ' x $pos . $val;
571                    push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ];
572                    warn "## marc_fixed($f,$pos,'val') created '$v'\n" if ($debug > 1);
573          }          }
574  }  }
575    
# Line 624  sub marc_duplicate { Line 701  sub marc_duplicate {
701           my $m = $marc_record->[ -1 ];           my $m = $marc_record->[ -1 ];
702           die "can't duplicate record which isn't defined" unless ($m);           die "can't duplicate record which isn't defined" unless ($m);
703           push @{ $marc_record }, dclone( $m );           push @{ $marc_record }, dclone( $m );
704           warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1);           push @{ $marc_leader }, dclone( marc_leader() );
705             warn "## marc_duplicate = ", dump(@$marc_leader, @$marc_record), $/ if ($debug > 1);
706           $marc_record_offset = $#{ $marc_record };           $marc_record_offset = $#{ $marc_record };
707           warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1);           warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1);
708    
709  }  }
710    
711  =head2 marc_remove  =head2 marc_remove
# Line 774  sub marc_original_order { Line 853  sub marc_original_order {
853          warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1);          warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1);
854  }  }
855    
856    =head2 marc_count
857    
858    Return number of MARC records created using L</marc_duplicate>.
859    
860      print "created ", marc_count(), " records";
861    
862    =cut
863    
864    sub marc_count {
865            return $#{ $marc_record };
866    }
867    
868    
869  =head1 Functions to extract data from input  =head1 Functions to extract data from input
870    
# Line 796  sub _pack_subfields_hash { Line 887  sub _pack_subfields_hash {
887    
888          my ($h,$include_subfields) = @_;          my ($h,$include_subfields) = @_;
889    
890            # sanity and ease of use
891            return $h if (ref($h) ne 'HASH');
892    
893          if ( defined($h->{subfields}) ) {          if ( defined($h->{subfields}) ) {
894                  my $sfs = delete $h->{subfields} || die "no subfields?";                  my $sfs = delete $h->{subfields} || die "no subfields?";
895                  my @out;                  my @out;
# Line 945  Prefix all values with a string Line 1039  Prefix all values with a string
1039  =cut  =cut
1040    
1041  sub prefix {  sub prefix {
1042          my $p = shift or return;          my $p = shift;
1043            return @_ unless defined( $p );
1044          return map { $p . $_ } grep { defined($_) } @_;          return map { $p . $_ } grep { defined($_) } @_;
1045  }  }
1046    
# Line 958  suffix all values with a string Line 1053  suffix all values with a string
1053  =cut  =cut
1054    
1055  sub suffix {  sub suffix {
1056          my $s = shift or die "suffix needs string as first argument";          my $s = shift;
1057            return @_ unless defined( $s );
1058          return map { $_ . $s } grep { defined($_) } @_;          return map { $_ . $s } grep { defined($_) } @_;
1059  }  }
1060    
# Line 971  surround all values with a two strings Line 1067  surround all values with a two strings
1067  =cut  =cut
1068    
1069  sub surround {  sub surround {
1070          my $p = shift or die "surround need prefix as first argument";          my $p = shift;
1071          my $s = shift or die "surround needs suffix as second argument";          my $s = shift;
1072            $p = '' unless defined( $p );
1073            $s = '' unless defined( $s );
1074          return map { $p . $_ . $s } grep { defined($_) } @_;          return map { $p . $_ . $s } grep { defined($_) } @_;
1075  }  }
1076    
# Line 1147  Consult config values stored in C<config Line 1245  Consult config values stored in C<config
1245    $database_code = config();    # use _ from hash    $database_code = config();    # use _ from hash
1246    $database_name = config('name');    $database_name = config('name');
1247    $database_input_name = config('input name');    $database_input_name = config('input name');
   $tag = config('input normalize tag');  
1248    
1249  Up to three levels are supported.  Up to three levels are supported.
1250    

Legend:
Removed from v.810  
changed lines
  Added in v.923

  ViewVC Help
Powered by ViewVC 1.1.26