blib/lib/Text/Normalize/NACO.pm

filter: Statementstmt 100% 44 / 44 Branchbran 100% 8 / 8 Conditioncond 100% 4 / 4 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 100% 70 / 70 CC 7 SCAR 7.6
CC
7
Cov
100%
naco_normalize
3.0
normalize
3.0
new
2.0
CRAP 2.1
↑ Summary
1 package Text::Normalize::NACO;
2  
3 =head1 NAME
4  
5 Text::Normalize::NACO - Normalize text based on the NACO rules
6  
7 =head1 SYNOPSIS
8  
9 # exported method
10 use Text::Normalize::NACO qw( naco_normalize );
11
12 $normalized = naco_normalize( $original );
13
14 # as an object
15 $naco = Text::Normalize::NACO->new;
16 $normalized = $naco->normalize( $original );
17  
18 # normalize to lowercase
19 $naco->case( 'lower' );
20 $normalized = $naco->normalize( $original );
21  
22 =head1 DESCRIPTION
23  
24 In general, normalization is defined as:
25  
26 To make (a text or language) regular and consistent, especially with respect to spelling or style.
27  
28 It is commonly used for comparative purposes. These particular normalization rules have been set out by the
29 Name Authority Cooperative. The rules are described in detail at: http://www.loc.gov/catdir/pcc/naco/normrule.html
30  
31 =head1 INSTALLATION
32  
33 perl Makefile.PL
34 make
35 make test
36 make install
37  
38 =cut
39  
40 4 ▶use base qw( Exporter );
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 4 ▶use Text::Unidecode;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 4 our $VERSION = '0.13';
48  
49 4 our @EXPORT_OK = qw( naco_normalize );
50  
51 =head1 METHODS
52  
53 =head2 new( %options )
54  
55 Creates a new Text::Normalize::NACO object. You explicitly request
56 strings to be normalized in upper or lower-case by setting
57 the "case" option (defaults to "upper").
58  
59 my $naco = Text::Normalize::NACO->new( case => 'lower' );
60  
61 =cut
62  
63 3 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
64 3 my $class = shift;
65 3 my %options = @_;
66 3 my $self = bless {}, $class;
67  
68 3 ▶ $self->case( $options{ case } || 'upper' );
Condition100%2/2
$options{'case'} || 'upper'
l!l
12
MC/DC100%1/1
$options{'case'} || 'upper'
$options{'case'}
Truth table100%2/2
A
A= $options{'case'}
Aresult
11
00
69  
70 3 return $self;
71 }
72  
73 =head2 case( $case )
74  
75 Accessor/Mutator for the case in which the string should be returned.
76  
77 # lower-case
78 $naco->case( 'lower' );
79  
80 # upper-case
81 $naco->case( 'upper' );
82  
83 =cut
84  
85 107 ▶sub case {
Subroutine case: called CC=2 SCAR=6.9
Pod documented
86 107 my $self = shift;
87 107 my ( $case ) = @_;
88  
89 107 ▶ $self->{ _CASE } = $case if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 4 103 107
90  
91 107 return $self->{ _CASE };
92 }
93  
94 =head2 naco_normalize( $text, { %options } )
95  
96 Exported version of C<normalize>. You can specify any extra
97 options by passing a hashref after the string to be normalized.
98  
99 my $normalized = naco_normalize( $original, { case => 'lower' } );
100  
101 =cut
102  
103 2 ▶sub naco_normalize {
Subroutine naco_normalize: called CC=3 SCAR=11.0
Pod documented
104 2 my $text = shift;
105 2 my $options = shift;
106 2 ▶ my $case = $options->{ case } || 'upper';
Condition100%2/2
$options->{'case'} || 'upper'
l!l
11
MC/DC100%1/1
$options->{'case'} || 'upper'
$options->{'case'}
Truth table100%2/2
A
A= $options->{'case'}
Aresult
11
00
107  
108 2 my $normalized = normalize( undef, $text );
109  
110 2 ▶ if ( $case eq 'lower' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($case eq 'lower') { } 1 1 2
111 1 $normalized =~ tr/A-Z/a-z/;
112 }
113 else {
114 1 $normalized =~ tr/a-z/A-Z/;
115 }
116  
117 2 return $normalized;
118 }
119  
120 =head2 normalize( $text )
121  
122 Normalizes $text and returns the new string.
123  
124 my $normalized = $naco->normalize( $original );
125  
126 =cut
127  
128 105 ▶sub normalize {
Subroutine normalize: called CC=3 SCAR=11.0
Pod documented
129 105 my $self = shift;
130 105 my $data = shift;
131  
132 # Rules taken from NACO Normalization
133 # http://lcweb.loc.gov/catdir/pcc/naco/normrule.html
134  
135 # Remove diacritical marks and convert special chars
136 105 unidecode( $data );
137  
138 # Convert special chars to spaces
139 105 $data =~ s/[\Q!(){}<>-;:.?,\/\\@*%=\$^_~\E]/ /g;
140  
141 # Delete special chars
142 105 $data =~ s/[\Q'[]|\E]//g;
143  
144 # Convert lowercase to uppercase or vice-versa.
145 105 ▶ if ( $self ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self) 103 2 105
146 103 ▶ if ( $self->case eq 'lower' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->case eq 'lower') { } 102 1 103
147 102 $data =~ tr/A-Z/a-z/;
148 }
149 else {
150 1 $data =~ tr/a-z/A-Z/;
151 }
152 }
153  
154 # Remove leading and trailing spaces
155 105 $data =~ s/^\s+|\s+$//g;
156  
157 # Condense multiple spaces
158 105 $data =~ s/\s+/ /g;
159  
160 105 return $data;
161 }
162  
163 =head1 SEE ALSO
164  
165 =over 4
166  
167 =item * http://www.loc.gov/catdir/pcc/naco/normrule.html
168  
169 =back
170  
171 =head1 AUTHOR
172  
173 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
174  
175 =head1 COPYRIGHT AND LICENSE
176  
177 Copyright 2004-2009 by Brian Cassidy
178  
179 This library is free software; you can redistribute it and/or modify
180 it under the same terms as Perl itself.
181  
182 =cut
183  
184 4 1;
↑ Summary