| 1 | package MARC::Xform::AddRDA33x; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use MARC::Loop qw(marcparse marcfield marcbuild TAG VALREF SUBS SUB_ID SUB_VALREF); | ||
| 7 | |||
| 8 | my (%a2b, %b2a, %tag2src, %act2descrip); | ||
| 9 | my %len2tag = qw( | ||
| 10 | 3 336 | ||
| 11 | 1 337 | ||
| 12 | 2 338 | ||
| 13 | ); | ||
| 14 | |||
| 15 | # --- Create a transformation | ||
| 16 | |||
| 17 | sub new { | ||
| 18 | my $cls = shift; | ||
| 19 | my $self = bless { 'minsize' => 1, @_ }, $cls; | ||
| 20 | build_mappings() if !%a2b; | ||
| 21 | my $rules = $self->rules; | ||
| 22 | my $vocab = $self->vocabulary; | ||
| 23 | $vocab = $self->read_vocabulary($vocab) if !ref $vocab; | ||
| 24 | $rules = $self->read_rules($rules) if !ref $rules; | ||
| 25 | return sub { | ||
| 26 | my ($bib) = @_; | ||
| 27 | my ($leader, $fields, $marcref, $anno) = @$bib; | ||
| 28 | my (@sig, %result, @add, $fixed); | ||
| 29 | my @f33x; | ||
| 30 | @f33x = grep { $_->[TAG] =~ /^33[678]/ } @$fields | ||
| 31 | if !$self->{'replace'}; | ||
| 32 | my $force = $self->{'force'}; | ||
| 33 | foreach (@f33x) { | ||
| 34 | my $tag = $_->[TAG]; | ||
| 35 | my @subs = subfields($_); | ||
| 36 | my ($sb) = map { ${ $_->[SUB_VALREF] } } grep { $_->[SUB_ID] eq 'b' } @subs; | ||
| 37 | my ($s2) = map { ${ $_->[SUB_VALREF] } } grep { $_->[SUB_ID] eq '2' } @subs; | ||
| 38 | my ($sa) = map { ${ $_->[SUB_VALREF] } } grep { $_->[SUB_ID] eq 'a' } @subs; | ||
| 39 | next if defined $sb | ||
| 40 | || !defined $s2 | ||
| 41 | || $s2 !~ /^rda/ | ||
| 42 | || !defined $sa; | ||
| 43 | $sa =~ s/^\s+|\s+$//g; | ||
| 44 | $sa =~ tr/A-Z/a-z/; | ||
| 45 | $sb = $a2b{$tag}{$sa}; | ||
| 46 | next if !defined $sb; | ||
| 47 | splice @$_, SUBS; | ||
| 48 | push @$_, ( | ||
| 49 | ['a' => \$sa ], | ||
| 50 | ['b' => \$sb ], | ||
| 51 | ['2' => \$s2 ], | ||
| 52 | ); | ||
| 53 | $anno->{'result'}{'fixed'}{$tag} = $fixed = 1; | ||
| 54 | } | ||
| 55 | if ($fixed) { | ||
| 56 | # Nothing special to do | ||
| 57 | } | ||
| 58 | elsif (@f33x && !$force) { | ||
| 59 | $anno->{'result'}{'skip'} = 1; | ||
| 60 | } | ||
| 61 | elsif ($self->{'add'}) { | ||
| 62 | @add = @f33x if $force; | ||
| 63 | push @add, map { mk33x($len2tag{length $_} || die("I don't know how to add code $_"), $_) } @{ $self->{'add'} }; | ||
| 64 | } | ||
| 65 | else { | ||
| 66 | my $ldr06 = substr($leader, 6, 1); | ||
| 67 | my @f007val = map { substr(${$_->[VALREF]}, 0, 2) } grep { $_->[TAG] eq '007' } @$fields; | ||
| 68 | @f007val = qw(--) if !@f007val; | ||
| 69 | @sig = map { $ldr06 . $_ } @f007val; | ||
| 70 | my %added; | ||
| 71 | foreach my $s (@sig) { | ||
| 72 | my $rule = $rules->{$s} or next; | ||
| 73 | my ($act, @fields) = $rule->{'code'}->($self, $s, $leader, $fields); | ||
| 74 | $anno->{'result'}{'skip'} = 1, next if $act eq ''; | ||
| 75 | if (@fields) { | ||
| 76 | $anno->{'result'}{'add'} = \@add; | ||
| 77 | foreach (map { split /,/ } @fields) { | ||
| 78 | s/^\s+|\s+$//g; | ||
| 79 | next if $added{$_}++; | ||
| 80 | push @add, mk33x($len2tag{length $_} || die("I don't know how to add code $_"), $_); | ||
| 81 | } | ||
| 82 | $anno->{'result'}{'flagged'} = 1 if $act eq 'flag'; | ||
| 83 | push @{ $anno->{'result'}{'rule'} ||= [] }, $rule; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | @$fields = ( | ||
| 88 | ( grep { $_->[TAG] lt '336' } @$fields ), | ||
| 89 | ( sort { $a->[TAG] cmp $b->[TAG] } @add, ), | ||
| 90 | ( grep { $_->[TAG] gt '338' } @$fields ), | ||
| 91 | ) if @add; | ||
| 92 | return [$leader, $fields, undef, $anno]; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | # --- Supporting functions | ||
| 97 | |||
| 98 | sub rules { $_[0]->{'rules'} } | ||
| 99 | sub vocabulary { $_[0]->{'vocabulary'} } | ||
| 100 | |||
| 101 | sub read_vocabulary { | ||
| 102 | my ($self, $f) = @_; | ||
| 103 | open my $fh, '<', $f | ||
| 104 | or die "Can't open $f for reading: $!"; | ||
| 105 | my (%vocab, $sec); | ||
| 106 | my $n = 0; | ||
| 107 | while (<$fh>) { | ||
| 108 | chomp; | ||
| 109 | if (/^\[(.+?)\](?:\s+"(.+)")?(?:\s+<([^<>]+)>)?$/) { | ||
| 110 | my ($v, $d) = ($1, $2 || $1); | ||
| 111 | $sec = $vocab{$v} ||= {}; | ||
| 112 | $sec->{'order'} = ++$n; | ||
| 113 | $sec->{'code'} = $v; | ||
| 114 | $sec->{'descrip'} = $d; | ||
| 115 | $sec->{'link'} = $3 if defined $3; | ||
| 116 | $sec->{'terms'} = {}; | ||
| 117 | } | ||
| 118 | elsif (!$sec) { | ||
| 119 | die "Term outside of section: $_"; | ||
| 120 | } | ||
| 121 | elsif (/^(\S+) = (.+)$/) { | ||
| 122 | $sec->{'terms'}{$1} = { | ||
| 123 | 'code' => $1, | ||
| 124 | 'descrip' => $2, | ||
| 125 | 'count' => -1, | ||
| 126 | }; | ||
| 127 | } | ||
| 128 | elsif (/^\s*(?:#.*)?$/) { | ||
| 129 | next; | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | die "Bad line in terms: $_"; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | close $fh; | ||
| 136 | # Build record signature terms XXX I should really do this outside of this script | ||
| 137 | my $pfx = $vocab{'sig'}{'prefix'}; | ||
| 138 | if ($pfx) { | ||
| 139 | my $db; # XXX | ||
| 140 | my $terms = $vocab{'sig'}{'terms'} ||= {}; | ||
| 141 | my $iter = $db->allterms_begin($pfx); | ||
| 142 | if ($iter) { | ||
| 143 | do { | ||
| 144 | my $code = substr($iter->get_termname, length($pfx)); | ||
| 145 | my $rtyp = substr($code, 0, 1); | ||
| 146 | my $mtyp = substr($code, 1); | ||
| 147 | my $descrip = join(' - ', | ||
| 148 | $vocab{'rtyp'}{'terms'}{$rtyp}{'descrip'} // '(unrecognized record type)', | ||
| 149 | $vocab{'mtyp'}{'terms'}{$mtyp}{'descrip'} // '(unrecognized material type)', | ||
| 150 | ); | ||
| 151 | $terms->{$code} = { | ||
| 152 | 'code' => $code, | ||
| 153 | 'descrip' => $descrip, | ||
| 154 | 'count' => $iter->get_termfreq, | ||
| 155 | }; | ||
| 156 | } while $iter++ | ||
| 157 | } | ||
| 158 | } | ||
| 159 | return $self->{'vocabulary'} = \%vocab; | ||
| 160 | } | ||
| 161 | |||
| 162 | sub read_rules { | ||
| 163 | my ($self, $f) = @_; | ||
| 164 | open my $fh, '<', $f | ||
| 165 | or die "Can't open $f for reading: $!"; | ||
| 166 | my %rule; | ||
| 167 | my $vocab = $self->vocabulary; | ||
| 168 | while (<$fh>) { | ||
| 169 | next if !s/^>\s+//; | ||
| 170 | chomp; | ||
| 171 | my ($sig, $act, $f336b, $f337b, $f338b) = split /\s+/, $_; | ||
| 172 | my %r = ( | ||
| 173 | 'sig' => $sig, | ||
| 174 | 'code336' => $f336b, | ||
| 175 | 'code337' => $f337b, | ||
| 176 | 'code338' => $f338b, | ||
| 177 | ); | ||
| 178 | if ($act eq '-') { | ||
| 179 | $r{'action'} = $act = 'skip'; | ||
| 180 | $r{'code'} = sub { $act }; | ||
| 181 | } | ||
| 182 | elsif ($act eq '?') { | ||
| 183 | $r{'action'} = $act = 'query'; | ||
| 184 | $r{'code'} = sub { 'query' }; | ||
| 185 | } | ||
| 186 | elsif ($act =~ /^\(([a-z]+)\)$/) { | ||
| 187 | $r{'action'} = $act = 'special'; | ||
| 188 | $r{'code'} = $self->can("rule_$1") or die "No rule handler: $1"; | ||
| 189 | } | ||
| 190 | else { | ||
| 191 | $r{'action'} = $act = $act eq '*' ? 'flag' : 'add'; | ||
| 192 | $r{'code'} = sub { $act, $f336b, $f337b, $f338b }; | ||
| 193 | } | ||
| 194 | my $pfx = $vocab->{'sig'}{'prefix'}; | ||
| 195 | $r{'actiondescrip'} = $act2descrip{$r{'action'}}; | ||
| 196 | $rule{$sig} = \%r; | ||
| 197 | } | ||
| 198 | return $self->{'rules'} = \%rule; | ||
| 199 | } | ||
| 200 | |||
| 201 | sub build_mappings { | ||
| 202 | my %f336a2b = ( | ||
| 203 | q{cartographic dataset} => q{crd}, | ||
| 204 | q{cartographic image} => q{cri}, | ||
| 205 | q{cartographic moving image} => q{crm}, | ||
| 206 | q{cartographic tactile image} => q{crt}, | ||
| 207 | q{cartographic tactile three-dimensional form} => q{crn}, | ||
| 208 | q{cartographic three-dimensional form} => q{crf}, | ||
| 209 | q{computer dataset} => q{cod}, | ||
| 210 | q{computer program} => q{cop}, | ||
| 211 | q{notated movement} => q{ntv}, | ||
| 212 | q{notated music} => q{ntm}, | ||
| 213 | q{performed music} => q{prm}, | ||
| 214 | q{sounds} => q{snd}, | ||
| 215 | q{spoken word} => q{spw}, | ||
| 216 | q{still image} => q{sti}, | ||
| 217 | q{tactile image} => q{tci}, | ||
| 218 | q{tactile notated music} => q{tcm}, | ||
| 219 | q{tactile notated movement} => q{tcn}, | ||
| 220 | q{tactile text} => q{tct}, | ||
| 221 | q{tactile three-dimensional form} => q{tcf}, | ||
| 222 | q{text} => q{txt}, | ||
| 223 | q{three-dimensional form} => q{tdf}, | ||
| 224 | q{three-dimensional moving image} => q{tdm}, | ||
| 225 | q{two-dimensional moving image} => q{tdi}, | ||
| 226 | q{other} => q{xxx}, | ||
| 227 | q{unspecified} => q{zzz}, | ||
| 228 | ); | ||
| 229 | my %f337a2b = ( | ||
| 230 | q{audio} => q{s}, | ||
| 231 | q{computer} => q{c}, | ||
| 232 | q{microform} => q{h}, | ||
| 233 | q{microscopic} => q{p}, | ||
| 234 | q{projected} => q{g}, | ||
| 235 | q{stereographic} => q{e}, | ||
| 236 | q{unmediated} => q{n}, | ||
| 237 | q{video} => q{v}, | ||
| 238 | q{other} => q{x}, | ||
| 239 | q{unspecified} => q{z}, | ||
| 240 | ); | ||
| 241 | my %f338a2b = ( | ||
| 242 | q{audio cartridge} => q{sg}, | ||
| 243 | q{audio cylinder} => q{se}, | ||
| 244 | q{audio disc} => q{sd}, | ||
| 245 | q{sound track reel} => q{si}, | ||
| 246 | q{audio roll} => q{sq}, | ||
| 247 | q{audiocassette} => q{ss}, | ||
| 248 | q{audiotape reel} => q{st}, | ||
| 249 | q{other} => q{sz}, | ||
| 250 | q{computer card} => q{ck}, | ||
| 251 | q{computer chip cartridge} => q{cb}, | ||
| 252 | q{computer disc} => q{cd}, | ||
| 253 | q{computer disc cartridge} => q{ce}, | ||
| 254 | q{computer tape cartridge} => q{ca}, | ||
| 255 | q{computer tape cassette} => q{cf}, | ||
| 256 | q{computer tape reel} => q{ch}, | ||
| 257 | q{online resource} => q{cr}, | ||
| 258 | q{other} => q{cz}, | ||
| 259 | q{aperture card} => q{ha}, | ||
| 260 | q{microfiche} => q{he}, | ||
| 261 | q{microfiche cassette} => q{hf}, | ||
| 262 | q{microfilm cartridge} => q{hb}, | ||
| 263 | q{microfilm cassette} => q{hc}, | ||
| 264 | q{microfilm reel} => q{hd}, | ||
| 265 | q{microfilm roll} => q{hj}, | ||
| 266 | q{microfilm slip} => q{hh}, | ||
| 267 | q{microopaque} => q{hg}, | ||
| 268 | q{other} => q{hz}, | ||
| 269 | q{microscope slide} => q{pp}, | ||
| 270 | q{other} => q{pz}, | ||
| 271 | q{film cartridge} => q{mc}, | ||
| 272 | q{film cassette} => q{mf}, | ||
| 273 | q{film reel} => q{mr}, | ||
| 274 | q{film roll} => q{mo}, | ||
| 275 | q{filmslip} => q{gd}, | ||
| 276 | q{filmstrip} => q{gf}, | ||
| 277 | q{filmstrip cartridge} => q{gc}, | ||
| 278 | q{overhead transparency} => q{gt}, | ||
| 279 | q{slide} => q{gs}, | ||
| 280 | q{other} => q{mz}, | ||
| 281 | q{stereograph card} => q{eh}, | ||
| 282 | q{stereograph disc} => q{es}, | ||
| 283 | q{other} => q{ez}, | ||
| 284 | q{card} => q{no}, | ||
| 285 | q{flipchart} => q{nn}, | ||
| 286 | q{roll} => q{na}, | ||
| 287 | q{sheet} => q{nb}, | ||
| 288 | q{volume} => q{nc}, | ||
| 289 | q{object} => q{nr}, | ||
| 290 | q{other} => q{nz}, | ||
| 291 | q{video cartridge} => q{vc}, | ||
| 292 | q{videocassette} => q{vf}, | ||
| 293 | q{videodisc} => q{vd}, | ||
| 294 | q{videotape reel} => q{vr}, | ||
| 295 | q{other} => q{vz}, | ||
| 296 | q{unspecified} => q{zu}, | ||
| 297 | ); | ||
| 298 | my %f336b2a = reverse %f336a2b; | ||
| 299 | my %f337b2a = reverse %f337a2b; | ||
| 300 | my %f338b2a = reverse %f338a2b; | ||
| 301 | %a2b = ( | ||
| 302 | '336' => \%f336a2b, | ||
| 303 | '337' => \%f337a2b, | ||
| 304 | '338' => \%f338a2b, | ||
| 305 | ); | ||
| 306 | %b2a = ( | ||
| 307 | '336' => \%f336b2a, | ||
| 308 | '337' => \%f337b2a, | ||
| 309 | '338' => \%f338b2a, | ||
| 310 | ); | ||
| 311 | %tag2src = qw( | ||
| 312 | 336 rdacontent | ||
| 313 | 337 rdamedia | ||
| 314 | 338 rdacarrier | ||
| 315 | ); | ||
| 316 | %act2descrip = ( | ||
| 317 | 'add' => 'add fields', | ||
| 318 | 'flag' => 'add fields but flag for review', | ||
| 319 | 'skip' => 'do not attempt to add 33x fields', | ||
| 320 | 'fix' => 'fix existing 33x fields', | ||
| 321 | 'ignore' => 'leave existing 33x fields unmodified', | ||
| 322 | 'query' => 'appropriate action is under review', | ||
| 323 | '???' => 'no criteria apply', | ||
| 324 | ); | ||
| 325 | } | ||
| 326 | |||
| 327 | sub mk33x { | ||
| 328 | my ($tag, $bval) = @_; | ||
| 329 | marcfield($tag, ' ', ' ', 'a' => $b2a{$tag}{$bval}, 'b' => $bval, '2' => $tag2src{$tag}); | ||
| 330 | } | ||
| 331 | |||
| 332 | sub subfields { | ||
| 333 | my ($field) = @_; | ||
| 334 | return @$field[SUBS..$#$field]; | ||
| 335 | } | ||
| 336 | |||
| 337 | sub subval1 { | ||
| 338 | my ($fields, $tagsub) = @_; | ||
| 339 | my ($tag, $sub) = ( $tagsub =~ /^(...)(.)$/ ); | ||
| 340 | foreach (grep { $_->[TAG] eq $tag } @$fields) { | ||
| 341 | my ($sub) = grep { $_->[SUB_ID] eq $sub } subfields($_); | ||
| 342 | return ${ $sub->[SUB_VALREF] } if $sub; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | sub rule_a { | ||
| 347 | my ($self, $sig, $leader, $fields) = @_; | ||
| 348 | return 'skip' if @$fields < $self->{'minsize'}; | ||
| 349 | my $f245h = subval1($fields, '245h'); | ||
| 350 | if ($f245h) { | ||
| 351 | return qw(add txt c cr) | ||
| 352 | if $f245h =~ /electronic resource/i; | ||
| 353 | } | ||
| 354 | return qw(add txt n nc); | ||
| 355 | } | ||
| 356 | |||
| 357 | sub rule_acr { | ||
| 358 | my ($self, $sig, $leader, $fields) = @_; | ||
| 359 | return 'skip' if @$fields < $self->{'minsize'}; | ||
| 360 | my $f245h = subval1($fields, '245h'); | ||
| 361 | if ($f245h) { | ||
| 362 | return qw(add txt c cr) | ||
| 363 | if $f245h =~ /electronic resource/i; | ||
| 364 | } | ||
| 365 | return 'skip'; # XXX Remove this restriction later? | ||
| 366 | return qw(add txt c cr); | ||
| 367 | } | ||
| 368 | |||
| 369 | sub rule_c { | ||
| 370 | my ($self, $sig, $leader, $fields) = @_; | ||
| 371 | my $f300a = subval1($fields, '300a') or return 'skip'; | ||
| 372 | return qw(flag ntm n nc) | ||
| 373 | if $f300a =~ /^\bscores?\b/; | ||
| 374 | my $f245h = subval1($fields, '245h') or return 'skip'; | ||
| 375 | return qw(flag ntm c cd) | ||
| 376 | if $f300a =~ /\d CD-ROM/ | ||
| 377 | && $f245h =~ /electronic resource/; | ||
| 378 | } | ||
| 379 | |||
| 380 | sub rule_m { | ||
| 381 | my ($self, $sig, $leader, $fields) = @_; | ||
| 382 | my $f245h = subval1($fields, '245h') or return 'skip'; | ||
| 383 | my $f516a = subval1($fields, '516a') or return 'skip'; | ||
| 384 | return qw(flag tdi c cr) | ||
| 385 | if $f516a =~ /Streaming video/ | ||
| 386 | && $f245h =~ /electronic resource/; | ||
| 387 | } | ||
| 388 | |||
| 389 | sub rule_j { | ||
| 390 | my ($self, $sig, $leader, $fields) = @_; | ||
| 391 | my $f245h = subval1($fields, '245h'); | ||
| 392 | if ($f245h) { | ||
| 393 | return qw(flag prm c cr) if $f245h =~ /electronic resource/; | ||
| 394 | } | ||
| 395 | return 'skip'; | ||
| 396 | } | ||
| 397 | |||
| 398 | sub rule_k { | ||
| 399 | my ($self, $sig, $leader, $fields) = @_; | ||
| 400 | my $f245h = subval1($fields, '245h'); | ||
| 401 | if ($f245h) { | ||
| 402 | return qw(flag sti n no) if $f245h =~ /(flash|activity) card/; | ||
| 403 | return qw(flag sti n nb) if $f245h =~ /study print|picture|poster/; | ||
| 404 | } | ||
| 405 | my $f300a = subval1($fields, '300a'); | ||
| 406 | if ($f300a) { | ||
| 407 | return qw(flag sti n no) if $f300a =~ /\d (post|activity )cards?\b/; | ||
| 408 | return qw(flag sti n nb) if $f300a =~ /\d posters?\b/; | ||
| 409 | } | ||
| 410 | } | ||
| 411 | |||
| 412 | sub rule_g { | ||
| 413 | my ($self, $sig, $leader, $fields) = @_; | ||
| 414 | my $f245h = subval1($fields, '245h') or return 'skip'; | ||
| 415 | if ($f245h =~ /\bDVD\b/) { | ||
| 416 | return qw(flag tdi v vd); | ||
| 417 | } | ||
| 418 | elsif ($f245h =~ /\b(videorecording|motion picture)\b/) { | ||
| 419 | my $f300a = subval1($fields, '300a') or return 'skip'; | ||
| 420 | return qw(flag tdi g mr) if $f300a =~ /\d (film )?reel/; | ||
| 421 | return qw(flag tdi v vd) if $f300a =~ /videodisc|dvd/i; | ||
| 422 | return qw(flag tdi v vf) if $f300a =~ /videocas+et+e/; | ||
| 423 | my $f300c = subval1($fields, '300c') or return 'skip'; | ||
| 424 | return qw(flag tdi g mr) if $f300c =~ /^\d+ *mm/; | ||
| 425 | } | ||
| 426 | elsif ($f245h =~ /\b(slide)\b/) { | ||
| 427 | return qw(flag sti g gs); | ||
| 428 | } | ||
| 429 | } | ||
| 430 | |||
| 431 | 1; |