|
Line 0
Link Here
|
|
|
1 |
package C4::Linker::BestMatch; |
| 2 |
|
| 3 |
# Copyright 2011 C & P Bibliography Services |
| 4 |
# |
| 5 |
# This file is part of Koha. |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use strict; |
| 21 |
use warnings; |
| 22 |
use Carp; |
| 23 |
use C4::Heading; |
| 24 |
use C4::AuthoritiesMarc; |
| 25 |
use C4::Linker::Default; # Use Default for flipping |
| 26 |
|
| 27 |
use base qw(C4::Linker); |
| 28 |
|
| 29 |
sub new { |
| 30 |
my $class = shift; |
| 31 |
my $param = shift; |
| 32 |
|
| 33 |
my $self = $class->SUPER::new($param); |
| 34 |
$self->{'default_linker'} = C4::Linker::Default->new($param); |
| 35 |
bless $self, $class; |
| 36 |
return $self; |
| 37 |
} |
| 38 |
|
| 39 |
sub get_link { |
| 40 |
my $self = shift; |
| 41 |
my $heading = shift; |
| 42 |
my $search_form = $heading->search_form(); |
| 43 |
my $authid; |
| 44 |
my $fuzzy = 0; |
| 45 |
|
| 46 |
my $fieldasstring = lc $heading->field()->as_string('abcdefghijklmnopqrstuvwxyz'); |
| 47 |
# Remove subdivisions - they mess up our search here |
| 48 |
$fieldasstring =~ s/(generalsubdiv|formsubdiv|chronologicalsubdiv|geographicalsubdiv|\s|,|-|;)//g; |
| 49 |
$fieldasstring =~ s/&/and/g; # Normalise to and |
| 50 |
|
| 51 |
if ( $self->{'cache'}->{$fieldasstring}->{'cached'} ) { |
| 52 |
$authid = $self->{'cache'}->{$fieldasstring}->{'authid'}; |
| 53 |
$fuzzy = $self->{'cache'}->{$fieldasstring}->{'fuzzy'}; |
| 54 |
#warn "Cached result for $fieldasstring as $authid (Fuzzy: $fuzzy)"; |
| 55 |
} else { |
| 56 |
#look for matching authorities |
| 57 |
my $authorities = $heading->authorities(1,9999999); # $skipmetadata = true |
| 58 |
if ($#{$authorities} >= 0) { |
| 59 |
my %authcodes = ( |
| 60 |
'PERSO_NAME','100', |
| 61 |
'CORPO_NAME', '110', |
| 62 |
'MEETI_NAME', '111', |
| 63 |
'UNIF_TITLE', '130', |
| 64 |
'CHRON_TERM', '148', |
| 65 |
'TOPIC_TERM', '150', |
| 66 |
'GEOGR_NAME', '151', |
| 67 |
'GENRE/FORM', '155', |
| 68 |
'GEN_SUBDIV', '180', |
| 69 |
'GEO_SUBDIV', '181', |
| 70 |
'CHRON_SUBD', '182', |
| 71 |
'FORM_SUBD', '185' |
| 72 |
); |
| 73 |
for (my $n = 0; $n <= $#{$authorities}; $n = $n + 1) { |
| 74 |
my $auth = GetAuthority($authorities->[$n]->{'authid'}); |
| 75 |
my $auth_field = $auth->field($authcodes{$heading->{'auth_type'}}); |
| 76 |
my $authfieldstring = lc $auth->field($authcodes{$heading->{'auth_type'}})->as_string('abcdefghijklmnopqrstuvwxyz'); |
| 77 |
$authfieldstring =~ s/(generalsubdiv|formsubdiv|chronologicalsubdiv|geographicalsubdiv|\s|,|-|;)//g; |
| 78 |
$authfieldstring =~ s/&/and/g; |
| 79 |
if ($fieldasstring eq $authfieldstring) { |
| 80 |
$authid = $authorities->[$n]->{'authid'}; |
| 81 |
$n = $#{$authorities} + 1; # break out of the loop |
| 82 |
} |
| 83 |
} |
| 84 |
if (! defined $authid) { |
| 85 |
warn "No authority could be perfectly matched for " . $heading->field()->as_string('abcdefghijklmnopqrstuvwxyz') . "\n"; |
| 86 |
$fieldasstring =~ s/(\d|\.)//g; # Try alos removing full-stops and numbers |
| 87 |
for (my $n = 0; $n <= $#{$authorities}; $n = $n + 1) { |
| 88 |
my $auth = GetAuthority($authorities->[$n]->{'authid'}); |
| 89 |
my $auth_field = $auth->field($authcodes{$heading->{'auth_type'}}); |
| 90 |
my $authfieldstring = lc $auth->field($authcodes{$heading->{'auth_type'}})->as_string('abcdefghijklmnopqrstuvwxyz'); |
| 91 |
$authfieldstring =~ s/(generalsubdiv|formsubdiv|chronologicalsubdiv|geographicalsubdiv|\s|\d|,|-|;|\.)//g; |
| 92 |
if ($fieldasstring eq $authfieldstring) { |
| 93 |
warn "Fuzzy matching $fieldasstring with $authfieldstring\n"; |
| 94 |
$authid = $authorities->[$n]->{'authid'}; |
| 95 |
$fuzzy = 1; |
| 96 |
$n = $#{$authorities} + 1; # break out of the loop |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
if (! defined $authid) { |
| 101 |
warn "No match found\n"; |
| 102 |
} |
| 103 |
} else { |
| 104 |
warn "No match found\n"; |
| 105 |
} |
| 106 |
$self->{'cache'}->{$fieldasstring}->{'cached'} = 1; |
| 107 |
$self->{'cache'}->{$fieldasstring}->{'authid'} = $authid; |
| 108 |
$self->{'cache'}->{$fieldasstring}->{'fuzzy'} = $fuzzy; |
| 109 |
} |
| 110 |
|
| 111 |
return $self->SUPER::_handle_auth_limit($authid), $fuzzy; |
| 112 |
} |
| 113 |
|
| 114 |
sub update_cache { |
| 115 |
my $self = shift; |
| 116 |
my $heading = shift; |
| 117 |
my $authid = shift; |
| 118 |
$self->{'default_linker'}->update_cache( $heading, $authid ); |
| 119 |
} |
| 120 |
|
| 121 |
sub flip_heading { |
| 122 |
my $self = shift; |
| 123 |
my $heading = shift; |
| 124 |
|
| 125 |
return $self->{'default_linker'}->flip($heading); |
| 126 |
} |
| 127 |
|
| 128 |
1; |
| 129 |
__END__ |
| 130 |
|
| 131 |
=head1 NAME |
| 132 |
|
| 133 |
C4::Linker::BestMatch - match against the authority record that is identical or near identical (fuzzy) |
| 134 |
|
| 135 |
=cut |