X-Git-Url: http://git.rot13.org/?p=crolist2marc;a=blobdiff_plain;f=crolist2marc.pl;h=49aa86a5d33c4c144b54743e8c0bf13fe54706ca;hp=56a54f24bcb49b0992c71c64da5aa157d1680e96;hb=11bf190ad5ab79edce6ab03045b87c8e72c8c64d;hpb=d919adf8f94e3ec21ff20ff2d26d48dc4476531a diff --git a/crolist2marc.pl b/crolist2marc.pl index 56a54f2..49aa86a 100755 --- a/crolist2marc.pl +++ b/crolist2marc.pl @@ -9,33 +9,127 @@ use utf8; my $data; -my $lines = 0; +sub csv_file { + my ($file,$parse) = @_; -open(my $fh, '<', 'TEKTAG.csv'); -my $h = <$fh>; # header -while(<$fh>) { - chomp; - my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $textkey, $textres ) = split(/,/,$_); + print STDERR "# reading $file "; + my $lines = 1; - $tagno ||= 0; - $sfino ||= 0; + open(my $fh, '<', $file); + my $h = <$fh>; # header + while(<$fh>) { + chomp; + my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text ) = $parse->($_); + $lines++; - $id .= " " if length $id < 2; - $id .= " " if length $id < 2; - my ($i1, $i2) = split(//, $id, 2); + if ( ! $text ) { + print STDERR "\nSKIP $file +$lines [$_] " if $idsl; + next; + } - $sfi =~ s/^\$// || die "can't fix subfield [$sfi]"; + $tagno ||= 0; + $sfino ||= 0; - my $text = $textkey . $textres; # FIXME fix CAPITAL letters in $textkey - $text =~ tr/^~]}\|[{@`/ČčĆćĐ𩹮ž/; # CROASCII (YUS|HRN) B1.002:1982 + $id .= " " if length $id < 2; + $id .= " " if length $id < 2; + my ($i1, $i2) = split(//, $id, 2); + + $sfi =~ s/^\$// || die "can't fix subfield [$sfi]"; + + $text =~ tr/^~]}\\|[{@`/ČčĆćĐ𩹮ž/; # CROASCII (YUS|HRN) B1.002:1982 - $data->{$idsl}->{$tag}->[ $tagno ]->[ 0 ] = $i1; - $data->{$idsl}->{$tag}->[ $tagno ]->[ 1 ] = $i2; - $data->{$idsl}->{$tag}->[ $tagno ]->[ ( $sfino * 2 ) + 2 ] = $sfi; - $data->{$idsl}->{$tag}->[ $tagno ]->[ ( $sfino * 2 ) + 3 ] = $text; + $data->{$idsl}->{$tag}->[ $tagno ]->[ 0 ] = $i1; + $data->{$idsl}->{$tag}->[ $tagno ]->[ 1 ] = $i2; + $data->{$idsl}->{$tag}->[ $tagno ]->[ ( $sfino * 2 ) + 2 ] = $sfi; + $data->{$idsl}->{$tag}->[ $tagno ]->[ ( $sfino * 2 ) + 3 ] = $text; -# last if $lines++ > 5000; + print STDERR "$lines " if $lines % 1000 == 0; + } + print STDERR "\n"; } -warn dump($data); +csv_file( 'TEKTAG.csv', sub { + my $line = shift; + + my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $textkey, $textres ) = split(/,/,$_); + + my $text = $textkey . $textres; # FIXME fix CAPITAL letters in $textkey + + return ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text ); +}); + +csv_file( 'LONTAG.csv', sub { + my $line = shift; + + my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text ) = split(/;/,$_, 7); + + $text =~ s/;+$//; + $text =~ s/;/\n/g; # join OPIS[1-11] + + return ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text ); +}); + +csv_file( 'IDNTAG.csv', sub { + my $line = shift; + + my ( $idsl, $tag, $STSL, $KZVS, $BIBRAZ, $HIRAZ, $KPS, $OKO ) = split(/,/,$_); + + my @leader; + $leader[5] = $STSL; + $leader[6] = $KZVS; + $leader[7] = $BIBRAZ; + $leader[8] = $HIRAZ; + $leader[17] = $KPS; + $leader[18] = $OKO; + + $leader[23] = ' '; # last char; + + my $full = join('', map { defined $_ ? $_ : ' ' } @leader); + $data->{$idsl}->{'leader'} = $full; + return; +}); + +csv_file( 'OBRTAG.csv', sub { + my $line = shift; + + my ( $IDSL, $TAG, $TAGNO, $ID, $SFI, $SFINO, $CODINF ) = split(/,/,$_); + return ( $IDSL, $TAG, $TAGNO, $ID, $SFI, $SFINO, $CODINF ); +}); + +print STDERR "\n# getting all ids "; +my @ids = keys %$data; +print STDERR scalar(@ids), " found\n"; + +my $marc_file = 'liberated.marc'; +open(my $marc_fh, '>:encoding(UTF-8)', $marc_file); +my $number = 0; + +foreach my $id ( @ids ) { + my $rec = MARC::Record->new; + $rec->encoding( 'UTF-8' ); + $rec->add_fields( [ '001', $id ] ); + + foreach my $field ( sort keys %{ $data->{$id} } ) { + if ( $field eq 'leader' ) { + $rec->leader( $data->{$id}->{$field} ); + next; + } + foreach my $arr ( @{ $data->{$id}->{$field} } ) { + if ( ! $arr ) { + print STDERR "SKIPPED $id $field ",dump( $data->{$id}->{$field} ), "\n"; + next; + } + $rec->add_fields( $field, @$arr ); + } + } + + #print $rec->as_formatted; + #print "# $id ",dump($data->{$id}); + + print $marc_fh $rec->as_usmarc; + $number++; + print "$number " if $number % 1000 == 0; +} +close($marc_fh); +print "$marc_file ",-s $marc_file, " bytes\n";