| 1 | package MARC::Record::Stats; | ||||||||||||
| 2 | |||||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 3 | ▶ | use version; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 3 | ▶ | use MARC::Record::Stats::Report; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | |||||||||||||
| 9 | =head1 NAME | ||||||||||||
| 10 | |||||||||||||
| 11 | MARC::Record::Stats - scans one or many MARC::Record and gives a statistics on the tags and subtags | ||||||||||||
| 12 | |||||||||||||
| 13 | =head1 VERSION | ||||||||||||
| 14 | |||||||||||||
| 15 | Version 0.0.4 | ||||||||||||
| 16 | |||||||||||||
| 17 | =cut | ||||||||||||
| 18 | |||||||||||||
| 19 | 3 | our $VERSION = qv('0.0.4'); | |||||||||||
| 20 | |||||||||||||
| 21 | |||||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||||
| 23 | |||||||||||||
| 24 | This module provides functionality for L<marcstats.pl> script. | ||||||||||||
| 25 | Description of the module interface follows. | ||||||||||||
| 26 | |||||||||||||
| 27 | use MARC::Record::Stats; | ||||||||||||
| 28 | |||||||||||||
| 29 | { | ||||||||||||
| 30 | my $records = []; | ||||||||||||
| 31 | |||||||||||||
| 32 | # code skipped ... | ||||||||||||
| 33 | my $stats = MARC::Record::Stats->new; | ||||||||||||
| 34 | |||||||||||||
| 35 | # $records is array of MARC::Record | ||||||||||||
| 36 | for my $r ( @$records ) { | ||||||||||||
| 37 | $stats->add_record_to_stats( $r ); | ||||||||||||
| 38 | } | ||||||||||||
| 39 | |||||||||||||
| 40 | $stats->report( *STDOUT, { dots => 1 } ); | ||||||||||||
| 41 | } | ||||||||||||
| 42 | |||||||||||||
| 43 | ### | ||||||||||||
| 44 | ### Some useless features: | ||||||||||||
| 45 | ### | ||||||||||||
| 46 | { | ||||||||||||
| 47 | my $record; | ||||||||||||
| 48 | my $records = []; | ||||||||||||
| 49 | # code skipped ... | ||||||||||||
| 50 | |||||||||||||
| 51 | # single record statistics | ||||||||||||
| 52 | # $record is a MARC::Record | ||||||||||||
| 53 | my $stats1 = Marc::Record::Stats->new( $record ); | ||||||||||||
| 54 | |||||||||||||
| 55 | # merge $stats1 and statistics for $records | ||||||||||||
| 56 | # $records is a reference to an array of MARC::Record | ||||||||||||
| 57 | my $stats2 = Marc::Record::Stats->new( $records, $stats1 ); | ||||||||||||
| 58 | # ... | ||||||||||||
| 59 | |||||||||||||
| 60 | $stats1->report( *STDOUT ); | ||||||||||||
| 61 | # $stats2->report( *STDOUT ); | ||||||||||||
| 62 | } | ||||||||||||
| 63 | |||||||||||||
| 64 | =head1 METHODS | ||||||||||||
| 65 | |||||||||||||
| 66 | =head2 new $records [, $stats] | ||||||||||||
| 67 | |||||||||||||
| 68 | Builds statistics on $records, appends $stats if given. | ||||||||||||
| 69 | |||||||||||||
| 70 | =over 4 | ||||||||||||
| 71 | |||||||||||||
| 72 | =item $records | ||||||||||||
| 73 | A MARC::Record or a reference to an array of MARC::Record | ||||||||||||
| 74 | |||||||||||||
| 75 | =item $stats | ||||||||||||
| 76 | Marc::Record::Stats object that contains accumulated statistics. | ||||||||||||
| 77 | |||||||||||||
| 78 | =back | ||||||||||||
| 79 | |||||||||||||
| 80 | =cut | ||||||||||||
| 81 | |||||||||||||
| 82 | 3 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 83 | 3 | my ($class, $records, $stats) = @_; | |||||||||||
| 84 | |||||||||||||
| 85 | 3 | my $self = { | |||||||||||
| 86 | stats => { nrecords => 0 }, | ||||||||||||
| 87 | }; | ||||||||||||
| 88 | |||||||||||||
| 89 | 3 | bless $self, $class; | |||||||||||
| 90 | |||||||||||||
| 91 | 3 | ▶ | $self->_copy_stats($stats) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 92 | if $stats; | ||||||||||||
| 93 | |||||||||||||
| 94 | 3 | ▶ | my $reclist = (ref $records eq 'ARRAY') ? $records : [ $records ]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 3 | foreach my $rec ( @$reclist ) { | |||||||||||
| 96 | 4 | $self->add_record_to_stats( $rec ); | |||||||||||
| 97 | } | ||||||||||||
| 98 | 3 | return $self; | |||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | =head2 report $fh, $config | ||||||||||||
| 102 | |||||||||||||
| 103 | Prints out a report on the collected statistics to a filehandle $fh. | ||||||||||||
| 104 | $config keeps configuretion for the reporter. See L<MARC::Record::Stats::Report> | ||||||||||||
| 105 | for details | ||||||||||||
| 106 | |||||||||||||
| 107 | =cut | ||||||||||||
| 108 | |||||||||||||
| 109 | 0 | ▶ | sub report { | ||||||||||
|
Subroutine
report: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 110 | 0 | my ($self, $fh, $config) = @_; | |||||||||||
| 111 | 0 | MARC::Record::Stats::Report->report($fh, $self, $config); | |||||||||||
| 112 | } | ||||||||||||
| 113 | |||||||||||||
| 114 | |||||||||||||
| 115 | =head2 get_stats_hash | ||||||||||||
| 116 | |||||||||||||
| 117 | Returns a hashref that contains the statistics: | ||||||||||||
| 118 | |||||||||||||
| 119 | <stats_hash> = { | ||||||||||||
| 120 | nrecords => <int>, # the number of records | ||||||||||||
| 121 | tags => { | ||||||||||||
| 122 | <tag> => <tagstat>, # for every tag found in records | ||||||||||||
| 123 | ... | ||||||||||||
| 124 | } | ||||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | <tag> = \d{3} # a tag, three digits | ||||||||||||
| 128 | |||||||||||||
| 129 | <tag_stat> = { | ||||||||||||
| 130 | occurence => <int>, # how many records contain this tag | ||||||||||||
| 131 | subtags => <subtag_stat>, | ||||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | <subtag_stat> = { | ||||||||||||
| 135 | <subtag> => { | ||||||||||||
| 136 | occurence => <int>, # how many records contain this subtag | ||||||||||||
| 137 | repeatable => <0|1>, # whether or not is repeatable | ||||||||||||
| 138 | } | ||||||||||||
| 139 | } | ||||||||||||
| 140 | |||||||||||||
| 141 | <subtag> = [a-z0-9] # alphanum, subtag | ||||||||||||
| 142 | |||||||||||||
| 143 | =cut | ||||||||||||
| 144 | |||||||||||||
| 145 | 10 | ▶ | sub get_stats_hash { return $_[0]->{stats} } | ||||||||||
|
Subroutine
get_stats_hash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 146 | |||||||||||||
| 147 | |||||||||||||
| 148 | =begin DEVELOPER | ||||||||||||
| 149 | |||||||||||||
| 150 | Deep copy of stats | ||||||||||||
| 151 | |||||||||||||
| 152 | =end DEVELOPER | ||||||||||||
| 153 | |||||||||||||
| 154 | =cut | ||||||||||||
| 155 | |||||||||||||
| 156 | 1 | ▶ | sub _copy_stats { | ||||||||||
|
Subroutine
_copy_stats: called CC=3 SCAR=11.0
| |||||||||||||
| 157 | 1 | my ($self, $stats) = @_; | |||||||||||
| 158 | 1 | my $stathash = $stats->get_stats_hash; | |||||||||||
| 159 | 1 | my $selfstat = $self->get_stats_hash; | |||||||||||
| 160 | |||||||||||||
| 161 | 1 | $selfstat->{nrecords} = $stathash->{nrecords}; | |||||||||||
| 162 | 1 | foreach my $tag ( keys %{ $stathash->{tags} } ) { | |||||||||||
| 163 | 4 | my $tagstat = $stathash->{tags}->{$tag}; | |||||||||||
| 164 | 4 | $selfstat->{tags}->{$tag}->{occurence} = $tagstat->{occurence}; | |||||||||||
| 165 | 4 | $selfstat->{tags}->{$tag}->{repeatable} = $tagstat->{repeatable}; | |||||||||||
| 166 | 4 | $selfstat->{tags}->{$tag}->{subtags} = { }; | |||||||||||
| 167 | 4 | foreach my $subtag ( keys %{ $tagstat->{subtags} } ) { | |||||||||||
| 168 | 4 | $selfstat->{tags}->{$tag}->{subtags}->{$subtag}->{occurence} = $tagstat->{subtags}->{$subtag}->{occurence}; | |||||||||||
| 169 | 4 | $selfstat->{tags}->{$tag}->{subtags}->{$subtag}->{repeatable} = $tagstat->{subtags}->{$subtag}->{repeatable}; | |||||||||||
| 170 | } | ||||||||||||
| 171 | } | ||||||||||||
| 172 | } | ||||||||||||
| 173 | |||||||||||||
| 174 | =head2 add_record_to_stats $record | ||||||||||||
| 175 | |||||||||||||
| 176 | Add $record to statistics. | ||||||||||||
| 177 | |||||||||||||
| 178 | =cut | ||||||||||||
| 179 | |||||||||||||
| 180 | 4 | ▶ | sub add_record_to_stats { | ||||||||||
|
Subroutine
add_record_to_stats: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 181 | 4 | my ($self, $record) = @_; | |||||||||||
| 182 | |||||||||||||
| 183 | 4 | ▶ | return unless $record; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 184 | |||||||||||||
| 185 | 4 | my $stats = $self->get_stats_hash; | |||||||||||
| 186 | |||||||||||||
| 187 | 4 | $stats->{nrecords}++; | |||||||||||
| 188 | |||||||||||||
| 189 | 4 | my $record_stats = $self->get_record_stats($record); | |||||||||||
| 190 | |||||||||||||
| 191 | 4 | foreach my $tag ( keys %$record_stats ) { | |||||||||||
| 192 | 16 | $stats->{tags}->{$tag}->{occurence}++; | |||||||||||
| 193 | 16 | ▶ | $stats->{tags}->{$tag}->{subtags} ||= {}; | ||||||||||
|
Condition100%2/2
$stats->{"tags"}{$tag}{"subtags"} ||= {}
MC/DC100%1/1
$stats->{"tags"}{$tag}{"subtags"} ||= {}
$stats->{"tags"}{$tag}{"subtags"}
Truth table100%2/2
A
A= $stats->{"tags"}{$tag}{"subtags"}
| |||||||||||||
| 194 | |||||||||||||
| 195 | $stats->{tags}->{$tag}->{repeatable} = | ||||||||||||
| 196 | 16 | ▶ | $record_stats->{$tag}->{occurence} > 1 ? | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 197 | 1 : 0; | ||||||||||||
| 198 | |||||||||||||
| 199 | 16 | my $subtag_stats = $stats->{tags}->{$tag}->{subtags}; | |||||||||||
| 200 | |||||||||||||
| 201 | 16 | foreach my $subtag ( keys %{ $record_stats->{$tag}->{subtags} } ) { | |||||||||||
| 202 | 18 | $subtag_stats->{$subtag}->{occurence}++; | |||||||||||
| 203 | |||||||||||||
| 204 | $subtag_stats->{$subtag}->{repeatable} = | ||||||||||||
| 205 | 18 | ▶ | $record_stats->{$tag}->{subtags}->{$subtag} > 1 ? | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 206 | 1 : 0; | ||||||||||||
| 207 | } | ||||||||||||
| 208 | } | ||||||||||||
| 209 | } | ||||||||||||
| 210 | |||||||||||||
| 211 | =head2 get_record_stats $record | ||||||||||||
| 212 | |||||||||||||
| 213 | returns a reference to a hash: { <tag> => <tag_data> } | ||||||||||||
| 214 | where <tag_data> is a reference to a hash with the keys | ||||||||||||
| 215 | I<occurence> - how many times the field with the tag | ||||||||||||
| 216 | <tag> was found in the record, I<subtags> - result of | ||||||||||||
| 217 | subtag_stats. | ||||||||||||
| 218 | |||||||||||||
| 219 | =cut | ||||||||||||
| 220 | |||||||||||||
| 221 | 4 | ▶ | sub get_record_stats { | ||||||||||
|
Subroutine
get_record_stats: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 222 | 4 | my ($self, $record) = @_; | |||||||||||
| 223 | 4 | my $stats; | |||||||||||
| 224 | |||||||||||||
| 225 | 4 | foreach my $field ( $record->fields ) { | |||||||||||
| 226 | 18 | my $tag = $field->tag; | |||||||||||
| 227 | |||||||||||||
| 228 | 18 | $stats->{$tag}->{occurence}++; | |||||||||||
| 229 | |||||||||||||
| 230 | 18 | ▶ | if( $tag > 9 ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 231 | 14 | ▶ | $stats->{$tag}->{subtags} = $self->subtag_stats($field) || { }; | ||||||||||
|
Condition50%1/2
$self->subtag_stats($field) || {}
MC/DC0%0/1
$self->subtag_stats($field) || {}
$self->subtag_stats($field)
Truth table50%1/2
A
A= $self->subtag_stats($field)
| |||||||||||||
| 232 | } | ||||||||||||
| 233 | } | ||||||||||||
| 234 | 4 | return $stats; | |||||||||||
| 235 | } | ||||||||||||
| 236 | |||||||||||||
| 237 | =head2 subtag_stats $field | ||||||||||||
| 238 | |||||||||||||
| 239 | returns a reference to a hash { <subtag letter> => <occurence> } | ||||||||||||
| 240 | where <occurence> is the number of times the subfield with | ||||||||||||
| 241 | the code <subtag letter> was found in the fied $field. | ||||||||||||
| 242 | |||||||||||||
| 243 | $field is MARC::Field | ||||||||||||
| 244 | |||||||||||||
| 245 | =cut | ||||||||||||
| 246 | |||||||||||||
| 247 | 14 | ▶ | sub subtag_stats { | ||||||||||
|
Subroutine
subtag_stats: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 248 | 14 | my ($self, $field) = @_; | |||||||||||
| 249 | 14 | my $substat = { }; | |||||||||||
| 250 | |||||||||||||
| 251 | 14 | foreach my $subtag ( $field->subfields ) { | |||||||||||
| 252 | 22 | $substat->{ $subtag->[0] }++; | |||||||||||
| 253 | } | ||||||||||||
| 254 | |||||||||||||
| 255 | 14 | return $substat; | |||||||||||
| 256 | } | ||||||||||||
| 257 | |||||||||||||
| 258 | 3 | 1; # End of Marc::Record::Stats | |||||||||||
| 259 | __END__ | ||||||||||||