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 |
#look for matching authorities |
47 |
my $authorities = $heading->authorities(1,9999999); # $skipmetadata = true |
48 |
if ($#{$authorities} >= 0) { |
49 |
my %authcodes = ( |
50 |
'PERSO_NAME','100', |
51 |
'CORPO_NAME', '110', |
52 |
'MEETI_NAME', '111', |
53 |
'UNIF_TITLE', '130', |
54 |
'CHRON_TERM', '148', |
55 |
'TOPIC_TERM', '150', |
56 |
'GEOGR_NAME', '151', |
57 |
'GENRE/FORM', '155', |
58 |
'GEN_SUBDIV', '180', |
59 |
'GEO_SUBDIV', '181', |
60 |
'CHRON_SUBD', '182', |
61 |
'FORM_SUBD', '185' |
62 |
); |
63 |
my $fieldasstring = lc $heading->field()->as_string('abcdefghijklmnopqrstuvwxyz'); |
64 |
# Remove subdivisions - they mess up our search here |
65 |
$fieldasstring =~ s/(generalsubdiv|formsubdiv|chronologicalsubdiv|geographicalsubdiv|\s|,|-|;)//g; |
66 |
$fieldasstring =~ s/&/and/g; # Normalise to and |
67 |
for (my $n = 0; $n <= $#{$authorities}; $n = $n + 1) { |
68 |
my $auth = GetAuthority($authorities->[$n]->{'authid'}); |
69 |
my $auth_field = $auth->field($authcodes{$heading->{'auth_type'}}); |
70 |
my $authfieldstring = lc $auth->field($authcodes{$heading->{'auth_type'}})->as_string('abcdefghijklmnopqrstuvwxyz'); |
71 |
$authfieldstring =~ s/(generalsubdiv|formsubdiv|chronologicalsubdiv|geographicalsubdiv|\s|,|-|;)//g; |
72 |
$authfieldstring =~ s/&/and/g; |
73 |
if ($fieldasstring eq $authfieldstring) { |
74 |
$authid = $authorities->[$n]->{'authid'}; |
75 |
$n = $#{$authorities} + 1; # break out of the loop |
76 |
} |
77 |
} |
78 |
if (! defined $authid) { |
79 |
warn "No authority could be perfectly matched for " . $heading->field()->as_string('abcdefghijklmnopqrstuvwxyz') . "\n"; |
80 |
$fieldasstring =~ s/(\d|\.)//g; # Try alos removing full-stops and numbers |
81 |
for (my $n = 0; $n <= $#{$authorities}; $n = $n + 1) { |
82 |
my $auth = GetAuthority($authorities->[$n]->{'authid'}); |
83 |
my $auth_field = $auth->field($authcodes{$heading->{'auth_type'}}); |
84 |
my $authfieldstring = lc $auth->field($authcodes{$heading->{'auth_type'}})->as_string('abcdefghijklmnopqrstuvwxyz'); |
85 |
$authfieldstring =~ s/(generalsubdiv|formsubdiv|chronologicalsubdiv|geographicalsubdiv|\s|\d|,|-|;|\.)//g; |
86 |
if ($fieldasstring eq $authfieldstring) { |
87 |
warn "Fuzzy matching $fieldasstring with $authfieldstring\n"; |
88 |
$authid = $authorities->[$n]->{'authid'}; |
89 |
$fuzzy = 1; |
90 |
$n = $#{$authorities} + 1; # break out of the loop |
91 |
} |
92 |
} |
93 |
} |
94 |
if (! defined $authid) { |
95 |
warn "No match found\n"; |
96 |
} |
97 |
} else { |
98 |
warn "No match found\n"; |
99 |
} |
100 |
|
101 |
$self->{'cache'}->{$search_form}->{'authid'} = $authid; |
102 |
$self->{'cache'}->{$search_form}->{'fuzzy'} = $fuzzy; |
103 |
|
104 |
return $self->SUPER::_handle_auth_limit($authid), $fuzzy; |
105 |
} |
106 |
|
107 |
sub update_cache { |
108 |
my $self = shift; |
109 |
my $heading = shift; |
110 |
my $authid = shift; |
111 |
$self->{'default_linker'}->update_cache( $heading, $authid ); |
112 |
} |
113 |
|
114 |
sub flip_heading { |
115 |
my $self = shift; |
116 |
my $heading = shift; |
117 |
|
118 |
return $self->{'default_linker'}->flip($heading); |
119 |
} |
120 |
|
121 |
1; |
122 |
__END__ |
123 |
|
124 |
=head1 NAME |
125 |
|
126 |
C4::Linker::BestMatch - match against the authority record that is identical or near identical (fuzzy) |
127 |
|
128 |
=cut |