somehow forgot to add the billing_type field
[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
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Utils::ModsParser;
8 use OpenSRF::Utils::SettingsClient;
9 use OpenSRF::Utils::Cache;
10
11
12 #use OpenILS::Application::Search::StaffClient;
13 use OpenILS::Application::Search::Biblio;
14 use OpenILS::Application::Search::Authority;
15 use OpenILS::Application::Search::Actor;
16 use OpenILS::Application::Search::Z3950;
17 use OpenILS::Application::Search::AddedContent;
18
19 use OpenILS::Application::AppUtils;
20
21 use Time::HiRes qw(time);
22 use OpenSRF::EX qw(:try);
23
24 use Text::Aspell; # spell checking...
25
26
27 # Houses generic search utilites 
28
29 sub initialize {
30         OpenILS::Application::Search::Z3950->initialize();
31         OpenILS::Application::Search::AddedContent->initialize();
32 }
33
34 sub filter_search {
35         my($self, $str, $full) = @_;
36
37         my $string = $str;      
38
39         $string =~ s/\s+the\s+/ /oi;
40         $string =~ s/\s+an\s+/ /oi;
41         $string =~ s/\s+a\s+/ /oi;
42
43         $string =~ s/^the\s+//io;
44         $string =~ s/^an\s+//io;
45         $string =~ s/^a\s+//io;
46
47         $string =~ s/\s+the$//io;
48         $string =~ s/\s+an$//io;
49         $string =~ s/\s+a$//io;
50
51         $string =~ s/^the$//io;
52         $string =~ s/^an$//io;
53         $string =~ s/^a$//io;
54
55
56         if(!$full) {
57                 if($string =~ /^\s*$/o) {
58                         return "";
59                 } else {
60                         return $str;
61                 }
62         }
63
64         my @words = qw/ 
65         fiction
66         bibliograph
67         juvenil    
68         histor   
69         literatur
70         biograph
71         stor    
72         american 
73         videorecord
74         count  
75         film   
76         life  
77         book 
78         children 
79         centur 
80         war    
81         genealog
82         etc    
83         state
84         unit
85         /;
86
87         push @words, "united state";
88
89         for my $word (@words) {
90                 if($string =~ /^\s*"?\s*$word\w*\s*"?\s*$/i) {
91                         return "";
92                 }
93         }
94
95         warn "Cleansed string to: $string\n";
96         if($string =~ /^\s*$/o) {
97                 return "";
98         } else {
99                 return $str;
100         }
101         
102         return $string;
103 }       
104
105
106 __PACKAGE__->register_method(
107         method  => "check_spelling",
108         api_name        => "open-ils.search.spell_check");
109
110 sub check_spelling {
111         my( $self, $client, $phrase ) = @_;
112
113         my @resp_objects = ();
114         my $speller = Text::Aspell->new();
115         $speller->set_option('lang', 'en_US');
116         my $return_something = 0;
117
118         my $return_phrase = "";
119
120         for my $word (split(' ',$phrase) ) {
121                 if( ! $speller->check($word) ) {
122                         if( $speller->suggest($word) ) { $return_something = 1; }
123 #                       $return_something = 1;
124                         my $word_stuff = {};
125                         $word_stuff->{'word'} = $word;
126                         $word_stuff->{'suggestions'} = [ $speller->suggest( $word ) ];
127                         if( ! $return_phrase ) { $return_phrase = ($speller->suggest($word))[0]; }
128                         else { $return_phrase .= " " . ($speller->suggest($word))[0];}
129                         
130                 } else { 
131                         if( ! $return_phrase ) { $return_phrase = $word; }
132                         else { $return_phrase .= " $word"; }
133                 }
134         }
135
136         if( $return_something ) { return $return_phrase; }
137         return 0;
138
139 }
140
141 1;