| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | =head2 NAME | ||
| 4 | |||
| 5 | 010 cleanup -- Fixes spacing problems in 010 subfield a. | ||
| 6 | |||
| 7 | =head2 DESCRIPTION | ||
| 8 | |||
| 9 | This version will clean only subfield 'a' of 010 and skips records which have no 010 and those that have no 010 subfield 'a' | ||
| 10 | |||
| 11 | =head2 OUTLINE OF PROCESS | ||
| 12 | |||
| 13 | Get subfield 'a', remove non-digits. | ||
| 14 | If result is exactly 8 digits, substr(result, 0, 2) >=70 or =00 or error. | ||
| 15 | New field a = ' result ' | ||
| 16 | If result is exactly 10 digits, substr(result, 0, 4) >=2001 and <= 2010 (will change as year changes) or error. | ||
| 17 | New field a = ' result' | ||
| 18 | Compare new field a with existing field a and report only those that differ. | ||
| 19 | |||
| 20 | |||
| 21 | =head2 TO DO | ||
| 22 | |||
| 23 | Think about whether subfield 'z' needs extra spaces removed. | ||
| 24 | |||
| 25 | Deal with non-digit characters in original 010a field. | ||
| 26 | Currently these are simply reported and the record is skipped. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | ########################### | ||
| 31 | ### Initialize includes ### | ||
| 32 | ### and basic needs ### | ||
| 33 | ########################### | ||
| 34 | use strict; | ||
| 35 | use MARC::Batch; | ||
| 36 | #use MARC::File::USMARC; | ||
| 37 | use MARC::BBMARC; | ||
| 38 | ########################## | ||
| 39 | ## Time coding routines ## | ||
| 40 | ## Print start time and ## | ||
| 41 | ## set start variable ## | ||
| 42 | ########################## | ||
| 43 | |||
| 44 | use Time::HiRes qw( tv_interval ); | ||
| 45 | # measure elapsed time | ||
| 46 | my $t0 = [Time::HiRes::time()]; | ||
| 47 | my $startingtime = MARC::BBMARC::startstop_time(); | ||
| 48 | ######################### | ||
| 49 | ### Start main program ## | ||
| 50 | ######################### | ||
| 51 | |||
| 52 | print ("Welcome to 010 cleanup\n"); | ||
| 53 | |||
| 54 | ##### File handling initialization ###### | ||
| 55 | #prompt for input file | ||
| 56 | print ("What is the input file? "); | ||
| 57 | my $inputfile=<>; | ||
| 58 | chomp $inputfile; | ||
| 59 | $inputfile =~ s/^\"(.*)\"$/$1/; | ||
| 60 | print ("What is the export file? "); | ||
| 61 | my $exportfile = <>; | ||
| 62 | chomp $exportfile; | ||
| 63 | $exportfile =~ s/^\"(.*)\"$/$1/; | ||
| 64 | open(OUT, ">$exportfile"); | ||
| 65 | print ("What is the error file? "); | ||
| 66 | my $errorfile = <>; | ||
| 67 | chomp $errorfile; | ||
| 68 | $errorfile =~ s/^\"(.*)\"$/$1/; | ||
| 69 | open(ERRORS, ">$errorfile"); | ||
| 70 | |||
| 71 | #if using MacPerl, set creator and type to BBEdit and Text | ||
| 72 | if ($^O eq 'MacOS') { | ||
| 73 | MacPerl::SetFileInfo('R*ch', 'TEXT', $exportfile); | ||
| 74 | MacPerl::SetFileInfo('R*ch', 'TEXT', $errorfile); | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | #initialize $batch as new MARC::Batch object | ||
| 79 | my $batch = MARC::Batch->new('USMARC', "$inputfile"); | ||
| 80 | |||
| 81 | ########## Start extraction ######### | ||
| 82 | |||
| 83 | ############################################ | ||
| 84 | # Set start time for main calculation loop # | ||
| 85 | ############################################ | ||
| 86 | my $t1 = [Time::HiRes::time()]; | ||
| 87 | my $runningrecordcount=0; | ||
| 88 | ################################################### | ||
| 89 | my $cleanedreccount=0; | ||
| 90 | #### Start while loop through records in file ##### | ||
| 91 | while (my $record = $batch->next()) { | ||
| 92 | |||
| 93 | ################################################### | ||
| 94 | ### add to count for user notification ### | ||
| 95 | $runningrecordcount++; | ||
| 96 | MARC::BBMARC::counting_print ($runningrecordcount); | ||
| 97 | ################################################### | ||
| 98 | ############################################## | ||
| 99 | ## Declare variables needed for each record ## | ||
| 100 | ############################################## | ||
| 101 | my $recordchanged = 0; | ||
| 102 | # predeclare $controlno for reporting errors | ||
| 103 | my $controlno; | ||
| 104 | # $field_010 will have MARC::Field version of the 010 field of the record | ||
| 105 | my $field_010; | ||
| 106 | #$cleaned010a will have the finished cleaned 010a data | ||
| 107 | my $cleaned010a; | ||
| 108 | # $haserrors will have tab-separated string of errors | ||
| 109 | my $haserrors = ""; | ||
| 110 | |||
| 111 | #skip records with no 010 and no 010$a | ||
| 112 | unless (($record->field('010')) && ($record->field('010')->subfield('a'))) {next;} | ||
| 113 | |||
| 114 | # record has an 010 with subfield a, so check for errors and then do cleanup | ||
| 115 | else { | ||
| 116 | #get record control number for reporting errors | ||
| 117 | $controlno = $record->field('001')->as_string(); | ||
| 118 | |||
| 119 | $field_010 = $record->field('010'); | ||
| 120 | my $orig010a = $field_010->subfield('a'); | ||
| 121 | my $subfielda = $field_010->subfield('a'); | ||
| 122 | |||
| 123 | #Get number portion of subfield | ||
| 124 | $subfielda =~ s/\D*(\d{8,10})\D*/$1/; | ||
| 125 | #report error if 8-10 digit number was not found | ||
| 126 | unless ($1) {$haserrors .= "$controlno\tCould not find an 8-10 digit number in $subfielda\t"} | ||
| 127 | ####################################################### | ||
| 128 | # LCCN validity checks and setting of cleaned version # | ||
| 129 | ####################################################### | ||
| 130 | #check validity of resulting digits | ||
| 131 | if ($subfielda =~ /^\d{8}$/) { | ||
| 132 | my $year = substr($subfielda, 0, 2); | ||
| 133 | #should be old lccn, so first 2 digits are 00 or > 80 (for QBI records) | ||
| 134 | if (($year >= 1) && ($year < 80)) {$haserrors .= "$controlno\tFirst digits of lccn are $year\t"} | ||
| 135 | #otherwise, 8 digit lccn needs 3 spaces before, 1 after, so put that in $cleaned010a | ||
| 136 | else { | ||
| 137 | $cleaned010a = " $subfielda "; | ||
| 138 | } #else $subfielda has valid lccn | ||
| 139 | } #if lccn is 8 digits | ||
| 140 | |||
| 141 | #otherwise if $subfielda is 10 digits | ||
| 142 | elsif ($subfielda =~ /^\d{10}$/) { | ||
| 143 | my $year = substr($subfielda, 0, 4); | ||
| 144 | # no valid 10 digit will be less than 2001 | ||
| 145 | # change upper limit as years progress | ||
| 146 | if (($year < 2001) || ($year > 2006)) {$haserrors .= "$controlno\tFirst digits of lccn are $year\t";} | ||
| 147 | #otherwise, 10 digit lccn needs 2 spaces before, 0 after, so put that in $cleaned010a | ||
| 148 | else { | ||
| 149 | $cleaned010a = " $subfielda"; | ||
| 150 | } #else $subfielda has valid lccn | ||
| 151 | } #if lccn is 10 digits | ||
| 152 | |||
| 153 | # lccn is not 8 or 10 digits so report error | ||
| 154 | else {$haserrors .= "$controlno\tLCCN $subfielda is not 8 or 10 digits\t";} | ||
| 155 | |||
| 156 | #report errors in validity of LCCNs | ||
| 157 | if ($haserrors) {print ERRORS "Original 010\t$orig010a\tvalidity errors:\t$haserrors\n"; next;} | ||
| 158 | |||
| 159 | ########################################### | ||
| 160 | ### Compare cleaned field with original ### | ||
| 161 | ########################################### | ||
| 162 | |||
| 163 | #if original and cleaned match, go to next record | ||
| 164 | if ($orig010a eq $cleaned010a) {next;} | ||
| 165 | |||
| 166 | #if cleaned version does not match original, replace old with cleaned | ||
| 167 | else { | ||
| 168 | #but only if $orig010a has no non-digitchars | ||
| 169 | #### Work on this code so that non-digit characters are not a problem. | ||
| 170 | if ($orig010a !~ /^[ \d]*$/) {print ERRORS "$controlno\t010\t$orig010a\thas non-digits\n"; next;} | ||
| 171 | |||
| 172 | my @subfields = $field_010->subfields(); | ||
| 173 | my @newsubfields = (); | ||
| 174 | |||
| 175 | #break subfields into code-data array (so the entire field is in one array) | ||
| 176 | while (my $subfield = pop(@subfields)) { | ||
| 177 | my ($code, $data) = @$subfield; | ||
| 178 | #replace subfield a with cleaned data | ||
| 179 | if ($code eq 'a') {$data = $cleaned010a} | ||
| 180 | unshift (@newsubfields, $code, $data); | ||
| 181 | } # while subfields | ||
| 182 | |||
| 183 | #replace field in $record | ||
| 184 | my $newfield = MARC::Field->new ( | ||
| 185 | $field_010->tag(), | ||
| 186 | $field_010->indicator(1), | ||
| 187 | $field_010->indicator(2), | ||
| 188 | @newsubfields | ||
| 189 | ); | ||
| 190 | |||
| 191 | $field_010->replace_with($newfield); | ||
| 192 | $recordchanged = 1; | ||
| 193 | } #else old differs | ||
| 194 | if ($recordchanged) {print OUT $record->as_usmarc; | ||
| 195 | $cleanedreccount++; | ||
| 196 | |||
| 197 | ####for program debugging and testing#### | ||
| 198 | print ERRORS "$controlno cleaned from\t$orig010a\tto\t$cleaned010a\n"; | ||
| 199 | } | ||
| 200 | } # else record has 010subfielda | ||
| 201 | |||
| 202 | } # while records | ||
| 203 | |||
| 204 | close $inputfile; | ||
| 205 | close OUT; | ||
| 206 | print "$cleanedreccount records cleaned\n"; | ||
| 207 | ########################## | ||
| 208 | ### Main program done. ## | ||
| 209 | ### Report elapsed time.## | ||
| 210 | ########################## | ||
| 211 | |||
| 212 | my $elapsed = tv_interval ($t0); | ||
| 213 | my $calcelapsed = tv_interval ($t1); | ||
| 214 | print sprintf ("%.4f %s\n", "$elapsed", "seconds from execution\n"); | ||
| 215 | print sprintf ("%.4f %s\n", "$calcelapsed", "seconds to calculate\n"); | ||
| 216 | my $endingtime = MARC::BBMARC::startstop_time(); | ||
| 217 | print "Started at $startingtime\nEnded at $endingtime"; | ||
| 218 | |||
| 219 | if ($^O eq 'MacOS') { | ||
| 220 | #set creator and type to BBEdit and Text | ||
| 221 | MacPerl::SetFileInfo('R*ch', 'TEXT', $exportfile); | ||
| 222 | } | ||
| 223 | |||
| 224 | print "\n\nPress Enter to quit"; | ||
| 225 | <>; | ||
| 226 | |||
| 227 | |||
| 228 | ##################### | ||
| 229 | ### END OF PROGRAM ## | ||
| 230 | ##################### | ||
| 231 | |||
| 232 | =head1 LICENSE | ||
| 233 | |||
| 234 | This code may be distributed under the same terms as Perl itself. | ||
| 235 | |||
| 236 | Please note that this code is not a product of or supported by the | ||
| 237 | employers of the various contributors to the code. | ||
| 238 | |||
| 239 | =head1 AUTHOR | ||
| 240 | |||
| 241 | Bryan Baldus | ||
| 242 | eija [at] inwave [dot] com | ||
| 243 | |||
| 244 | Copyright (c) 2003-2004 | ||
| 245 | |||
| 246 | =cut |