substitution command implemented
[migration-tools.git] / marc-cleanup
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Getopt::Long;
7 use Term::ReadLine;
8
9 $| = 1;
10
11 my $term = new Term::ReadLine 'yaz-cleanup';
12 my $OUT = $term->OUT || \*STDOUT;
13
14 my $count = 0;
15 my $reccount = 0;
16 my $line = '';
17
18 my @record = (); # current record storage
19 my %reccontext = ();
20 my @linecontext= (); # last 5 lines of file
21
22 my $input = shift || 'incoming.marc.xml';
23
24 open MARC, '<', $input;
25 open my $NUMARC, '>', 'incoming.clean.marc.xml';
26 print $NUMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
27 open my $EXMARC, '>', 'incoming.exceptions.marc.xml';
28 print $EXMARC '<?xml version="1.0" encoding="UTF-8"?>',"\n";
29 open MARC2, '<', $input;
30 <MARC2>;
31
32 # this is the dispatch table which drives command selection in
33 # edit(), below
34 my %commands = ( c => \&print_context,
35                  C => \&print_linecontext,
36                  k => \&kill_line,
37                  o => \&show_original,
38                  m => \&merge_lines,
39                  s => \&substitute,
40                  t => \&commit_edit,
41                  x => \&dump_record,
42                  q => \&quit,
43                  '?' => \&help,
44                  h   => \&help,
45                  help => \&help,
46                );
47
48 my @spinner = qw(- / | \\);
49 my $sidx = 0;
50
51 while (my $line = getline()) {
52     unless ($count % 2000) {
53         print "\rWorking... ", $spinner[$sidx];
54         $sidx = ($sidx == $#spinner) ? 0 : $sidx + 1;
55     }
56     update_linecontext();
57
58     # catch empty datafield elements
59     if ($line =~ m|</datafield>|) {
60         if ($record[-2] =~ m/<datafield tag="..." ind1="." ind2=".">/) {
61             pop @record; pop @record;
62             print $OUT "\rEmpty datafield scrubbed at line $count\n";
63             next;
64         }
65     }
66
67     # clean misplaced dollarsigns
68     if ($line =~ m|<subfield code="\$">c?\d+\.\d{2}|) {
69         $line =~ s|"\$">c?(\d+\.\d{2})|"c">\$$1|;
70         print $OUT "\rDollar sign in subfield code corrected at line $count\n";
71     }
72
73     # clean up tags with spaces in them
74     $line =~ s/tag="  /tag="00/g;
75     $line =~ s/tag=" /tag="0/g;
76     $line =~ s/tag="-/tag="0/g;
77     $line =~ s/tag="(\d\d) /tag="0$1/g;
78
79     # stow tag data if we're looking at it
80     if ($line =~ m/<datafield tag="(.{3})" ind1="(.)" ind2="(.)">/) {
81         $reccontext{tag}  = $1;
82         $reccontext{ind1} = $2;
83         $reccontext{ind2} = $3;
84     }
85
86     # naked ampersands
87     if ($line =~ /&/ && $line !~ /&\w+?;/)
88       { edit("Naked ampersand", $line); next }
89
90     # tags must be numeric
91     if ($line =~ /<datafield tag="(.+?)"/) {
92         my $match = $1;
93         if ($match =~ /\D/) {
94             edit("Non-numerics in tag", $line);
95             next;
96         }
97     }
98
99     # subfields can't be non-alphanumeric
100     if ($line =~ /<subfield code="(.+?)"/) {
101         my $match = $1;
102         if ($match =~ /\P{IsAlnum}/) {
103             edit("Junk in subfield code", $line);
104             next;
105         }
106     }
107
108 }
109 print $NUMARC "</xml>\n";
110 print $EXMARC "</xml>\n";
111
112 =head2 edit
113
114 Handles the Term::ReadLine loop
115
116 =cut
117
118 sub edit {
119     my ($msg, $line_in) = @_;
120     print $OUT "\r".$msg, " at line $count:\n";
121     print_context();
122
123     while (1) {
124         my $line = $term->readline('marc-cleanup>');
125         my @chunks = split /\s+/, $line;
126
127         if (length $chunks[0] == 1)
128           { next unless (defined $commands{$chunks[0]}) }
129
130         if (defined $commands{$chunks[0]}) {
131             my $term = $commands{$chunks[0]}->($line_in, @chunks[1..$#chunks]);
132             last if $term;
133         } else {
134             if ($linecontext[3] eq " [LINE KILLED]\n") {
135                 push @record, "$line\n"
136             } else {
137                 $record[-1] = "$line\n";
138             }
139             $linecontext[3] = "$line\n";
140             print_linecontext();
141         }
142     }
143 }
144
145 =head2 getline
146
147 Reads from the incoming MARC file; returns lines into the driver
148 loop. Batches records for output, and maintains the linecontext listing.
149
150 =cut
151
152 sub getline {
153     my $l = <MARC>;
154     $count++;
155     if (defined $l) {
156         if ($l =~ /<record>/) {
157             @record = ($l);
158             %reccontext = ();
159             $reccount++;
160         } elsif ($l =~ m|</record>|) {
161             push @record, $l;
162             write_record($NUMARC) if $reccount;
163         } else {
164             push @record, $l;
165         }
166     }
167     return $l;
168 }
169
170 sub write_record {
171     my ($FH) = @_;
172     print $FH '<collection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd" xmlns="http://www.loc.gov/MARC21/slim">',"\n";
173     print $FH @record;
174     print $FH "</collection>\n";
175 }
176
177 sub update_linecontext {
178     my $line2 = <MARC2>;
179     push @linecontext, $line2;
180     shift @linecontext if (@linecontext > 5);
181 }
182
183 #-----------------------------------------------------------------------------------
184 # command routines
185 #-----------------------------------------------------------------------------------
186
187 sub substitute {
188     my ($line_in, $from, $to) = @_;
189     $from = join("\\", '', split(//,$from));
190     $record[-1] =~ s/$from/$to/;
191     $linecontext[3] = $record[-1];
192     print_linecontext();
193     return 0;
194 }
195
196 sub merge_lines {
197     my $last = pop @record;
198     $last =~ s/^\s+//;
199     $record[-1] =~ s/\n//;
200     $record[-1] = join('', $record[-1], $last);
201     my @temp = ("\n");
202     push @temp, @linecontext[0..1];
203     $temp[3] = $record[-1];
204     $temp[4] = $linecontext[4];
205     @linecontext = @temp;
206     print_linecontext();
207     return 0;
208 }
209
210 sub kill_line {
211     pop @record;
212     $linecontext[3] = " [LINE KILLED]\n";
213     print_linecontext();
214     return 0;
215 }
216
217 sub dump_record {
218     my $line = <MARC>; $count++;
219     update_linecontext();
220     until ($line =~ m|</record>|) {
221         push @record, $line;
222         $line = <MARC>; $count++;
223         update_linecontext();
224     }
225     push @record, $line;
226     write_record($EXMARC);
227     return 1;
228 }
229
230 sub commit_edit { return 1 }
231
232 sub print_context {
233     print "\n Tag:",$reccontext{tag}, " Ind1:'",
234       $reccontext{ind1},"' Ind2:'", $reccontext{ind2}, "'";
235     print_linecontext();
236     return 0;
237 }
238
239 sub print_linecontext {
240     print $OUT "\n", join('    |','',@linecontext[0..2]);
241     print $OUT '==> |', $linecontext[3];
242     print $OUT '    |', $linecontext[4],"\n";
243     return 0;
244 }
245
246 sub show_original {
247     my ($line_in) = @_;
248     print $OUT "\n$line_in\n";
249     return 0;
250 }
251
252 sub help {
253 print $OUT <<HELP;
254
255 Type a replacement for the indicated line, or enter a command.
256
257 Commands: c  Show record context ('C' for brief context)
258           k  Kill indicated line (remove from record)
259           m  Merge indicated line with previous line
260           o  Show original line
261           s  Substitute ARG1 for ARG2 in indicated line
262           t  Commit changes and resume stream edit
263           x  Write this record to the exception file instead of output
264           q  Quit
265
266 HELP
267 return 0;
268 }
269
270 sub quit { exit }