/[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 819 by dpavlin, Wed Apr 11 10:09:34 2007 UTC revision 983 by dpavlin, Sun Nov 4 11:12:38 2007 UTC
# Line 1  Line 1 
1  package WebPAC::Normalize;  package WebPAC::Normalize;
2  use Exporter 'import';  use Exporter 'import';
3  @EXPORT = qw/  our @EXPORT = qw/
4          _set_rec _set_lookup          _set_ds _set_lookup
5            get_ds
6          _set_load_row          _set_load_row
7          _get_ds _clean_ds          _get_ds _clean_ds
8          _debug          _debug
9          _pack_subfields_hash          _pack_subfields_hash
10    
11          tag search display          search_display search display sorted
12    
13          marc marc_indicators marc_repeatable_subfield          marc marc_indicators marc_repeatable_subfield
14          marc_compose marc_leader marc_fixed          marc_compose marc_leader marc_fixed
15          marc_duplicate marc_remove marc_count          marc_duplicate marc_remove marc_count
16          marc_original_order          marc_original_order
17    
18          rec1 rec2 rec          rec1 rec2 rec
19            frec
20          regex prefix suffix surround          regex prefix suffix surround
21          first lookup join_with          first lookup join_with
22          save_into_lookup          save_into_lookup
# Line 22  use Exporter 'import'; Line 25  use Exporter 'import';
25    
26          get set          get set
27          count          count
28    
29  /;  /;
30    
31  use warnings;  use warnings;
# Line 35  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    use WebPAC::Normalize::ISBN;
43    push @EXPORT, ( 'isbn_10', 'isbn_13' );
44    
45  =head1 NAME  =head1 NAME
46    
47  WebPAC::Normalize - describe normalisaton rules using sets  WebPAC::Normalize - describe normalisaton rules using sets
48    
 =head1 VERSION  
   
 Version 0.29  
   
49  =cut  =cut
50    
51  our $VERSION = '0.29';  our $VERSION = '0.32';
52    
53  =head1 SYNOPSIS  =head1 SYNOPSIS
54    
# Line 59  means that you check it's validity befor Line 61  means that you check it's validity befor
61  C<perl -c normalize.pl>.  C<perl -c normalize.pl>.
62    
63  Normalisation can generate multiple output normalized data. For now, supported output  Normalisation can generate multiple output normalized data. For now, supported output
64  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
65  C<marc>.  C<marc>.
66    
67  =head1 FUNCTIONS  =head1 FUNCTIONS
# Line 78  Return data structure Line 80  Return data structure
80          marc_encoding => 'utf-8',          marc_encoding => 'utf-8',
81          config => $config,          config => $config,
82          load_row_coderef => sub {          load_row_coderef => sub {
83                  my ($database,$input,$mfn) = shift;                  my ($database,$input,$mfn) = @_;
84                  $store->load_row( database => $database, input => $input, id => $mfn );                  $store->load_row( database => $database, input => $input, id => $mfn );
85          },          },
86    );    );
# Line 106  sub data_structure { Line 108  sub data_structure {
108    
109          no strict 'subs';          no strict 'subs';
110          _set_lookup( $arg->{lookup} ) if defined($arg->{lookup});          _set_lookup( $arg->{lookup} ) if defined($arg->{lookup});
111          _set_rec( $arg->{row} );          _set_ds( $arg->{row} );
112          _set_config( $arg->{config} ) if defined($arg->{config});          _set_config( $arg->{config} ) if defined($arg->{config});
113          _clean_ds( %{ $arg } );          _clean_ds( %{ $arg } );
114          $load_row_coderef = $arg->{load_row_coderef};          $load_row_coderef = $arg->{load_row_coderef};
# Line 117  sub data_structure { Line 119  sub data_structure {
119          return _get_ds();          return _get_ds();
120  }  }
121    
122  =head2 _set_rec  =head2 _set_ds
123    
124  Set current record hash  Set current record hash
125    
126    _set_rec( $rec );    _set_ds( $rec );
127    
128  =cut  =cut
129    
130  my $rec;  my $rec;
131    
132  sub _set_rec {  sub _set_ds {
133          $rec = shift or die "no record hash";          $rec = shift or die "no record hash";
134  }  }
135    
136    =head2 get_ds
137    
138    Access to original record from input module
139    
140      my $ds = get_rec;
141    
142    =cut
143    
144    sub get_ds {
145            return $rec;
146    }
147    
148  =head2 _set_config  =head2 _set_config
149    
150  Set current config hash  Set current config hash
# Line 171  my ($out, $marc_record, $marc_encoding, Line 185  my ($out, $marc_record, $marc_encoding,
185  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);  my ($marc_record_offset, $marc_fetch_offset) = (0, 0);
186    
187  sub _get_ds {  sub _get_ds {
188    #warn "## out = ",dump($out);
189          return $out;          return $out;
190  }  }
191    
# Line 442  sub _debug { Line 457  sub _debug {
457    
458  Those functions generally have to first in your normalization file.  Those functions generally have to first in your normalization file.
459    
460  =head2 tag  =head2 search_display
461    
462  Define new tag for I<search> and I<display>.  Define output for L<search> and L<display> at the same time
463    
464    tag('Title', rec('200','a') );    search_display('Title', rec('200','a') );
465    
466    
467  =cut  =cut
468    
469  sub tag {  sub search_display {
470          my $name = shift or die "tag needs name as first argument";          my $name = shift or die "search_display needs name as first argument";
471          my @o = grep { defined($_) && $_ ne '' } @_;          my @o = grep { defined($_) && $_ ne '' } @_;
472          return unless (@o);          return unless (@o);
         $out->{$name}->{tag} = $name;  
473          $out->{$name}->{search} = \@o;          $out->{$name}->{search} = \@o;
474          $out->{$name}->{display} = \@o;          $out->{$name}->{display} = \@o;
475  }  }
476    
477    =head2 tag
478    
479    Old name for L<search_display>, but supported
480    
481    =cut
482    
483    sub tag {
484            search_display( @_ );
485    }
486    
487  =head2 display  =head2 display
488    
489  Define tag just for I<display>  Define output just for I<display>
490    
491    @v = display('Title', rec('200','a') );    @v = display('Title', rec('200','a') );
492    
493  =cut  =cut
494    
495  sub display {  sub _field {
496          my $name = shift or die "display needs name as first argument";          my $type = shift or confess "need type -- BUG?";
497            my $name = shift or confess "needs name as first argument";
498          my @o = grep { defined($_) && $_ ne '' } @_;          my @o = grep { defined($_) && $_ ne '' } @_;
499          return unless (@o);          return unless (@o);
500          $out->{$name}->{tag} = $name;          $out->{$name}->{$type} = \@o;
         $out->{$name}->{display} = \@o;  
501  }  }
502    
503    sub display { _field( 'display', @_ ) }
504    
505  =head2 search  =head2 search
506    
507  Prepare values just for I<search>  Prepare values just for I<search>
# Line 484  Prepare values just for I<search> Line 510  Prepare values just for I<search>
510    
511  =cut  =cut
512    
513  sub search {  sub search { _field( 'search', @_ ) }
514          my $name = shift or die "search needs name as first argument";  
515          my @o = grep { defined($_) && $_ ne '' } @_;  =head2 sorted
516          return unless (@o);  
517          $out->{$name}->{tag} = $name;  Insert into lists which will be automatically sorted
518          $out->{$name}->{search} = \@o;  
519  }   sorted('Title', rec('200','a') );
520    
521    =cut
522    
523    sub sorted { _field( 'sorted', @_ ) }
524    
525    
526  =head2 marc_leader  =head2 marc_leader
527    
# Line 535  sub marc_fixed { Line 566  sub marc_fixed {
566          my ($f, $pos, $val) = @_;          my ($f, $pos, $val) = @_;
567          die "need marc(field, position, value)" unless defined($f) && defined($pos);          die "need marc(field, position, value)" unless defined($f) && defined($pos);
568    
569            confess "need val" unless defined $val;
570    
571          my $update = 0;          my $update = 0;
572    
573          map {          map {
574                  if ($_->[0] eq $f) {                  if ($_->[0] eq $f) {
575                          my $old = $_->[1];                          my $old = $_->[1];
576                          if (length($old) < $pos) {                          if (length($old) <= $pos) {
577                                  $_->[1] .= ' ' x ( $pos - length($old) ) . $val;                                  $_->[1] .= ' ' x ( $pos - length($old) ) . $val;
578                                  warn "## marc_fixed($f,$pos,'$val') append '$old' -> '$_->[1]'\n" if ($debug > 1);                                  warn "## marc_fixed($f,$pos,'$val') append '$old' -> '$_->[1]'\n" if ($debug > 1);
579                          } else {                          } else {
# Line 872  sub _pack_subfields_hash { Line 905  sub _pack_subfields_hash {
905    
906          my ($h,$include_subfields) = @_;          my ($h,$include_subfields) = @_;
907    
908            # sanity and ease of use
909            return $h if (ref($h) ne 'HASH');
910    
911          if ( defined($h->{subfields}) ) {          if ( defined($h->{subfields}) ) {
912                  my $sfs = delete $h->{subfields} || die "no subfields?";                  my $sfs = delete $h->{subfields} || die "no subfields?";
913                  my @out;                  my @out;
# Line 976  return scalar, not array. Line 1012  return scalar, not array.
1012    
1013  =cut  =cut
1014    
1015    sub frec {
1016            my @out = rec(@_);
1017            warn "rec(",dump(@_),") has more than one return value, ignoring\n" if $#out > 0;
1018            return shift @out;
1019    }
1020    
1021  sub rec {  sub rec {
1022          my @out;          my @out;
1023          if ($#_ == 0) {          if ($#_ == 0) {
# Line 1227  Consult config values stored in C<config Line 1269  Consult config values stored in C<config
1269    $database_code = config();    # use _ from hash    $database_code = config();    # use _ from hash
1270    $database_name = config('name');    $database_name = config('name');
1271    $database_input_name = config('input name');    $database_input_name = config('input name');
   $tag = config('input normalize tag');  
1272    
1273  Up to three levels are supported.  Up to three levels are supported.
1274    

Legend:
Removed from v.819  
changed lines
  Added in v.983

  ViewVC Help
Powered by ViewVC 1.1.26