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);
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Utils::ModsParser;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenSRF::Utils::Cache;
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;
21 use OpenILS::Application::AppUtils;
23 use Time::HiRes qw(time);
24 use OpenSRF::EX qw(:try);
28 # Houses generic search utilites
31 OpenILS::Application::Search::Z3950->initialize();
32 OpenILS::Application::Search::Zips->initialize();
33 OpenILS::Application::Search::Biblio->initialize();
37 OpenILS::Application::Search::Z3950->child_init;
42 # ------------------------------------------------------------------
43 # Create custom dictionaries like so:
44 # aspell --lang=en create master ./oils_authority.dict < /tmp/words
45 # where /tmp/words is a space separated list of words
46 # ------------------------------------------------------------------
48 __PACKAGE__->register_method(
49 method => "spellcheck",
50 api_name => "open-ils.search.spellcheck",
52 desc => 'Returns alternate spelling suggestions',
56 desc => 'Word or phrase to return alternate spelling suggestions for',
60 name => 'Dictionary class',
61 desc => 'Alternate configured dictionary to use (optional)',
66 desc => 'Array with a suggestions hash for each word in the phrase, like: '
67 . q# [{ word: original_word, suggestions: [sug1, sug2, ...], found: 1 }, ... ] #
68 . 'The "found" value will be 1 if the word was found in the dictionary, 0 otherwise.',
74 my $speller = Text::Aspell->new();
77 my( $self, $client, $phrase, $class ) = @_;
79 return [] unless $phrase; # nothing to check, abort.
81 my $conf = OpenSRF::Utils::SettingsClient->new;
84 my @conf_path = (apps => 'open-ils.search' => app_settings => spelling_dictionary => $class);
86 if( my $dict = $conf->config_value(@conf_path) ) {
87 $speller->set_option('master', $dict);
88 $logger->debug("spelling dictionary set to $dict");
93 for my $word (split(/\s+/,$phrase) ) {
95 my @suggestions = $speller->suggest($word);
98 for my $sug (@suggestions) {
100 # suggestion matches alternate case of original word
101 next if lc($sug) eq lc($word);
103 # suggestion matches alternate case of already suggested word
104 next if grep { lc($sug) eq lc($_) } @trimmed;
106 push(@trimmed, $sug);
112 suggestions => (@trimmed) ? [@trimmed] : undef,
113 found => $speller->check($word)