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