ef4b056174fd2d2a070eab5219fa8c38d72153c0
[crolist2marc] / crolist2marc.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use autodie;
5
6 use MARC::Record;
7 use Data::Dump qw(dump);
8 use utf8;
9
10 my $data;
11
12 sub csv_file {
13         my ($file,$parse) = @_;
14
15         print STDERR "# reading $file ";
16         my $lines = 0;
17
18         open(my $fh, '<', $file);
19         my $h = <$fh>; # header
20         while(<$fh>) {
21                 chomp;
22                 my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text ) = $parse->($_);
23                 $lines++;
24
25                 if ( ! $text ) {
26                         print STDERR "\nSKIP $lines [$_] ";
27                         next;
28                 }
29
30                 $tagno ||= 0;
31                 $sfino ||= 0;
32
33                 $id .= " " if length $id < 2;
34                 $id .= " " if length $id < 2;
35                 my ($i1, $i2) = split(//, $id, 2);
36
37                 $sfi =~ s/^\$// || die "can't fix subfield [$sfi]";
38
39                 $text =~ tr/^~]}\|[{@`/ČčĆćĐ𩹮ž/; # CROASCII (YUS|HRN) B1.002:1982
40
41                 $data->{$idsl}->{$tag}->[ $tagno ]->[ 0 ] = $i1;
42                 $data->{$idsl}->{$tag}->[ $tagno ]->[ 1 ] = $i2;
43                 $data->{$idsl}->{$tag}->[ $tagno ]->[ ( $sfino * 2 ) + 2 ] = $sfi;
44                 $data->{$idsl}->{$tag}->[ $tagno ]->[ ( $sfino * 2 ) + 3 ] = $text;
45
46                 print STDERR "$lines " if $lines % 1000 == 0;
47         }
48         print STDERR "\n";
49 }
50
51 csv_file( 'TEKTAG.csv', sub {
52         my $line = shift;
53
54         my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $textkey, $textres ) = split(/,/,$_);
55
56         my $text = $textkey . $textres; # FIXME fix CAPITAL letters in $textkey
57
58         return ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text );
59 });
60
61 csv_file( 'LONTAG.csv', sub {
62         my $line = shift;
63
64         my ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text ) = split(/;/,$_, 7);
65
66         $text =~ s/;+$//;
67         $text =~ s/;/\n/g; # join OPIS[1-11]
68
69         return ( $idsl, $tag, $tagno, $id, $sfi, $sfino, $text );
70 });
71
72 print STDERR "\n# getting all ids ";
73 my @ids = keys %$data;
74 print STDERR scalar(@ids), " found\n";
75 foreach my $id ( @ids ) {
76         print "# $id ",dump($data->{$id});
77 }
78