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