16 |
use strict; |
use strict; |
17 |
|
|
18 |
#use base qw/WebPAC::Common/; |
#use base qw/WebPAC::Common/; |
19 |
use Data::Dumper; |
use Data::Dump qw/dump/; |
20 |
use Encode qw/from_to/; |
use Encode qw/from_to/; |
21 |
|
|
22 |
|
# debugging warn(s) |
23 |
|
my $debug = 0; |
24 |
|
|
25 |
|
|
26 |
=head1 NAME |
=head1 NAME |
27 |
|
|
28 |
WebPAC::Normalize - describe normalisaton rules using sets |
WebPAC::Normalize - describe normalisaton rules using sets |
129 |
|
|
130 |
sub _clean_ds { |
sub _clean_ds { |
131 |
my $a = {@_}; |
my $a = {@_}; |
132 |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (undef); |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (); |
133 |
$marc_encoding = $a->{marc_encoding}; |
$marc_encoding = $a->{marc_encoding}; |
134 |
} |
} |
135 |
|
|
167 |
=cut |
=cut |
168 |
|
|
169 |
sub _get_marc_fields { |
sub _get_marc_fields { |
170 |
|
|
171 |
|
|
172 |
|
# first, sort all existing fields |
173 |
|
# XXX might not be needed, but modern perl might randomize elements in hash |
174 |
|
my @sorted_marc_record = sort { |
175 |
|
$a->[0] . $a->[3] cmp $b->[0] . $b->[3] |
176 |
|
} @{ $marc_record }; |
177 |
|
|
178 |
|
# output marc fields |
179 |
my @m; |
my @m; |
180 |
my $last; |
|
181 |
foreach my $row (@{ $marc_record }) { |
# count unique field-subfields (used for offset when walking to next subfield) |
182 |
if ($last && |
my $u; |
183 |
$last->[0] eq $row->[0] && # check if field is same |
map { $u->{ $_->[0] . $_->[3] }++ } @sorted_marc_record; |
184 |
$last->[1] eq $row->[1] && # check for i1 |
|
185 |
$last->[2] eq $row->[2] && # and for i2 |
if ($debug) { |
186 |
( $last->[3] ne $row->[3] || # and subfield is different |
warn "## marc_repeatable_subfield ", dump( $marc_repeatable_subfield ), $/; |
187 |
$last->[3] eq $row->[3] && # or subfield is same, |
warn "## marc_record ", dump( $marc_record ), $/; |
188 |
$marc_repeatable_subfield->{ $row->[3] } # but is repeatable |
warn "## sorted_marc_record ", dump( \@sorted_marc_record ), $/; |
189 |
) |
warn "## subfield count ", dump( $u ), $/; |
190 |
) { |
} |
191 |
push @$last, ( $row->[3] , $row->[4] ); |
|
192 |
warn "## ++ added $row->[0] ^$row->[3] to $last->[0]\n"; |
my $len = $#sorted_marc_record; |
193 |
next; |
my $visited; |
194 |
} elsif ($last) { |
my $i = 0; |
195 |
push @m, $last; |
my $field; |
196 |
|
|
197 |
|
foreach ( 0 .. $len ) { |
198 |
|
|
199 |
|
# find next element which isn't visited |
200 |
|
while ($visited->{$i}) { |
201 |
|
$i = ($i + 1) % ($len + 1); |
202 |
|
} |
203 |
|
|
204 |
|
# mark it visited |
205 |
|
$visited->{$i}++; |
206 |
|
|
207 |
|
my $row = $sorted_marc_record[$i]; |
208 |
|
|
209 |
|
# field and subfield which is key for |
210 |
|
# marc_repeatable_subfield and u |
211 |
|
my $fsf = $row->[0] . $row->[3]; |
212 |
|
|
213 |
|
if ($debug > 1) { |
214 |
|
|
215 |
|
print "### field so far [", $#$field, "] : ", dump( $field ), " ", $field ? 'T' : 'F', $/; |
216 |
|
print "### this [$i]: ", dump( $row ),$/; |
217 |
|
print "### sf: ", $row->[3], " vs ", $field->[3], |
218 |
|
$marc_repeatable_subfield->{ $row->[0] . $row->[3] } ? ' (repeatable)' : '', $/, |
219 |
|
if ($#$field >= 0); |
220 |
|
|
221 |
|
} |
222 |
|
|
223 |
|
# if field exists |
224 |
|
if ( $#$field >= 0 ) { |
225 |
|
if ( |
226 |
|
$row->[0] ne $field->[0] || # field |
227 |
|
$row->[1] ne $field->[1] || # i1 |
228 |
|
$row->[2] ne $field->[2] # i2 |
229 |
|
) { |
230 |
|
push @m, $field; |
231 |
|
warn "## saved/1 ", dump( $field ),$/ if ($debug); |
232 |
|
$field = $row; |
233 |
|
|
234 |
|
} elsif ( |
235 |
|
( $row->[3] lt $field->[-2] ) # subfield which is not next (e.g. a after c) |
236 |
|
|| |
237 |
|
( $row->[3] eq $field->[-2] && # same subfield, but not repeatable |
238 |
|
! $marc_repeatable_subfield->{ $fsf } |
239 |
|
) |
240 |
|
) { |
241 |
|
push @m, $field; |
242 |
|
warn "## saved/2 ", dump( $field ),$/ if ($debug); |
243 |
|
$field = $row; |
244 |
|
|
245 |
|
} else { |
246 |
|
# append new subfields to existing field |
247 |
|
push @$field, ( $row->[3], $row->[4] ); |
248 |
|
} |
249 |
|
} else { |
250 |
|
# insert first field |
251 |
|
$field = $row; |
252 |
} |
} |
253 |
|
|
254 |
$last = $row; |
if (! $marc_repeatable_subfield->{ $fsf }) { |
255 |
|
# make step to next subfield |
256 |
|
$i = ($i + $u->{ $fsf } ) % ($len + 1); |
257 |
|
} |
258 |
} |
} |
259 |
|
|
260 |
push @m, $last if ($last); |
if ($#$field >= 0) { |
261 |
|
push @m, $field; |
262 |
|
warn "## saved/3 ", dump( $field ),$/ if ($debug); |
263 |
|
} |
264 |
|
|
265 |
return @m; |
return @m; |
266 |
} |
} |
337 |
my $v = $_; # make var read-write for Encode |
my $v = $_; # make var read-write for Encode |
338 |
next unless (defined($v) && $v !~ /^\s*$/); |
next unless (defined($v) && $v !~ /^\s*$/); |
339 |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
340 |
push @{ $marc_record }, [ |
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
341 |
$f, |
push @{ $marc_record }, [ $f, $i1, $i2, $sf => $v ]; |
|
$marc_indicators->{$f}->{i1} || ' ', |
|
|
$marc_indicators->{$f}->{i2} || ' ', |
|
|
$sf => $v |
|
|
]; |
|
342 |
} |
} |
343 |
} |
} |
344 |
|
|
351 |
=cut |
=cut |
352 |
|
|
353 |
sub marc_repeatable_subfield { |
sub marc_repeatable_subfield { |
354 |
die "marc_repeatable_subfield need subfield!\n" unless (defined($_[1])); |
my ($f,$sf) = @_; |
355 |
$marc_repeatable_subfield->{ $_[1] }++; |
die "marc_repeatable_subfield need field and subfield!\n" unless ($f && $sf); |
356 |
|
$marc_repeatable_subfield->{ $f . $sf }++; |
357 |
marc(@_); |
marc(@_); |
358 |
} |
} |
359 |
|
|
375 |
|
|
376 |
$i1 = ' ' if ($i1 !~ /^\d$/); |
$i1 = ' ' if ($i1 !~ /^\d$/); |
377 |
$i2 = ' ' if ($i2 !~ /^\d$/); |
$i2 = ' ' if ($i2 !~ /^\d$/); |
378 |
$marc_indicators->{$f}->{i1} = $i1; |
@{ $marc_indicators->{$f} } = ($i1,$i2); |
|
$marc_indicators->{$f}->{i2} = $i2; |
|
379 |
} |
} |
380 |
|
|
381 |
|
|
453 |
sub regex { |
sub regex { |
454 |
my $r = shift; |
my $r = shift; |
455 |
my @out; |
my @out; |
456 |
#warn "r: $r\n",Dumper(\@_); |
#warn "r: $r\n", dump(\@_); |
457 |
foreach my $t (@_) { |
foreach my $t (@_) { |
458 |
next unless ($t); |
next unless ($t); |
459 |
eval "\$t =~ $r"; |
eval "\$t =~ $r"; |