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.05';
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 # always strip off a trailing comma, even if it's the only one
49 $s =~ s/,$//;
50 }
51 else {
52 $s =~ s/,/ /g;
53 }
54  
55 # lastly, trim and deduplicate whitespace
56 $s =~ s/\s\s+/ /g;
57 $s =~ s/^\s+|\s+$//g;
58  
59 return $s;
60 }
61  
62 func naco_from_array( ArrayRef $subfs ) {
63 # Expects $subfs == [ 'a', 'Thurber, James', 'd', '1914-', ... ]
64 my $itr = natatime 2, @$subfs;
65 my $out = '';
66 while (my ($subf, $val) = $itr->()) {
67 my $norm = naco_from_string( $val, keep_first_comma => $subf eq 'a' );
68 $out .= '$'. $subf . $norm;
69 }
70 return $out;
71 }
72  
73 func naco_from_field( MARC::Field $f, :$subfields = 'a-df-hj-vx-z') {
74 my @flat = map {@$_} grep {$_->[0] =~ /[$subfields]/} $f->subfields;
75 return naco_from_array( \@flat );
76 }
77  
78 func naco_from_authority( MARC::Record $r ) {
79 return naco_from_field( scalar $r->field('1..'), subfields => 'a-z' );
80 }
81  
82 {
83 no warnings qw(once);
84 *MARC::Field::as_naco = \&naco_from_field;
85 }
86  
87 1;
88 __END__
89  
90 =encoding utf-8
91  
92 =head1 NAME
93  
94 MARC::Field::Normalize::NACO - Matching normalization for MARC::Field
95  
96 =head1 SYNOPSIS
97  
98 use MARC::Field;
99 use MARC::Field::Normalize::NACO;
100  
101 my $field = MARC::Field->new(
102 '100', ' ', ' ', a => 'Stephenson, Neal,', d => '1953-');
103 my $normalized = $field->as_naco;
104 my $custom = $field->as_naco(subfields => 'a');
105  
106 =head1 DESCRIPTION
107  
108 MARC::Field::Normalize::NACO turns MARC::Field objects into
109 strings canonicalized into NACO format. This makes them
110 suitable for matching against an index of similarly normalized
111 fields.
112  
113 The principal means of invoking is through the as_naco() method
114 that the module injects into MARC::Field when loaded. A string
115 is returned.
116  
117 This method takes an optional named parameter, subfields. The
118 value of this parameter should be something that fits nicely
119 into the regex qr/[$subfields]/, typically a range of letters.
120 The default value is "a-z68".
121  
122 =head1 AUTHOR
123  
124 Clay Fouts E<lt>cfouts@khephera.netE<gt>
125  
126 =head1 COPYRIGHT
127  
128 Copyright 2013 PTFS, Inc.
129  
130 =head1 LICENSE
131  
132 This library is free software; you can redistribute it and/or modify
133 it under the same terms as Perl itself.
134  
135 =head1 SEE ALSO
136  
137 =over
138  
139 =item *
140  
141 http://www.loc.gov/aba/pcc/naco/normrule-2.html
142  
143 =item *
144  
145 MARC::Record
146  
147 =back
148  
149 =cut
« Build ↑ Summary