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