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