]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search.pm
bug fixes after testing
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Search.pm
1 package OpenILS::Application::Search;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
4 use JSON;
5 use OpenSRF::Utils::Logger qw(:logger);
6
7 use OpenILS::Utils::Fieldmapper;
8 use OpenILS::Utils::ModsParser;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenSRF::Utils::Cache;
11
12
13 #use OpenILS::Application::Search::StaffClient;
14 use OpenILS::Application::Search::Biblio;
15 use OpenILS::Application::Search::Authority;
16 #use OpenILS::Application::Search::Actor;
17 use OpenILS::Application::Search::Z3950;
18
19
20 use OpenILS::Application::AppUtils;
21
22 use Time::HiRes qw(time);
23 use OpenSRF::EX qw(:try);
24
25 use Text::Aspell; # spell checking...
26
27
28 # Houses generic search utilites 
29
30 sub initialize {
31         OpenILS::Application::Search::Z3950->initialize();
32
33         # try to load the added content handler
34         my $conf = OpenSRF::Utils::SettingsClient->new;
35         my $implementation = $conf->config_value(                                       
36                 "apps", "open-ils.search","app_settings", "added_content", "implementation" );
37
38         $implementation = "OpenILS::Application::Search::AddedContent" unless $implementation;
39
40         $logger->debug("Attempting to load Added Content handler: $implementation");
41         eval "use $implementation";
42         if($@) {        
43                 $logger->error("Unable to load Added Content handler [$implementation]: $@"); 
44                 return; 
45         }
46
47         eval { $implementation->initialize(); };
48 }
49
50 sub filter_search {
51         my($self, $str, $full) = @_;
52
53         my $string = $str;      
54
55         $string =~ s/\s+the\s+/ /oi;
56         $string =~ s/\s+an\s+/ /oi;
57         $string =~ s/\s+a\s+/ /oi;
58
59         $string =~ s/^the\s+//io;
60         $string =~ s/^an\s+//io;
61         $string =~ s/^a\s+//io;
62
63         $string =~ s/\s+the$//io;
64         $string =~ s/\s+an$//io;
65         $string =~ s/\s+a$//io;
66
67         $string =~ s/^the$//io;
68         $string =~ s/^an$//io;
69         $string =~ s/^a$//io;
70
71
72         if(!$full) {
73                 if($string =~ /^\s*$/o) {
74                         return "";
75                 } else {
76                         return $str;
77                 }
78         }
79
80         my @words = qw/ 
81         fiction
82         bibliograph
83         juvenil    
84         histor   
85         literatur
86         biograph
87         stor    
88         american 
89         videorecord
90         count  
91         film   
92         life  
93         book 
94         children 
95         centur 
96         war    
97         genealog
98         etc    
99         state
100         unit
101         /;
102
103         push @words, "united state";
104
105         for my $word (@words) {
106                 if($string =~ /^\s*"?\s*$word\w*\s*"?\s*$/i) {
107                         return "";
108                 }
109         }
110
111         warn "Cleansed string to: $string\n";
112         if($string =~ /^\s*$/o) {
113                 return "";
114         } else {
115                 return $str;
116         }
117         
118         return $string;
119 }       
120
121
122 __PACKAGE__->register_method(
123         method  => "check_spelling",
124         api_name        => "open-ils.search.spell_check");
125
126 sub check_spelling {
127         my( $self, $client, $phrase ) = @_;
128
129         my @resp_objects = ();
130         my $speller = Text::Aspell->new();
131         $speller->set_option('lang', 'en_US');
132         my $return_something = 0;
133
134         my $return_phrase = "";
135
136         for my $word (split(' ',$phrase) ) {
137                 if( ! $speller->check($word) ) {
138                         if( $speller->suggest($word) ) { $return_something = 1; }
139 #                       $return_something = 1;
140                         my $word_stuff = {};
141                         $word_stuff->{'word'} = $word;
142                         $word_stuff->{'suggestions'} = [ $speller->suggest( $word ) ];
143                         if( ! $return_phrase ) { $return_phrase = ($speller->suggest($word))[0]; }
144                         else { $return_phrase .= " " . ($speller->suggest($word))[0];}
145                         
146                 } else { 
147                         if( ! $return_phrase ) { $return_phrase = $word; }
148                         else { $return_phrase .= " $word"; }
149                 }
150         }
151
152         if( $return_something ) { return $return_phrase; }
153         return 0;
154
155 }
156
157 1;