1 package Equinox::Migration::MARCXMLSampler;
3 # Copyright 2009-2012, Equinox Software, Inc.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 use Equinox::Migration::SimpleTagList 1.001;
28 Equinox::Migration::MARCXMLSampler
36 our $VERSION = '1.003';
44 Produce a list of all fields in a MARCXML file which have a C<tag>
45 attribute, and count how many times each occurs
47 my $s = E::M::MARCXMLSampler->new( marcfile => "foo.marc.xml" );
50 Also deeply introspect certain tags, producing lists of all subfields,
51 and counts of how many times each subfield occurs I<in toto> and how
52 many records each subfield appears in
54 my $s = E::M::MARCXMLSampler->new( marcfile => "foo.marc.xml",
55 mapfile => "foo.map" );
58 my $s = E::M::MARCXMLSampler->new( marcfile => "foo.marc.xml",
59 mapstring => "852 999" );
68 Takes one required argument, C<marcfile>, which points to the MARCXML
71 Has two mutually-exclusive optional arguments, C<mapfile> and
72 C<mapstring>". The former should point to a file which will be used as
73 a L<Equinox::Migration::SimpleTagList> map; the latter should have as
74 its value a text string which will be used in the same way (handy for
75 when you only want deep introspection on a handful of tags).
80 my ($class, %args) = @_;
82 $dstore = { rcnt => 0, # record counter
83 tcnt => 0, # tag counter
84 scnt => {}, # subfield/tag counters
85 samp => {}, # data samples
86 tags => {}, # all found tags
89 my $self = bless { data => $dstore,
92 # if we have a sample arg, create the sample map
93 die "Can't use a mapfile and mapstring\n"
94 if ($args{mapfile} and $args{mapstring});
95 $taglist = Equinox::Migration::SimpleTagList->new(file => $args{mapfile})
97 $taglist = Equinox::Migration::SimpleTagList->new(str => $args{mapstring})
98 if ($args{mapstring});
100 # initialize twig and process xml
101 die "Argument 'marcfile' must be specified\n" unless ($args{marcfile});
102 if (-r $args{marcfile}) {
103 my $xmltwig = XML::Twig->new( twig_handlers => { record => \&parse_record } );
104 $xmltwig->parsefile( $args{marcfile} );
106 die "Can't open marc file: $!\n";
109 # hand ourselves back for datastore manipulation
116 XML::Twig handler for record elements; drives data extraction process.
121 my ($twig, $record) = @_;
123 my @fields = $record->children;
125 { process_field($f) }
127 # cleanup memory and increment pointer
135 my $tag = $field->{'att'}->{'tag'};
136 return unless ($tag and ($tag =~ /[^0-9]/ or $tag > 9));
138 # increment raw tag count
140 $dstore->{tags}{$tag}++;
143 if ($taglist and $taglist->has($tag)) {
144 my @subs = $field->children('subfield');
147 { process_subs($tag, $sub); $i++ }
149 # increment sub length counter
150 $dstore->{scnt}{$tag}{$i}++;
155 my ($tag, $sub) = @_;
156 my $code = $sub->{'att'}->{'code'};
158 # handle unmapped tag/subs
159 my $samp = $dstore->{samp};
160 # set a value, total-seen count and records-seen-in count
161 $samp->{$tag}{$code}{value} = $sub->text unless ($samp->{$tag}{$code}{value} and
162 $samp->{$tag}{$code}{value} =~ /\w/);
163 $samp->{$tag}{$code}{count}++;
164 $samp->{$tag}{$code}{tcnt}++ unless ( defined $samp->{$tag}{$code}{last} and
165 $samp->{$tag}{$code}{last} == $dstore->{tcnt} );
166 $samp->{$tag}{$code}{last} = $dstore->{tcnt};
172 If the C<mapfile> or C<mapstring> arguments are passed to L</new>, a
173 structure will be constructed which holds data about tags in the map.
176 sub_code => { value => VALUE,
185 For each subfield in each mapped tag, there is a hash of data about
186 that subfield containing
188 * value - A sample of the subfield text
189 * count - Total number of times the subfield was seen
190 * tcnt - The number of tags the subfield was seen in
194 Shawn Boyette, C<< <sboyette at esilibrary.com> >>
198 Please report any bugs or feature requests to the above email address.
202 You can find documentation for this module with the perldoc command.
204 perldoc Equinox::Migration::MARCXMLSampler
207 =head1 COPYRIGHT & LICENSE
209 Copyright 2009 Equinox, all rights reserved.
211 This program is free software; you can redistribute it and/or modify it
212 under the same terms as Perl itself.
217 1; # End of Equinox::Migration::MARCXMLSampler