/[webpac2]/trunk/lib/WebPAC/Normalize/MARC.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

Contents of /trunk/lib/WebPAC/Normalize/MARC.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1025 - (show annotations)
Sun Nov 11 11:54:48 2007 UTC (16 years, 5 months ago) by dpavlin
File size: 6295 byte(s)
 r1588@llin:  dpavlin | 2007-11-11 12:54:45 +0100
 more changes towards isis_template

1 package WebPAC::Normalize::MARC;
2 use Exporter 'import';
3 @EXPORT = qw/
4 marc_template
5 /;
6
7 use strict;
8 use warnings;
9
10 use Data::Dump qw/dump/;
11 use Carp qw/confess/;
12
13 use WebPAC::Normalize;
14
15 =head1 NAME
16
17 WebPAC::Normalize::MARC - create MARC/ISO2709 records
18
19 =cut
20
21 =head1 FUNCTIONS
22
23 =head2 marc_template
24
25 marc_template(
26 from => 225, to => 440,
27 subfields_rename => [
28 'a' => 'a',
29 'x' => 'x',
30 'v' => 'v',
31 'h' => 'n',
32 'i' => 'p',
33 'w' => 'v',
34 ],
35 isis_template => [
36 'a ; |v. |i',
37 'a. |i ; |w',
38 ],
39 marc_template => [
40 'a, |x ; |v. |n, |p ; |v',
41 'a ; |v. |p ; |v',
42 ],
43 );
44
45 =cut
46
47 sub marc_template {
48 my $args = {@_};
49 warn "## marc_template(",dump($args),")";
50
51 foreach ( qw/subfields_rename isis_template marc_template/ ) {
52 # warn "ref($_) = ",ref($args->{$_});
53 die "$_ not ARRAY" if defined($args->{$_}) && ref($args->{$_}) ne 'ARRAY';
54 }
55
56 die "marc_template needs isis_template or marc_template"
57 if ! defined $args->{isis_template} && ! defined $args->{marc_template};
58
59 my $rec = WebPAC::Normalize::_get_rec() || die "_get_rec?";
60 my $r = $rec->{ $args->{from} } || return;
61 die "record field ", $args->{from}, " isn't array ",dump( $rec ) unless (ref($r) eq 'ARRAY');
62
63 my @subfields_rename = @{ $args->{subfields_rename} };
64 # warn "### subfields_rename [$#subfields_rename] = ",dump( @subfields_rename );
65
66 confess "need mapping in pairs for subfields_rename"
67 if $#subfields_rename % 2 != 1;
68
69 my ( $subfields_rename, $from_subfields, $to_subfields );
70 while ( my ( $from, $to ) = splice(@subfields_rename, 0, 2) ) {
71 my ( $f, $t ) = (
72 $from_subfields->{ $from }++,
73 $to_subfields->{ $to }++
74 );
75 $subfields_rename->{ $from }->[ $f ] = [ $to => $t ];
76 }
77 warn "### subfields_rename = ",dump( $subfields_rename ),$/;
78 warn "### from_subfields = ", dump( $from_subfields ),$/;
79 warn "### to_subfields = ", dump( $to_subfields ),$/;
80
81 our $_template;
82
83 $_template->{fields_re} = {
84 isis => join('|', keys %$from_subfields ),
85 marc => join('|', keys %$to_subfields ),
86 };
87
88 my @marc_out;
89
90 sub _parse_template {
91 my ( $name, $templates ) = @_;
92
93 my $fields_re = $_template->{fields_re}->{ $name } || die "can't find $name in ",dump( $_template->{fields_re} );
94
95 foreach my $template ( @{ $templates } ) {
96 our $count = {};
97 our @order = ();
98 sub my_count {
99 my $sf = shift;
100 my $nr = $count->{$sf}++;
101 push @order, [ $sf, $nr ];
102 return $sf . $nr;
103 }
104 my $pos_template = $template;
105 $pos_template =~ s/($fields_re)/my_count($1)/ge;
106 my $count_key = dump( $count );
107 warn "### template: |$template| -> |$pos_template| count = $count_key order = ",dump( @order ),$/;
108 $_template->{$name}->{pos}->{ $count_key } = $pos_template;
109 $_template->{$name}->{order}->{ $pos_template } = [ @order ];
110 }
111 warn "### from ",dump( $templates ), " using $fields_re created ", dump( $_template );
112 }
113
114 _parse_template( 'marc', $args->{marc_template} );
115 _parse_template( 'isis', $args->{isis_template} );
116 warn "### _template = ",dump( $_template );
117
118 my $m;
119
120 foreach my $r ( @{ $rec->{ $args->{from} } } ) {
121
122 my $i1 = $r->{i1} || ' ';
123 my $i2 = $r->{i2} || ' ';
124 $m = [ $args->{to}, $i1, $i2 ];
125
126 warn "### r = ",dump( $r );
127
128 my ( $new_r, $from_count, $to_count );
129 foreach my $sf ( keys %{$r} ) {
130 # skip everything which isn't one char subfield (e.g. 'subfields')
131 next unless $sf =~ m/^\w$/;
132 my $nr = $from_count->{$sf}++;
133 my $rename_to = $subfields_rename->{ $sf } ||
134 die "can't find subfield rename for $sf/$nr in ", dump( $subfields_rename );
135 warn "### rename $sf/$nr to ", dump( $rename_to->[$nr] ), $/;
136 my ( $to_sf, $to_nr ) = @{ $rename_to->[$nr] };
137 $new_r->{ $to_sf }->[ $to_nr ] = [ $sf => $nr ];
138
139 $to_count->{ $to_sf }++;
140 }
141
142 warn "### new_r = ",dump( $new_r );
143
144 my $count_key = {
145 from => dump( $from_count ),
146 to => dump( $to_count),
147 };
148
149 warn "### count_key = ",dump( $count_key ), $/;
150
151 my $processed_templates = 0;
152
153 # this defines order of traversal
154 foreach ( qw/isis:from marc:to/ ) {
155 my ($name,$count_name) = split(/:/);
156 warn "## traverse $name $count_name\n";
157
158 my $ckey = $count_key->{$count_name} || die "can't find count_key $count_name in ",dump( $count_key );
159
160 my $template = $_template->{$name}->{pos}->{ $ckey } || next;
161 $processed_templates++;
162
163 warn "### selected template: |$template|\n";
164
165 our $fill_in = {};
166
167 my @templates = split(/\|/, $template );
168 @templates = ( $template );
169
170 foreach my $sf ( @templates ) {
171 sub fill_in {
172 my ( $name, $r, $sf, $nr ) = @_;
173 my ( $from_sf, $from_nr, $v );
174 if ( $name eq 'marc' ) {
175 ( $from_sf, $from_nr ) = @{ $new_r->{$sf}->[$nr] };
176 } else {
177 ( $from_sf, $from_nr ) = ( $sf, $nr );
178 }
179 my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr";
180 warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ), $/;
181 if ( ref( $v ) eq 'ARRAY' ) {
182 $fill_in->{$sf}->[$nr] = $v->[$from_nr];
183 return $v->[$from_nr];
184 } elsif ( $from_nr == 0 ) {
185 $fill_in->{$sf}->[$nr] = $v;
186 return $v;
187 } else {
188 die "requested subfield $from_sf/$from_nr but it's ",dump( $v );
189 }
190 }
191 my $fields_re = $_template->{fields_re}->{ $name } || die "can't find $name in ",dump( $_template->{fields_re} );
192 warn "#### $sf <<<< $fields_re\n";
193 $sf =~ s/($fields_re)(\d+)/fill_in($name,$r,$1,$2)/ge;
194 warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/;
195 }
196
197 warn "## template: |$template|\n## _template->$name = ",dump( $_template->{$name} );
198
199 $sf_pos = $#m;
200
201 foreach my $sf ( @{ $_template->{$name}->{order}->{$template} } ) {
202 my ( $sf, $nr ) = @$sf;
203 my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr";
204 warn "++ $sf/$nr |$v|\n";
205 push @$m, ( $sf, $v );
206 }
207
208 warn "#### >>>> created MARC record: ", dump( $m );
209
210 push @marc_out, $m;
211 }
212
213 die "I don't have template for fields ",dump( $count_key ), "\n## available templates\n", dump( $_template ) unless $processed_templates;
214 warn ">>> $processed_templates templates applied to data\n";
215 }
216
217
218 warn "### marc_template produced following MARC records: ",dump( @marc_out );
219
220 foreach my $marc ( @marc_out ) {
221 warn "+++ ",dump( $marc );
222 WebPAC::Normalize::_marc_push( $marc );
223 }
224 }
225
226 1;

  ViewVC Help
Powered by ViewVC 1.1.26