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