Create a search_normalize() variant of naco_normalize()
authorDan Scott <dscott@laurentian.ca>
Thu, 10 Nov 2011 06:05:40 +0000 (01:05 -0500)
committerDan Scott <dscott@laurentian.ca>
Mon, 14 Nov 2011 21:45:57 +0000 (16:45 -0500)
In the quest to support searching text that contains leading articles
joined by an apostrophe - for example, "l'histoire" - such that a
searcher can enter either "l'histoire" or "histoire" and get results -
add a variant of naco_normalize() that does not strip the apostrophe
entirely, but rather replaces it with a space such that the root word
can be indexed appropriately.

This implementation refactors the OpenILS::Utils::Normalize code to make
the differences between search_normalize() and naco_normalize() as clear
as possible, but duplicates code significantly in the in-db version of
the code. Someday maybe the database can rely on
OpenILS::Utils::Normalize instead of inline functions :)

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/QueryParser.pm
Open-ILS/src/perlmods/lib/OpenILS/Utils/Normalize.pm
Open-ILS/src/perlmods/t/14-OpenILS-Utils.t
Open-ILS/src/sql/Pg/002.functions.config.sql
Open-ILS/src/sql/Pg/950.data.seed-values.sql

index 41ed64c..8affc65 100644 (file)
@@ -359,7 +359,7 @@ sub TEST_SETUP {
     __PACKAGE__->add_relevance_bump( title => translated => full_match => 20 );
     
     __PACKAGE__->add_search_field_id_map( title => proper => 6 => 1 );
-    __PACKAGE__->add_query_normalizer( title => proper => 'naco_normalize' );
+    __PACKAGE__->add_query_normalizer( title => proper => 'search_normalize' );
     __PACKAGE__->add_relevance_bump( title => proper => first_word => 1.5 );
     __PACKAGE__->add_relevance_bump( title => proper => full_match => 20 );
     __PACKAGE__->add_relevance_bump( title => proper => word_order => 10 );
@@ -373,7 +373,7 @@ sub TEST_SETUP {
     __PACKAGE__->add_search_field_id_map( author => personal => 8 => 1 );
     __PACKAGE__->add_relevance_bump( author => personal => first_word => 1.5 );
     __PACKAGE__->add_relevance_bump( author => personal => full_match => 20 );
-    __PACKAGE__->add_query_normalizer( author => personal => 'naco_normalize' );
+    __PACKAGE__->add_query_normalizer( author => personal => 'search_normalize' );
     __PACKAGE__->add_query_normalizer( author => personal => 'split_date_range' );
     
     __PACKAGE__->add_facet_field_id_map( subject => topic => 14 => 1 );
@@ -401,8 +401,8 @@ sub TEST_SETUP {
     __PACKAGE__->add_search_class_alias( series => 'se' );
     __PACKAGE__->add_search_class_alias( keyword => 'dc.identifier' );
     
-    __PACKAGE__->add_query_normalizer( author => corporate => 'naco_normalize' );
-    __PACKAGE__->add_query_normalizer( keyword => keyword => 'naco_normalize' );
+    __PACKAGE__->add_query_normalizer( author => corporate => 'search_normalize' );
+    __PACKAGE__->add_query_normalizer( keyword => keyword => 'search_normalize' );
     
     __PACKAGE__->add_search_field_alias( subject => name => 'bib.subjectName' );
     
@@ -693,13 +693,13 @@ sub rel_bump {
     return '' if (!@$only_atoms);
 
     if ($bump eq 'first_word') {
-        return " /* first_word */ COALESCE(NULLIF( (naco_normalize(".$node->table_alias.".value) ~ ('^'||naco_normalize(".$self->QueryParser->quote_value($only_atoms->[0]->content)."))), FALSE )::INT * $multiplier, 1)";
+        return " /* first_word */ COALESCE(NULLIF( (search_normalize(".$node->table_alias.".value) ~ ('^'||search_normalize(".$self->QueryParser->quote_value($only_atoms->[0]->content)."))), FALSE )::INT * $multiplier, 1)";
     } elsif ($bump eq 'full_match') {
-        return " /* full_match */ COALESCE(NULLIF( (naco_normalize(".$node->table_alias.".value) ~ ('^'||".
-                    join( "||' '||", map { "naco_normalize(".$self->QueryParser->quote_value($_->content).")" } @$only_atoms )."||'\$')), FALSE )::INT * $multiplier, 1)";
+        return " /* full_match */ COALESCE(NULLIF( (search_normalize(".$node->table_alias.".value) ~ ('^'||".
+                    join( "||' '||", map { "search_normalize(".$self->QueryParser->quote_value($_->content).")" } @$only_atoms )."||'\$')), FALSE )::INT * $multiplier, 1)";
     } elsif ($bump eq 'word_order') {
-        return " /* word_order */ COALESCE(NULLIF( (naco_normalize(".$node->table_alias.".value) ~ (".
-                    join( "||'.*'||", map { "naco_normalize(".$self->QueryParser->quote_value($_->content).")" } @$only_atoms ).")), FALSE )::INT * $multiplier, 1)";
+        return " /* word_order */ COALESCE(NULLIF( (search_normalize(".$node->table_alias.".value) ~ (".
+                    join( "||'.*'||", map { "search_normalize(".$self->QueryParser->quote_value($_->content).")" } @$only_atoms ).")), FALSE )::INT * $multiplier, 1)";
     }
 
     return '';
index d71503c..e3e699f 100644 (file)
@@ -5,10 +5,9 @@ use Unicode::Normalize;
 use Encode;
 
 use Exporter 'import';
-our @EXPORT_OK = qw( naco_normalize );
+our @EXPORT_OK = qw( naco_normalize search_normalize );
 
 sub naco_normalize {
-
     my $str = decode_utf8(shift);
     my $sf = shift;
 
@@ -18,10 +17,34 @@ sub naco_normalize {
     # Note that unlike a strict reading of the NACO normalization rules,
     # output is returned as lowercase instead of uppercase for compatibility
     # with previous versions of the Evergreen naco_normalize routine.
+    $str = _normalize_substitutions($str, $sf);
+
+    # Remove apostrophes, per NACO specs
+    $str =~ tr/'//d;
+    
+    $str = _normalize_codes($str, $sf);
+
+    return $str;
+}
+
+sub search_normalize {
+    my $str = decode_utf8(shift);
+    my $sf = shift;
+
+    $str = _normalize_substitutions($str, $sf);
+    $str = _normalize_codes($str, $sf);
+    
+    return $str;
+}
+
+sub _normalize_substitutions {
+    my $str = shift;
+    my $sf = shift;
 
     # Convert to upper-case first; even though final output will be lowercase, doing this will
     # ensure that the German eszett (ß) and certain ligatures (ff, fi, ffl, etc.) will be handled correctly.
     # If there are any bugs in Perl's implementation of upcasing, they will be passed through here.
+
     $str = uc $str;
 
     # remove non-filing strings
@@ -33,7 +56,14 @@ sub naco_normalize {
     $str =~ s/\x{00C6}/AE/g;
     $str =~ s/\x{00DE}/TH/g;
     $str =~ s/\x{0152}/OE/g;
-    $str =~ tr/\x{0110}\x{00D0}\x{00D8}\x{0141}\x{2113}\x{02BB}\x{02BC}]['/DDOLl/d;
+    $str =~ tr/\x{0110}\x{00D0}\x{00D8}\x{0141}\x{2113}\x{02BB}\x{02BC}][/DDOLl/d;
+
+    return $str;
+}
+
+sub _normalize_codes {
+    my $str = shift;
+    my $sf = shift;
 
     # transformations based on Unicode category codes
     $str =~ s/[\p{Cc}\p{Cf}\p{Co}\p{Cs}\p{Lm}\p{Mc}\p{Me}\p{Mn}]//g;
index 28a7267..924e2a3 100644 (file)
@@ -1,6 +1,6 @@
 #!perl -T
 
-use Test::More tests => 20;
+use Test::More tests => 22;
 
 use_ok( 'OpenILS::Utils::Configure' );
 use_ok( 'OpenILS::Utils::Cronscript' );
@@ -37,3 +37,9 @@ is(@comp_holdings, 0, "Compressed holdings for an MFHD record that only has a ca
 
 my @decomp_holdings = $co_mfhd->get_decompressed_holdings($co_mfhd->field('853'));
 is(@decomp_holdings, 0, "Decompressed holdings for an MFHD record that only has a caption");
+
+my $apostring = OpenILS::Utils::Normalize::naco_normalize("it's time");
+is($apostring, "its time", "naco_normalize: strip apostrophes");
+
+my $apos = OpenILS::Utils::Normalize::search_normalize("it's time");
+is($apos, "it s time", "search_normalize: replace apostrophes with space");
index 4836384..3ea61ae 100644 (file)
@@ -699,6 +699,72 @@ CREATE OR REPLACE FUNCTION public.naco_normalize( TEXT, TEXT ) RETURNS TEXT AS $
     return lc $str;
 $func$ LANGUAGE 'plperlu' STRICT IMMUTABLE;
 
+-- Currently, the only difference from naco_normalize is that search_normalize
+-- turns apostrophes into spaces, while naco_normalize collapses them.
+CREATE OR REPLACE FUNCTION public.search_normalize( TEXT, TEXT ) RETURNS TEXT AS $func$
+
+    use strict;
+    use Unicode::Normalize;
+    use Encode;
+
+    my $str = decode_utf8(shift);
+    my $sf = shift;
+
+    # Apply NACO normalization to input string; based on
+    # http://www.loc.gov/catdir/pcc/naco/SCA_PccNormalization_Final_revised.pdf
+    #
+    # Note that unlike a strict reading of the NACO normalization rules,
+    # output is returned as lowercase instead of uppercase for compatibility
+    # with previous versions of the Evergreen naco_normalize routine.
+
+    # Convert to upper-case first; even though final output will be lowercase, doing this will
+    # ensure that the German eszett (ß) and certain ligatures (ff, fi, ffl, etc.) will be handled correctly.
+    # If there are any bugs in Perl's implementation of upcasing, they will be passed through here.
+    $str = uc $str;
+
+    # remove non-filing strings
+    $str =~ s/\x{0098}.*?\x{009C}//g;
+
+    $str = NFKD($str);
+
+    # additional substitutions - 3.6.
+    $str =~ s/\x{00C6}/AE/g;
+    $str =~ s/\x{00DE}/TH/g;
+    $str =~ s/\x{0152}/OE/g;
+    $str =~ tr/\x{0110}\x{00D0}\x{00D8}\x{0141}\x{2113}\x{02BB}\x{02BC}][/DDOLl/d;
+
+    # transformations based on Unicode category codes
+    $str =~ s/[\p{Cc}\p{Cf}\p{Co}\p{Cs}\p{Lm}\p{Mc}\p{Me}\p{Mn}]//g;
+
+       if ($sf && $sf =~ /^a/o) {
+               my $commapos = index($str, ',');
+               if ($commapos > -1) {
+                       if ($commapos != length($str) - 1) {
+                $str =~ s/,/\x07/; # preserve first comma
+                       }
+               }
+       }
+
+    # since we've stripped out the control characters, we can now
+    # use a few as placeholders temporarily
+    $str =~ tr/+&@\x{266D}\x{266F}#/\x01\x02\x03\x04\x05\x06/;
+    $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;
+    $str =~ tr/\x01\x02\x03\x04\x05\x06\x07/+&@\x{266D}\x{266F}#,/;
+
+    # decimal digits
+    $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/;
+
+    # intentionally skipping step 8 of the NACO algorithm; if the string
+    # gets normalized away, that's fine.
+
+    # leading and trailing spaces
+    $str =~ s/\s+/ /g;
+    $str =~ s/^\s+//;
+    $str =~ s/\s+$//g;
+
+    return lc $str;
+$func$ LANGUAGE 'plperlu' STRICT IMMUTABLE;
+
 CREATE OR REPLACE FUNCTION public.naco_normalize_keep_comma( TEXT ) RETURNS TEXT AS $func$
         SELECT public.naco_normalize($1,'a');
 $func$ LANGUAGE SQL STRICT IMMUTABLE;
@@ -707,5 +773,14 @@ CREATE OR REPLACE FUNCTION public.naco_normalize( TEXT ) RETURNS TEXT AS $func$
        SELECT public.naco_normalize($1,'');
 $func$ LANGUAGE 'sql' STRICT IMMUTABLE;
 
+CREATE OR REPLACE FUNCTION public.search_normalize_keep_comma( TEXT ) RETURNS TEXT AS $func$
+        SELECT public.search_normalize($1,'a');
+$func$ LANGUAGE SQL STRICT IMMUTABLE;
+
+CREATE OR REPLACE FUNCTION public.search_normalize( TEXT ) RETURNS TEXT AS $func$
+       SELECT public.search_normalize($1,'');
+$func$ LANGUAGE 'sql' STRICT IMMUTABLE;
+
+
 COMMIT;
 
index e032835..7f09424 100644 (file)
@@ -7170,6 +7170,13 @@ INSERT INTO config.index_normalizer (name, description, func, param_count) VALUE
     1
 );
 
+INSERT INTO config.index_normalizer (name, description, func, param_count) VALUES (
+       'Search Normalize',
+       'Apply search normalization rules to the extracted text. A less extreme version of NACO normalization.',
+       'search_normalize',
+       0
+);
+
 -- make use of the index normalizers
 
 INSERT INTO config.metabib_field_index_norm_map (field,norm)
@@ -7177,7 +7184,7 @@ INSERT INTO config.metabib_field_index_norm_map (field,norm)
             i.id
       FROM  config.metabib_field m,
         config.index_normalizer i
-      WHERE i.func IN ('naco_normalize','split_date_range')
+      WHERE i.func IN ('search_normalize','split_date_range')
             AND m.id NOT IN (18, 19);
 
 INSERT INTO config.metabib_field_index_norm_map (field,norm,pos)