Bug 14457: Integrate LIBRIS spellchecking
[koha.git] / Koha / SuggestionEngine / Plugin / LibrisSpellcheck.pm
1 package Koha::SuggestionEngine::Plugin::LibrisSpellcheck;
2 # Copyright (C) 2015 Eivin Giske Skaaren
3 #
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20 use LWP::UserAgent;
21 use XML::Simple qw(XMLin);
22 use C4::Context;
23 use base qw(Koha::SuggestionEngine::Base);
24
25 sub NAME {
26     return 'LibrisSpellcheck';
27 }
28
29 sub get_suggestions {
30     my ($self, $query) = @_;
31     my $key = C4::Context->preference('LibrisKey');
32
33     my $search = $query->{'search'};
34     my $response = LWP::UserAgent->new->get("http://api.libris.kb.se/bibspell/spell?query={$search}&key=$key");
35     my $xml = XMLin($response->content, NoAttr => 1, ForceArray => qr/term/);
36
37     my @terms;
38     my $label;
39
40     if ($xml->{suggestion}->{term}) {
41         for (@{$xml->{suggestion}->{term}}) {
42             push @terms, $_;
43         }
44         $label = join(' ', @terms);
45     } else {
46         return; # No result from LIBRIS
47     }
48
49     my @results;
50     push @results,
51     {
52         'search'  => $label,  #$thissearch,
53         relevance => 100,
54             # FIXME: it'd be nice to have some empirical measure of
55             #        "relevance" in this case, but we don't.
56         label => $label
57     };
58     return \@results;
59 }
60
61 1;
62 __END__
63
64 =head1 NAME
65
66 Koha::SuggestionEngine::Plugin::LibrisSpellcheck
67
68 =head2 FUNCTIONS
69
70 This module provides facilities for using the LIBRIS spell checker API
71
72 =over
73
74 =item get_suggestions(query)
75
76 Sends in the search query and gets an XML with a suggestion
77
78 =back
79
80 =cut
81
82 =head1 NOTES
83
84 =cut
85
86 =head1 AUTHOR
87
88 Eivin Giske Skaaren <eskaaren@yahoo.no>
89
90 =cut