]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search.pm
moved ->use to eval "use ..."
[working/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
42         eval "use $implementation";
43
44         if($@) {        
45                 $logger->error("Unable to load Added Content handler [$implementation]: $@"); 
46                 return; 
47         }
48
49         eval { $implementation->initialize(); };
50 }
51
52 sub filter_search {
53         my($self, $str, $full) = @_;
54
55         my $string = $str;      
56
57         $string =~ s/\s+the\s+/ /oi;
58         $string =~ s/\s+an\s+/ /oi;
59         $string =~ s/\s+a\s+/ /oi;
60
61         $string =~ s/^the\s+//io;
62         $string =~ s/^an\s+//io;
63         $string =~ s/^a\s+//io;
64
65         $string =~ s/\s+the$//io;
66         $string =~ s/\s+an$//io;
67         $string =~ s/\s+a$//io;
68
69         $string =~ s/^the$//io;
70         $string =~ s/^an$//io;
71         $string =~ s/^a$//io;
72
73
74         if(!$full) {
75                 if($string =~ /^\s*$/o) {
76                         return "";
77                 } else {
78                         return $str;
79                 }
80         }
81
82         my @words = qw/ 
83         fiction
84         bibliograph
85         juvenil    
86         histor   
87         literatur
88         biograph
89         stor    
90         american 
91         videorecord
92         count  
93         film   
94         life  
95         book 
96         children 
97         centur 
98         war    
99         genealog
100         etc    
101         state
102         unit
103         /;
104
105         push @words, "united state";
106
107         for my $word (@words) {
108                 if($string =~ /^\s*"?\s*$word\w*\s*"?\s*$/i) {
109                         return "";
110                 }
111         }
112
113         warn "Cleansed string to: $string\n";
114         if($string =~ /^\s*$/o) {
115                 return "";
116         } else {
117                 return $str;
118         }
119         
120         return $string;
121 }       
122
123
124 __PACKAGE__->register_method(
125         method  => "check_spelling",
126         api_name        => "open-ils.search.spell_check");
127
128 sub check_spelling {
129         my( $self, $client, $phrase ) = @_;
130
131         my @resp_objects = ();
132         my $speller = Text::Aspell->new();
133         $speller->set_option('lang', 'en_US');
134         my $return_something = 0;
135
136         my $return_phrase = "";
137
138         for my $word (split(' ',$phrase) ) {
139                 if( ! $speller->check($word) ) {
140                         if( $speller->suggest($word) ) { $return_something = 1; }
141 #                       $return_something = 1;
142                         my $word_stuff = {};
143                         $word_stuff->{'word'} = $word;
144                         $word_stuff->{'suggestions'} = [ $speller->suggest( $word ) ];
145                         if( ! $return_phrase ) { $return_phrase = ($speller->suggest($word))[0]; }
146                         else { $return_phrase .= " " . ($speller->suggest($word))[0];}
147                         
148                 } else { 
149                         if( ! $return_phrase ) { $return_phrase = $word; }
150                         else { $return_phrase .= " $word"; }
151                 }
152         }
153
154         if( $return_something ) { return $return_phrase; }
155         return 0;
156
157 }
158
159 1;