dynamic scoring integraation
[migration-tools.git] / filter_record.ids
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Getopt::Long;
6
7 # configuration hashref
8 my $conf  = {};
9 initialize($conf);
10
11 my %id;
12
13 open F, "<", $conf->{idfile};
14 while (<F>) {
15         chomp;
16         $id{$_} = 1;
17 }
18 close F;
19
20 my $M; my $I; my $S;
21 open $M, '<:utf8', $conf->{marcfile}
22   or die "Can't open marcfile '",$conf->{marcfile},"'\n";
23 open $I, '>:utf8', $conf->{'output-import'}
24   or die "Can't open import file '",$conf->{'output-import'},"'\n";
25 open $S, '>:utf8', $conf->{'output-shelved'}
26   or die "Can't open shelf file '",$conf->{'output-shelve'},"'\n";
27
28 while (<$M>) {
29     my $tag = $conf->{tag};
30     my $sub = $conf->{subfield};
31
32     /tag="$tag" ind1=" " ind2=" ">.*?<subfield code="$sub">(\d+)</;
33     if ($conf->{mode} eq "exclude") {
34         print $S $_ if ($id{$1});
35         print $I $_ unless ($id{$1});;
36     } else {
37         print $S $_ unless ($id{$1});
38         print $I $_ if ($id{$1});;
39     }
40     $conf->{count}++;
41
42     unless ($conf->{count} % 100) {
43         print STDERR "\rProcessed: ",$conf->{count};
44     }
45 }
46
47 =head2 initialize
48
49 Performs boring script initialization. Handles argument parsing,
50 mostly.
51
52 =cut
53
54 sub initialize {
55     my ($c) = @_;
56     my @missing = ();
57
58     # set mode on existing filehandles
59     binmode(STDIN, ':utf8');
60
61     my $rc = GetOptions( $c,
62                          'mode=s',
63                          'include',
64                          'tag|t=i',
65                          'subfield|s=s',
66                          'idfile|i=s',
67                          'marcfile|m=s',
68                          'output-import|oi=s',
69                          'output-shelved|os=s',
70                          'help|h',
71                        );
72     show_help() unless $rc;
73     show_help() if ($c->{help});
74
75     my @keys = keys %{$c};
76     for my $key ('mode', 'idfile', 'marcfile', 'tag', 'subfield',
77                  'output-import', 'output-shelved')
78       { push @missing, $key unless $c->{$key} }
79     if (@missing) {
80         print "Required option: ", join(', ', @missing), " missing!\n";
81         show_help();
82     }
83     unless ($c->{mode} eq "include" or $c->{mode} eq "exclude") {
84         print "Unknown run mode '", $c->{mode}, "'\n";
85         show_help();
86     }
87 }
88
89
90 =head2 show_help
91
92 Display usage message when things go wrong
93
94 =cut
95
96 sub show_help {
97 print <<HELP;
98 Usage is: $0 [ARGS]
99
100   --mode=MODE           Runmode to use (exclude, include; usually exclude)
101   --idfile         -i   File of record ids to use as source for matchpoints
102   --marcfile       -m   MARCXML source file
103   --tag            -t   MARC tag to use as matchpoint (903 when matching
104                         incoming records, 901 when matching incumbents)
105   --subfield       -s   Subfield of tag to use ('a' for incoming, 'c'
106                         for incumbent)
107   --output-import  -oi  Output MARCXML file for records to be imported
108   --output-shelved -os  Output MARCXML file for records to be ignored
109
110 If '--mode=exclude' is specified, the record ids in the file specified
111 by -idfile will be used as EXCLUSION data.
112
113 That is, the given record ids will be treated as records which match
114 incumbent records, are being compressed into existing data, and so
115 WILL NOT be imported. The --output-import file will contain records
116 whose ids DO NOT occur in --idfile; --output-shelved will contain the
117 records which DO occur.
118
119 If '--mode=include' is specified, the reverse occurs.
120 HELP
121 exit 1;
122 }