| 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 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use vars qw( @EXPORT_OK ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 15 | naco_from_string naco_from_array | ||||||||||||||||||||||||||||
| 16 | naco_from_field naco_from_authority | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | #func naco_from_string( Str $s, Bool :$keep_first_comma ) { | ||||||||||||||||||||||||||||
| 20 | 44 | ▶ | sub naco_from_string { | ||||||||||||||||||||||||||
|
Subroutine
naco_from_string: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 21 | 44 | my $s = shift; | |||||||||||||||||||||||||||
| 22 | 44 | ▶ | my $keep_first_comma = (@_ == 2) ? $_[1] : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | # decompose and uppercase | ||||||||||||||||||||||||||||
| 24 | 44 | $s = uc( NFD($s) ); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # strip out combining diacritics | ||||||||||||||||||||||||||||
| 27 | 44 | $s =~ s/\p{M}//g; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # transpose diagraphs and related characters | ||||||||||||||||||||||||||||
| 30 | 44 | $s =~ s/Æ/AE/g; | |||||||||||||||||||||||||||
| 31 | 44 | $s =~ s/Œ/OE/g; | |||||||||||||||||||||||||||
| 32 | 44 | $s =~ s/Ø|Ҩ/O/g; | |||||||||||||||||||||||||||
| 33 | 44 | $s =~ s/Þ/TH/g; | |||||||||||||||||||||||||||
| 34 | 44 | $s =~ s/Ð/D/g; | |||||||||||||||||||||||||||
| 35 | 44 | $s =~ s/ß/SS/g; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | # transpose sub- and super-script with numerals | ||||||||||||||||||||||||||||
| 38 | 44 | $s =~ tr/⁰¹²³⁴⁵⁶⁷⁸⁹/0123456789/; | |||||||||||||||||||||||||||
| 39 | 44 | $s =~ tr/₀₁₂₃₄₅₆₇₈₉/0123456789/; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | # delete or blank out punctuation | ||||||||||||||||||||||||||||
| 42 | 44 | $s =~ s/[!"()\-{}<>;:.?¿¡\/\\*\|%=±⁺⁻™℗©°^_`~]/ /g; | |||||||||||||||||||||||||||
| 43 | 44 | $s =~ s/['\[\]ЪЬ·]//g; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | # blank out commas | ||||||||||||||||||||||||||||
| 46 | 44 | ▶ | if ($keep_first_comma && $s =~ /,/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$keep_first_comma and $s =~ /,/
MC/DC100%2/2
$keep_first_comma and $s =~ /,/
$keep_first_comma
$s =~ /,/
Truth table100%3/3
A and B
A= $keep_first_commaB= $s =~ /,/
| |||||||||||||||||||||||||||||
| 47 | 18 | my $i = index $s, ','; | |||||||||||||||||||||||||||
| 48 | 18 | $s =~ s/,/ /g; | |||||||||||||||||||||||||||
| 49 | 18 | substr $s, $i, 1, ','; | |||||||||||||||||||||||||||
| 50 | # always strip off a trailing comma, even if it's the only one | ||||||||||||||||||||||||||||
| 51 | 18 | $s =~ s/,$//; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | else { | ||||||||||||||||||||||||||||
| 54 | 26 | $s =~ s/,/ /g; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # lastly, trim and deduplicate whitespace | ||||||||||||||||||||||||||||
| 58 | 44 | $s =~ s/\s\s+/ /g; | |||||||||||||||||||||||||||
| 59 | 44 | $s =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 44 | return $s; | |||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | #func naco_from_array( ArrayRef $subfs ) { | ||||||||||||||||||||||||||||
| 65 | 15 | ▶ | sub naco_from_array { | ||||||||||||||||||||||||||
|
Subroutine
naco_from_array: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 15 | my $subfs = shift; | |||||||||||||||||||||||||||
| 67 | # Expects $subfs == [ 'a', 'Thurber, James', 'd', '1914-', ... ] | ||||||||||||||||||||||||||||
| 68 | 15 | my $itr = natatime 2, @$subfs; | |||||||||||||||||||||||||||
| 69 | 15 | my $out = ''; | |||||||||||||||||||||||||||
| 70 | 15 | ▶ | while (my ($subf, $val) = $itr->()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 37 | my $norm = naco_from_string( $val, keep_first_comma => $subf eq 'a' ); | |||||||||||||||||||||||||||
| 72 | 37 | $out .= '$'. $subf . $norm; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 15 | return $out; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | #func naco_from_field( MARC::Field $f, :$subfields = 'a-df-hj-vx-z') { | ||||||||||||||||||||||||||||
| 78 | 12 | ▶ | sub naco_from_field { | ||||||||||||||||||||||||||
|
Subroutine
naco_from_field: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 12 | my $f = shift; | |||||||||||||||||||||||||||
| 80 | 12 | ▶ | my $subfields = (@_ == 2) ? $_[1] : 'a-df-hj-vx-z'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 12 | my @flat = map {@$_} grep {$_->[0] =~ /[$subfields]/} $f->subfields; | |||||||||||||||||||||||||||
| 82 | 12 | return naco_from_array( \@flat ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | #func naco_from_authority( MARC::Record $r ) { | ||||||||||||||||||||||||||||
| 86 | 2 | ▶ | sub naco_from_authority { | ||||||||||||||||||||||||||
|
Subroutine
naco_from_authority: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 2 | my $r = shift; | |||||||||||||||||||||||||||
| 88 | 2 | return naco_from_field( scalar $r->field('1..'), subfields => 'a-z' ); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | { | ||||||||||||||||||||||||||||
| 92 | 1 | ▶ | no warnings qw(once); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 1 | *MARC::Field::as_naco = \&naco_from_field; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 1 | 1; | |||||||||||||||||||||||||||
| 97 | __END__ | ||||||||||||||||||||||||||||