]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Search.pm
lp1863252 toward geosort
[Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Search.pm
1 package OpenILS::Application::Search;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
4 use strict; use warnings;
5 use OpenSRF::Utils::JSON;
6 use OpenSRF::Utils::Logger qw(:logger);
7
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Utils::ModsParser;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenSRF::Utils::Cache;
12
13 use OpenILS::Application::Search::Biblio;
14 use OpenILS::Application::Search::Authority;
15 use OpenILS::Application::Search::Z3950;
16 use OpenILS::Application::Search::Zips;
17 use OpenILS::Application::Search::CNBrowse;
18 use OpenILS::Application::Search::Serial;
19 use OpenILS::Application::Search::Browse;
20
21
22 use OpenILS::Application::AppUtils;
23
24 use Time::HiRes qw(time);
25 use OpenSRF::EX qw(:try);
26
27 use Text::Aspell; 
28
29 # Houses generic search utilites 
30
31 sub initialize {
32     OpenILS::Application::Search::Zips->initialize();
33     OpenILS::Application::Search::Biblio->initialize();
34 }
35
36 sub child_init {
37     OpenILS::Application::Search::Z3950->child_init;
38     OpenILS::Application::Search::Browse->child_init;
39 }
40     
41
42
43 # ------------------------------------------------------------------
44 # Create custom dictionaries like so:
45 # aspell --lang=en create  master ./oils_authority.dict < /tmp/words
46 # where /tmp/words is a space separated list of words
47 # ------------------------------------------------------------------
48
49 __PACKAGE__->register_method(
50     method    => "spellcheck",
51     api_name  => "open-ils.search.spellcheck",
52     signature => {
53         desc  => 'Returns alternate spelling suggestions',
54         param => [
55             {
56                 name => 'phrase',
57                 desc => 'Word or phrase to return alternate spelling suggestions for',
58                 type => 'string'
59             },
60             {
61                 name => 'Dictionary class',
62                 desc => 'Alternate configured dictionary to use (optional)',
63                 type => 'string'
64             },
65         ],
66         return => {
67             desc => 'Array with a suggestions hash for each word in the phrase, like: '
68                   . q# [{ word: original_word, suggestions: [sug1, sug2, ...], found: 1 }, ... ] #
69                   . 'The "found" value will be 1 if the word was found in the dictionary, 0 otherwise.',
70             type => 'array',
71         }
72     }
73 );
74
75 my $speller = Text::Aspell->new();
76
77 sub spellcheck {
78     my( $self, $client, $phrase, $class ) = @_;
79
80     return [] unless $phrase;   # nothing to check, abort.
81
82     my $conf = OpenSRF::Utils::SettingsClient->new;
83     $class ||= 'default';
84
85     my @conf_path = (apps => 'open-ils.search' => app_settings => spelling_dictionary => $class);
86
87     if( my $dict = $conf->config_value(@conf_path) ) {
88         $speller->set_option('master', $dict);
89         $logger->debug("spelling dictionary set to $dict");
90     }
91
92     $speller->set_option('ignore-case', 'true');
93
94     my @resp;
95
96     for my $word (split(/\s+/,$phrase) ) {
97
98         my @suggestions = $speller->suggest($word);
99         my @trimmed;
100
101         for my $sug (@suggestions) {
102
103             # suggestion matches alternate case of original word
104             next if lc($sug) eq lc($word); 
105
106             # suggestion matches alternate case of already suggested word
107             next if grep { lc($sug) eq lc($_) } @trimmed;
108
109             push(@trimmed, $sug);
110         }
111
112         push( @resp, 
113             {
114                 word => $word, 
115                 suggestions => (@trimmed) ? [@trimmed] : undef,
116                 found => $speller->check($word)
117             } 
118         ); 
119     }
120     return \@resp;
121 }
122
123
124
125 1;