]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Search.pm
added custom aspell dictionary support
[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 # Houses generic search utilites 
29
30 sub initialize {
31         OpenILS::Application::Search::Z3950->initialize();
32         OpenILS::Application::Search::Zips->initialize();
33
34         # try to load the added content handler
35         my $conf = OpenSRF::Utils::SettingsClient->new;
36         my $implementation = $conf->config_value(                                       
37                 "apps", "open-ils.search","app_settings", "added_content", "implementation" );
38
39         $implementation = "OpenILS::Application::Search::AddedContent" unless $implementation;
40
41         $logger->debug("Attempting to load Added Content handler: $implementation");
42
43         eval "use $implementation";
44
45         if($@) {        
46                 $logger->error("Unable to load Added Content handler [$implementation]: $@"); 
47                 return; 
48         }
49
50         eval { $implementation->initialize(); };
51 }
52         
53
54
55 __PACKAGE__->register_method(
56         method  => "check_spelling",
57         api_name        => "open-ils.search.spell_check");
58
59 sub check_spelling {
60         my( $self, $client, $phrase ) = @_;
61
62         my @resp_objects = ();
63         my $speller = Text::Aspell->new();
64         $speller->set_option('lang', 'en_US');
65         my $return_something = 0;
66
67         my $return_phrase = "";
68
69         for my $word (split(' ',$phrase) ) {
70                 if( ! $speller->check($word) ) {
71                         if( $speller->suggest($word) ) { $return_something = 1; }
72                         my $word_stuff = {};
73                         $word_stuff->{'word'} = $word;
74                         $word_stuff->{'suggestions'} = [ $speller->suggest( $word ) ];
75                         if( ! $return_phrase ) { $return_phrase = ($speller->suggest($word))[0]; }
76                         else { $return_phrase .= " " . ($speller->suggest($word))[0];}
77                         
78                 } else { 
79                         if( ! $return_phrase ) { $return_phrase = $word; }
80                         else { $return_phrase .= " $word"; }
81                 }
82         }
83
84         if( $return_something ) { return $return_phrase; }
85         return 0;
86
87 }
88
89
90
91 __PACKAGE__->register_method(
92         method  => "spellcheck",
93         api_name        => "open-ils.search.spellcheck");
94
95 my $speller = Text::Aspell->new();
96
97 sub spellcheck {
98         my( $self, $client, $phrase ) = @_;
99
100         my $conf = OpenSRF::Utils::SettingsClient->new;
101
102         if( my $dict = $conf->config_value(
103                         "apps", "open-ils.search", "app_settings", "spelling_dictionary")) {
104                 $speller->set_option('master', $dict);
105                 $logger->debug("spelling dictionary set to $dict");
106         }
107
108         my @resp;
109         return \@resp unless $phrase;
110         for my $word (split(/\s+/,$phrase) ) {
111                 push( @resp, 
112                         {
113                                 word => $word, 
114                                 suggestions => ($speller->check($word)) ? undef : [$speller->suggest($word)]
115                         } 
116                 ); 
117         }
118         return \@resp;
119 }
120
121
122
123 1;