]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Utils/Normalize.pm
Revert "LP#1635737 Use new OpenSRF interval_to_seconds() context"
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Utils / Normalize.pm
1 package OpenILS::Utils::Normalize;
2 use strict;
3 use warnings;
4 use Unicode::Normalize;
5 use Encode;
6 use UNIVERSAL;
7 use MARC::Record;
8 use MARC::File::XML ( BinaryEncoding => 'UTF-8' );
9 use OpenILS::Application::AppUtils;
10
11 use Exporter 'import';
12 our @EXPORT_OK = qw( clean_marc naco_normalize search_normalize );
13
14 sub naco_normalize {
15     my $str = shift;
16     my $sf = shift;
17
18     # Apply NACO normalization to input string; based on
19     # https://www.loc.gov/aba/pcc/naco/documents/SCA_PccNormalization_Final_revised.pdf 
20     #
21     # Note that unlike a strict reading of the NACO normalization rules,
22     # output is returned as lowercase instead of uppercase for compatibility
23     # with previous versions of the Evergreen naco_normalize routine.
24     $str = _normalize_substitutions($str, $sf);
25
26     # Remove apostrophes, per NACO specs
27     $str =~ tr/'//d;
28     
29     $str = _normalize_codes($str, $sf);
30
31     return $str;
32 }
33
34 sub search_normalize {
35     my $str = shift;
36     my $sf = shift;
37
38     $str = _normalize_substitutions($str, $sf);
39     $str = _normalize_codes($str, $sf);
40     
41     return $str;
42 }
43
44 sub _normalize_substitutions {
45     my $str = shift;
46     my $sf = shift;
47
48     # Convert to upper-case first; even though final output will be lowercase, doing this will
49     # ensure that the German eszett (ß) and certain ligatures (ff, fi, ffl, etc.) will be handled correctly.
50     # If there are any bugs in Perl's implementation of upcasing, they will be passed through here.
51
52     $str = uc $str;
53
54     # remove non-filing strings
55     $str =~ s/\x{0098}.*?\x{009C}//g;
56
57     $str = NFKD($str);
58
59     # additional substitutions - 3.6.
60     $str =~ s/\x{00C6}/AE/g;
61     $str =~ s/\x{00DE}/TH/g;
62     $str =~ s/\x{0152}/OE/g;
63     $str =~ tr/\x{0110}\x{00D0}\x{00D8}\x{0141}\x{2113}\x{02BB}\x{02BC}][/DDOLl/d;
64
65     return $str;
66 }
67
68 sub _normalize_codes {
69     my $str = shift;
70     my $sf = shift;
71
72     # transformations based on Unicode category codes
73     $str =~ s/[\p{Cc}\p{Cf}\p{Co}\p{Cs}\p{Lm}\p{Mc}\p{Me}\p{Mn}]//g;
74
75     if ($sf && $sf =~ /^a/o) {
76         my $commapos = index($str, ',');
77         if ($commapos > -1) {
78             if ($commapos != length($str) - 1) {
79                 $str =~ s/,/\x07/; # preserve first comma
80             }
81         }
82     }
83
84     # since we've stripped out the control characters, we can now
85     # use a few as placeholders temporarily
86     $str =~ tr/+&@\x{266D}\x{266F}#/\x01\x02\x03\x04\x05\x06/;
87     $str =~ s/[\p{Pc}\p{Pd}\p{Pe}\p{Pf}\p{Pi}\p{Po}\p{Ps}\p{Sk}\p{Sm}\p{So}\p{Zl}\p{Zp}\p{Zs}]/ /g;
88     $str =~ tr/\x01\x02\x03\x04\x05\x06\x07/+&@\x{266D}\x{266F}#,/;
89
90     # decimal digits
91     $str =~ tr/\x{0660}-\x{0669}\x{06F0}-\x{06F9}\x{07C0}-\x{07C9}\x{0966}-\x{096F}\x{09E6}-\x{09EF}\x{0A66}-\x{0A6F}\x{0AE6}-\x{0AEF}\x{0B66}-\x{0B6F}\x{0BE6}-\x{0BEF}\x{0C66}-\x{0C6F}\x{0CE6}-\x{0CEF}\x{0D66}-\x{0D6F}\x{0E50}-\x{0E59}\x{0ED0}-\x{0ED9}\x{0F20}-\x{0F29}\x{1040}-\x{1049}\x{1090}-\x{1099}\x{17E0}-\x{17E9}\x{1810}-\x{1819}\x{1946}-\x{194F}\x{19D0}-\x{19D9}\x{1A80}-\x{1A89}\x{1A90}-\x{1A99}\x{1B50}-\x{1B59}\x{1BB0}-\x{1BB9}\x{1C40}-\x{1C49}\x{1C50}-\x{1C59}\x{A620}-\x{A629}\x{A8D0}-\x{A8D9}\x{A900}-\x{A909}\x{A9D0}-\x{A9D9}\x{AA50}-\x{AA59}\x{ABF0}-\x{ABF9}\x{FF10}-\x{FF19}/0-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-90-9/;
92
93     # intentionally skipping step 8 of the NACO algorithm; if the string
94     # gets normalized away, that's fine.
95
96     # leading and trailing spaces
97     $str =~ s/\s+/ /g;
98     $str =~ s/^\s+//;
99     $str =~ s/\s+$//g;
100
101     return lc $str;
102 }
103
104 # Cleans up a MARC::Record or MARCXML string for storage in the
105 # Open-ILS database.
106 #
107 # Takes either a MARC::Record or a string of MARCXML.
108 #
109 # Returns a string of MARCXML as Open-ILS likes to store it.
110 #
111 # Assumes input is already in UTF-8.
112 sub clean_marc {
113     my $input = shift;
114     my $xml = (UNIVERSAL::isa($input, 'MARC::Record')) ? $input->as_xml_record() : $input;
115     $xml =~ s/\n//sog;
116     $xml =~ s/^<\?xml.+\?\s*>//go;
117     $xml =~ s/>\s+</></go;
118     $xml =~ s/\p{Cc}//go;
119     $xml = OpenILS::Application::AppUtils->entityize($xml);
120     $xml =~ s/[\x00-\x1f]//go;
121     return $xml;
122 }
123
124 1;