use tab delimited files
[crolist2marc] / crolist2marc.pl
index ef4b056..39eb028 100755 (executable)
@@ -13,7 +13,7 @@ sub csv_file {
        my ($file,$parse) = @_;
 
        print STDERR "# reading $file ";
        my ($file,$parse) = @_;
 
        print STDERR "# reading $file ";
-       my $lines = 0;
+       my $lines = 1;
 
        open(my $fh, '<', $file);
        my $h = <$fh>; # header
 
        open(my $fh, '<', $file);
        my $h = <$fh>; # header
@@ -23,7 +23,7 @@ sub csv_file {
                $lines++;
 
                if ( ! $text ) {
                $lines++;
 
                if ( ! $text ) {
-                       print STDERR "\nSKIP $lines [$_] ";
+                       print STDERR "\nSKIP $file +$lines [$_] " if $idsl;
                        next;
                }
 
                        next;
                }
 
@@ -36,7 +36,7 @@ sub csv_file {
 
                $sfi =~ s/^\$// || die "can't fix subfield [$sfi]";
 
 
                $sfi =~ s/^\$// || die "can't fix subfield [$sfi]";
 
-               $text =~ tr/^~]}\|[{@`/ČčĆćĐ𩹮ž/; # CROASCII (YUS|HRN) B1.002:1982
+               $text =~ tr/^~]}\\|[{@`/ČčĆćĐ𩹮ž/; # CROASCII (YUS|HRN) B1.002:1982
 
                $data->{$idsl}->{$tag}->[ $tagno ]->[ 0 ] = $i1;
                $data->{$idsl}->{$tag}->[ $tagno ]->[ 1 ] = $i2;
 
                $data->{$idsl}->{$tag}->[ $tagno ]->[ 0 ] = $i1;
                $data->{$idsl}->{$tag}->[ $tagno ]->[ 1 ] = $i2;
@@ -48,31 +48,89 @@ sub csv_file {
        print STDERR "\n";
 }
 
        print STDERR "\n";
 }
 
-csv_file( 'TEKTAG.csv', sub {
+csv_file( 'tsv/TEKTAG.csv', sub {
        my $line = shift;
 
        my $line = shift;
 
-       my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $textkey, $textres ) = split(/,/,$_);
+       my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $textkey, $textres ) = split(/\t/,$_);
 
        my $text = $textkey . $textres; # FIXME fix CAPITAL letters in $textkey
 
        return ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text );
 });
 
 
        my $text = $textkey . $textres; # FIXME fix CAPITAL letters in $textkey
 
        return ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text );
 });
 
-csv_file( 'LONTAG.csv', sub {
+csv_file( 'tsv/LONTAG.csv', sub {
        my $line = shift;
 
        my $line = shift;
 
-       my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text ) = split(/;/,$_, 7);
+       my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text ) = split(/\t/,$_, 7);
 
 
-       $text =~ s/;+$//;
-       $text =~ s/;/\n/g; # join OPIS[1-11]
+       $text =~ s/\t+$//;
+       $text =~ s/\t/\n/g; # join OPIS[1-11]
 
        return ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text );
 });
 
 
        return ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text );
 });
 
+csv_file( 'tsv/IDNTAG.csv', sub {
+       my $line = shift;
+
+       my ( $idsl, $tag, $STSL, $KZVS, $BIBRAZ, $HIRAZ, $KPS, $OKO ) = split(/\t/,$_);
+
+       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( 'tsv/OBRTAG.csv', sub {
+       my $line = shift;
+
+       my ( $IDSL, $TAG, $TAGNO, $ID, $SFI, $SFINO, $CODINF ) = split(/\t/,$_);
+       return ( $IDSL, $TAG, $TAGNO, $ID, $SFI, $SFINO, $CODINF );
+});
+
 print STDERR "\n# getting all ids ";
 print STDERR "\n# getting all ids ";
-my @ids = keys %$data;
+my @ids = sort keys %$data;
 print STDERR scalar(@ids), " found\n";
 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 ) {
 foreach my $id ( @ids ) {
-       print "# $id ",dump($data->{$id});
+       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;
+                       }
+                       $arr = [ $arr->[3] ] if ( $field < 010 ); # control fields don't have idicators or subfields
+                       $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";