]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1029.schema.update-broken-naco-links.sql
LP#1248734: (follow-up) add new indexes to schema update script
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1029.schema.update-broken-naco-links.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('1029', :eg_version); -- csharp/gmcharlt
4
5 UPDATE config.index_normalizer SET description = 'Apply NACO normalization rules to the extracted text.  See https://www.loc.gov/aba/pcc/naco/normrule-2.html for details.' WHERE func = 'naco_normalize';
6 UPDATE config.index_normalizer SET description = 'Apply NACO normalization rules to the extracted text, retaining the first comma.  See https://www.loc.gov/aba/pcc/naco/normrule-2.html for details.' WHERE func = 'naco_normalize_keep_comma';
7
8 CREATE OR REPLACE FUNCTION public.naco_normalize( TEXT, TEXT ) RETURNS TEXT AS $func$
9
10     use strict;
11     use Unicode::Normalize;
12     use Encode;
13
14     my $str = shift;
15     my $sf = shift;
16
17     # Apply NACO normalization to input string; based on
18     # https://www.loc.gov/aba/pcc/naco/documents/SCA_PccNormalization_Final_revised.pdf
19     #
20     # Note that unlike a strict reading of the NACO normalization rules,
21     # output is returned as lowercase instead of uppercase for compatibility
22     # with previous versions of the Evergreen naco_normalize routine.
23
24     # Convert to upper-case first; even though final output will be lowercase, doing this will
25     # ensure that the German eszett (ß) and certain ligatures (ff, fi, ffl, etc.) will be handled correctly.
26     # If there are any bugs in Perl's implementation of upcasing, they will be passed through here.
27     $str = uc $str;
28
29     # remove non-filing strings
30     $str =~ s/\x{0098}.*?\x{009C}//g;
31
32     $str = NFKD($str);
33
34     # additional substitutions - 3.6.
35     $str =~ s/\x{00C6}/AE/g;
36     $str =~ s/\x{00DE}/TH/g;
37     $str =~ s/\x{0152}/OE/g;
38     $str =~ tr/\x{0110}\x{00D0}\x{00D8}\x{0141}\x{2113}\x{02BB}\x{02BC}]['/DDOLl/d;
39
40     # transformations based on Unicode category codes
41     $str =~ s/[\p{Cc}\p{Cf}\p{Co}\p{Cs}\p{Lm}\p{Mc}\p{Me}\p{Mn}]//g;
42
43         if ($sf && $sf =~ /^a/o) {
44                 my $commapos = index($str, ',');
45                 if ($commapos > -1) {
46                         if ($commapos != length($str) - 1) {
47                 $str =~ s/,/\x07/; # preserve first comma
48                         }
49                 }
50         }
51
52     # since we've stripped out the control characters, we can now
53     # use a few as placeholders temporarily
54     $str =~ tr/+&@\x{266D}\x{266F}#/\x01\x02\x03\x04\x05\x06/;
55     $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;
56     $str =~ tr/\x01\x02\x03\x04\x05\x06\x07/+&@\x{266D}\x{266F}#,/;
57
58     # decimal digits
59     $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/;
60
61     # intentionally skipping step 8 of the NACO algorithm; if the string
62     # gets normalized away, that's fine.
63
64     # leading and trailing spaces
65     $str =~ s/\s+/ /g;
66     $str =~ s/^\s+//;
67     $str =~ s/\s+$//g;
68
69     return lc $str;
70 $func$ LANGUAGE 'plperlu' STRICT IMMUTABLE;
71
72 -- Currently, the only difference from naco_normalize is that search_normalize
73 -- turns apostrophes into spaces, while naco_normalize collapses them.
74 CREATE OR REPLACE FUNCTION public.search_normalize( TEXT, TEXT ) RETURNS TEXT AS $func$
75
76     use strict;
77     use Unicode::Normalize;
78     use Encode;
79
80     my $str = shift;
81     my $sf = shift;
82
83     # Apply NACO normalization to input string; based on
84     # https://www.loc.gov/aba/pcc/naco/documents/SCA_PccNormalization_Final_revised.pdf
85     #
86     # Note that unlike a strict reading of the NACO normalization rules,
87     # output is returned as lowercase instead of uppercase for compatibility
88     # with previous versions of the Evergreen naco_normalize routine.
89
90     # Convert to upper-case first; even though final output will be lowercase, doing this will
91     # ensure that the German eszett (ß) and certain ligatures (ff, fi, ffl, etc.) will be handled correctly.
92     # If there are any bugs in Perl's implementation of upcasing, they will be passed through here.
93     $str = uc $str;
94
95     # remove non-filing strings
96     $str =~ s/\x{0098}.*?\x{009C}//g;
97
98     $str = NFKD($str);
99
100     # additional substitutions - 3.6.
101     $str =~ s/\x{00C6}/AE/g;
102     $str =~ s/\x{00DE}/TH/g;
103     $str =~ s/\x{0152}/OE/g;
104     $str =~ tr/\x{0110}\x{00D0}\x{00D8}\x{0141}\x{2113}\x{02BB}\x{02BC}][/DDOLl/d;
105
106     # transformations based on Unicode category codes
107     $str =~ s/[\p{Cc}\p{Cf}\p{Co}\p{Cs}\p{Lm}\p{Mc}\p{Me}\p{Mn}]//g;
108
109         if ($sf && $sf =~ /^a/o) {
110                 my $commapos = index($str, ',');
111                 if ($commapos > -1) {
112                         if ($commapos != length($str) - 1) {
113                 $str =~ s/,/\x07/; # preserve first comma
114                         }
115                 }
116         }
117
118     # since we've stripped out the control characters, we can now
119     # use a few as placeholders temporarily
120     $str =~ tr/+&@\x{266D}\x{266F}#/\x01\x02\x03\x04\x05\x06/;
121     $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;
122     $str =~ tr/\x01\x02\x03\x04\x05\x06\x07/+&@\x{266D}\x{266F}#,/;
123
124     # decimal digits
125     $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/;
126
127     # intentionally skipping step 8 of the NACO algorithm; if the string
128     # gets normalized away, that's fine.
129
130     # leading and trailing spaces
131     $str =~ s/\s+/ /g;
132     $str =~ s/^\s+//;
133     $str =~ s/\s+$//g;
134
135     return lc $str;
136 $func$ LANGUAGE 'plperlu' STRICT IMMUTABLE;
137
138 COMMIT;