blib/lib/MARC/Field/Normalize/NACO.pm

filter: Statementstmt 100% 27 / 27 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 100% 36 / 36 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Build ↑ Summary
1 package MARC::Field::Normalize::NACO;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Unicode::Normalize qw(NFD);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MARC::Field;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Method::Signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 our $VERSION = '0.04';
12  
13 1 ▶use vars qw( @EXPORT_OK );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 @EXPORT_OK = qw(
16 naco_from_string naco_from_array
17 naco_from_field naco_from_authority
18 );
19  
20 func naco_from_string( Str $s, Bool :$keep_first_comma ) {
21 # decompose and uppercase
22 $s = uc( NFD($s) );
23  
24 # strip out combining diacritics
25 $s =~ s/\p{M}//g;
26  
27 # transpose diagraphs and related characters
28 $s =~ s/Æ/AE/g;
29 $s =~ s/Œ/OE/g;
30 $s =~ s/Ø|Ҩ/O/g;
31 $s =~ s/Þ/TH/g;
32 $s =~ s/Ð/D/g;
33 $s =~ s/ß/SS/g;
34  
35 # transpose sub- and super-script with numerals
36 $s =~ tr/⁰¹²³⁴⁵⁶⁷⁸⁹/0123456789/;
37 $s =~ tr/₀₁₂₃₄₅₆₇₈₉/0123456789/;
38  
39 # delete or blank out punctuation
40 $s =~ s/[!"()\-{}<>;:.?¿¡\/\\*\|%=±⁺⁻™℗©°^_`~]/ /g;
41 $s =~ s/['\[\]ЪЬ·]//g;
42  
43 # blank out commas
44 if ($keep_first_comma) {
45 my $i = index $s, ',';
46 $s =~ s/,/ /g;
47 $s =~ s/^((?:.){$i})\s/$1,/;
48 }
49 else {
50 $s =~ s/,/ /g;
51 }
52  
53 # lastly, trim and deduplicate whitespace
54 $s =~ s/\s\s+/ /g;
55 $s =~ s/^\s+|\s+$//g;
56  
57 return $s;
58 }
59  
60 func naco_from_array( ArrayRef $subfs ) {
61 # Expects $subfs == [ 'a', 'Thurber, James', 'd', '1914-', ... ]
62 my $itr = natatime 2, @$subfs;
63 my $out = '';
64 while (my ($subf, $val) = $itr->()) {
65 my $norm = naco_from_string( $val, keep_first_comma => $subf eq 'a' );
66 $out .= '$'. $subf . $norm;
67 }
68 return $out;
69 }
70  
71 func naco_from_field( MARC::Field $f, :$subfields = 'a-df-hj-vx-z') {
72 my @flat = map {@$_} grep {$_->[0] =~ /[$subfields]/} $f->subfields;
73 return naco_from_array( \@flat );
74 }
75  
76 func naco_from_authority( MARC::Record $r ) {
77 return naco_from_field( scalar $r->field('1..'), subfields => 'a-z' );
78 }
79  
80 {
81 no warnings qw(once);
82 *MARC::Field::as_naco = \&naco_from_field;
83 }
84  
85 1;
86 __END__
87  
88 =encoding utf-8
89  
90 =head1 NAME
91  
92 MARC::Field::Normalize::NACO - Matching normalization for MARC::Field
93  
94 =head1 SYNOPSIS
95  
96 use MARC::Field;
97 use MARC::Field::Normalize::NACO;
98  
99 my $field = MARC::Field->new(
100 '100', ' ', ' ', a => 'Stephenson, Neal,', d => '1953-');
101 my $normalized = $field->as_naco;
102 my $custom = $field->as_naco(subfields => 'a');
103  
104 =head1 DESCRIPTION
105  
106 MARC::Field::Normalize::NACO turns MARC::Field objects into
107 strings canonicalized into NACO format. This makes them
108 suitable for matching against an index of similarly normalized
109 fields.
110  
111 The principal means of invoking is through the as_naco() method
112 that the module injects into MARC::Field when loaded. A string
113 is returned.
114  
115 This method takes an optional named parameter, subfields. The
116 value of this parameter should be something that fits nicely
117 into the regex qr/[$subfields]/, typically a range of letters.
118 The default value is "a-z68".
119  
120 =head1 AUTHOR
121  
122 Clay Fouts E<lt>cfouts@khephera.netE<gt>
123  
124 =head1 COPYRIGHT
125  
126 Copyright 2013 PTFS, Inc.
127  
128 =head1 LICENSE
129  
130 This library is free software; you can redistribute it and/or modify
131 it under the same terms as Perl itself.
132  
133 =head1 SEE ALSO
134  
135 =over
136  
137 =item *
138  
139 http://www.loc.gov/aba/pcc/naco/normrule-2.html
140  
141 =item *
142  
143 MARC::Record
144  
145 =back
146  
147 =cut
« Build ↑ Summary