3 # Copyright (C) 2007 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 use vars
qw($VERSION);
31 # set the version for version checking
37 C4::Matcher - find MARC records matching another one
41 my @matchers = C4::Matcher::GetMatcherList();
43 my $matcher = C4::Matcher->new($record_type);
44 $matcher->threshold($threshold);
45 $matcher->code($code);
46 $matcher->description($description);
48 $matcher->add_simple_matchpoint('isbn', 1000, '020', 'a', -1, 0, '');
49 $matcher->add_simple_matchpoint('Date', 1000, '008', '', 7, 4, '');
50 $matcher->add_matchpoint('isbn', 1000, [ { tag => '020', subfields => 'a', norms => [] } ]);
52 $matcher->add_simple_required_check('245', 'a', -1, 0, '', '245', 'a', -1, 0, '');
53 $matcher->add_required_check([ { tag => '245', subfields => 'a', norms => [] } ],
54 [ { tag => '245', subfields => 'a', norms => [] } ]);
56 my @matches = $matcher->get_matches($marc_record, $max_matches);
58 foreach $match (@matches) {
60 # matches already sorted in order of
62 print "record ID: $match->{'record_id'};
63 print "score: $match->{'score'};
67 my $matcher_description = $matcher->dump();
75 my @matchers = C4::Matcher::GetMatcherList();
77 Returns an array of hashrefs list all matchers
78 present in the database. Each hashref includes:
87 my $dbh = C4::Context->dbh;
89 my $sth = $dbh->prepare_cached("SELECT matcher_id, code, description FROM marc_matchers ORDER BY matcher_id");
92 while (my $row = $sth->fetchrow_hashref) {
102 my $matcher = C4::Matcher->new($record_type, $threshold);
104 Creates a new Matcher. C<$record_type> indicates which search
105 database to use, e.g., 'biblio' or 'authority' and defaults to
106 'biblio', while C<$threshold> is the minimum score required for a match
107 and defaults to 1000.
115 $self->{'id'} = undef;
118 $self->{'record_type'} = shift;
120 $self->{'record_type'} = 'biblio';
124 $self->{'threshold'} = shift;
126 $self->{'threshold'} = 1000;
129 $self->{'code'} = '';
130 $self->{'description'} = '';
132 $self->{'matchpoints'} = [];
133 $self->{'required_checks'} = [];
141 my $matcher = C4::Matcher->fetch($id);
143 Creates a matcher object from the version stored
144 in the database. If a matcher with the given
145 id does not exist, returns undef.
152 my $dbh = C4::Context->dbh();
154 my $sth = $dbh->prepare_cached("SELECT * FROM marc_matchers WHERE matcher_id = ?");
156 my $row = $sth->fetchrow_hashref;
158 return undef unless defined $row;
161 $self->{'id'} = $row->{'matcher_id'};
162 $self->{'record_type'} = $row->{'record_type'};
163 $self->{'code'} = $row->{'code'};
164 $self->{'description'} = $row->{'description'};
165 $self->{'threshold'} = int($row->{'threshold'});
169 $self->{'matchpoints'} = [];
170 $sth = $dbh->prepare_cached("SELECT * FROM matcher_matchpoints WHERE matcher_id = ? ORDER BY matchpoint_id");
171 $sth->execute($self->{'id'});
172 while (my $row = $sth->fetchrow_hashref) {
173 my $matchpoint = $self->_fetch_matchpoint($row->{'matchpoint_id'});
174 push @{ $self->{'matchpoints'} }, $matchpoint;
178 $self->{'required_checks'} = [];
179 $sth = $dbh->prepare_cached("SELECT * FROM matchchecks WHERE matcher_id = ? ORDER BY matchcheck_id");
180 $sth->execute($self->{'id'});
181 while (my $row = $sth->fetchrow_hashref) {
182 my $source_matchpoint = $self->_fetch_matchpoint($row->{'source_matchpoint_id'});
183 my $target_matchpoint = $self->_fetch_matchpoint($row->{'target_matchpoint_id'});
185 $matchcheck->{'source_matchpoint'} = $source_matchpoint;
186 $matchcheck->{'target_matchpoint'} = $target_matchpoint;
187 push @{ $self->{'required_checks'} }, $matchcheck;
193 sub _fetch_matchpoint {
195 my $matchpoint_id = shift;
197 my $dbh = C4::Context->dbh;
198 my $sth = $dbh->prepare_cached("SELECT * FROM matchpoints WHERE matchpoint_id = ?");
199 $sth->execute($matchpoint_id);
200 my $row = $sth->fetchrow_hashref;
202 $matchpoint->{'index'} = $row->{'search_index'};
203 $matchpoint->{'score'} = int($row->{'score'});
206 $matchpoint->{'components'} = [];
207 $sth = $dbh->prepare_cached("SELECT * FROM matchpoint_components WHERE matchpoint_id = ? ORDER BY sequence");
208 $sth->execute($matchpoint_id);
209 while ($row = $sth->fetchrow_hashref) {
211 $component->{'tag'} = $row->{'tag'};
212 $component->{'subfields'} = { map { $_ => 1 } split(//, $row->{'subfields'}) };
213 $component->{'offset'} = int($row->{'offset'});
214 $component->{'length'} = int($row->{'length'});
215 $component->{'norms'} = [];
216 my $sth2 = $dbh->prepare_cached("SELECT *
217 FROM matchpoint_component_norms
218 WHERE matchpoint_component_id = ? ORDER BY sequence");
219 $sth2->execute($row->{'matchpoint_component_id'});
220 while (my $row2 = $sth2->fetchrow_hashref) {
221 push @{ $component->{'norms'} }, $row2->{'norm_routine'};
223 push @{ $matchpoint->{'components'} }, $component;
230 my $id = $matcher->store();
232 Stores matcher in database. The return value is the ID
233 of the marc_matchers row. If the matcher was
234 previously retrieved from the database via the fetch()
235 method, the DB representation of the matcher
243 if (defined $self->{'id'}) {
245 $self->_del_matcher_components();
246 $self->_update_marc_matchers();
249 $self->_new_marc_matchers();
251 $self->_store_matcher_components();
252 return $self->{'id'};
255 sub _del_matcher_components {
258 my $dbh = C4::Context->dbh();
259 my $sth = $dbh->prepare_cached("DELETE FROM matchpoints WHERE matcher_id = ?");
260 $sth->execute($self->{'id'});
261 $sth = $dbh->prepare_cached("DELETE FROM matchchecks WHERE matcher_id = ?");
262 $sth->execute($self->{'id'});
263 # foreign key delete cascades take care of deleting relevant rows
264 # from matcher_matchpoints, matchpoint_components, and
265 # matchpoint_component_norms
268 sub _update_marc_matchers {
271 my $dbh = C4::Context->dbh();
272 my $sth = $dbh->prepare_cached("UPDATE marc_matchers
277 WHERE matcher_id = ?");
278 $sth->execute($self->{'code'}, $self->{'description'}, $self->{'record_type'}, $self->{'threshold'}, $self->{'id'});
281 sub _new_marc_matchers {
284 my $dbh = C4::Context->dbh();
285 my $sth = $dbh->prepare_cached("INSERT INTO marc_matchers
286 (code, description, record_type, threshold)
287 VALUES (?, ?, ?, ?)");
288 $sth->execute($self->{'code'}, $self->{'description'}, $self->{'record_type'}, $self->{'threshold'});
289 $self->{'id'} = $dbh->{'mysql_insertid'};
292 sub _store_matcher_components {
295 my $dbh = C4::Context->dbh();
297 my $matcher_id = $self->{'id'};
298 foreach my $matchpoint (@{ $self->{'matchpoints'}}) {
299 my $matchpoint_id = $self->_store_matchpoint($matchpoint);
300 $sth = $dbh->prepare_cached("INSERT INTO matcher_matchpoints (matcher_id, matchpoint_id)
302 $sth->execute($matcher_id, $matchpoint_id);
304 foreach my $matchcheck (@{ $self->{'required_checks'} }) {
305 my $source_matchpoint_id = $self->_store_matchpoint($matchcheck->{'source_matchpoint'});
306 my $target_matchpoint_id = $self->_store_matchpoint($matchcheck->{'target_matchpoint'});
307 $sth = $dbh->prepare_cached("INSERT INTO matchchecks
308 (matcher_id, source_matchpoint_id, target_matchpoint_id)
310 $sth->execute($matcher_id, $source_matchpoint_id, $target_matchpoint_id);
315 sub _store_matchpoint {
317 my $matchpoint = shift;
319 my $dbh = C4::Context->dbh();
321 my $matcher_id = $self->{'id'};
322 $sth = $dbh->prepare_cached("INSERT INTO matchpoints (matcher_id, search_index, score)
324 $sth->execute($matcher_id, $matchpoint->{'index'}, $matchpoint->{'score'});
325 my $matchpoint_id = $dbh->{'mysql_insertid'};
327 foreach my $component (@{ $matchpoint->{'components'} }) {
329 $sth = $dbh->prepare_cached("INSERT INTO matchpoint_components
330 (matchpoint_id, sequence, tag, subfields, offset, length)
331 VALUES (?, ?, ?, ?, ?, ?)");
332 $sth->bind_param(1, $matchpoint_id);
333 $sth->bind_param(2, $seqnum);
334 $sth->bind_param(3, $component->{'tag'});
335 $sth->bind_param(4, join "", sort keys %{ $component->{'subfields'} });
336 $sth->bind_param(5, $component->{'offset'});
337 $sth->bind_param(6, $component->{'length'});
339 my $matchpoint_component_id = $dbh->{'mysql_insertid'};
341 foreach my $norm (@{ $component->{'norms'} }) {
343 $sth = $dbh->prepare_cached("INSERT INTO matchpoint_component_norms
344 (matchpoint_component_id, sequence, norm_routine)
346 $sth->execute($matchpoint_component_id, $normseq, $norm);
349 return $matchpoint_id;
355 C4::Matcher->delete($id);
357 Deletes the matcher of the specified ID
364 my $matcher_id = shift;
366 my $dbh = C4::Context->dbh;
367 my $sth = $dbh->prepare("DELETE FROM marc_matchers WHERE matcher_id = ?");
368 $sth->execute($matcher_id); # relying on cascading deletes to clean up everything
373 $matcher->threshold(1000);
374 my $threshold = $matcher->threshold();
382 @_ ? $self->{'threshold'} = shift : $self->{'threshold'};
388 my $id = $matcher->_id();
390 Accessor method. Note that using this method
391 to set the DB ID of the matcher should not be
392 done outside of the editing CGI.
398 @_ ? $self->{'id'} = shift : $self->{'id'};
403 $matcher->code('ISBN');
404 my $code = $matcher->code();
412 @_ ? $self->{'code'} = shift : $self->{'code'};
417 $matcher->description('match on ISBN');
418 my $description = $matcher->description();
426 @_ ? $self->{'description'} = shift : $self->{'description'};
429 =head2 add_matchpoint
431 $matcher->add_matchpoint($index, $score, $matchcomponents);
433 Adds a matchpoint that may include multiple components. The $index
434 parameter identifies the index that will be searched, while $score
435 is the weight that will be added if a match is found.
437 $matchcomponents should be a reference to an array of matchpoint
438 compoents, each of which should be a hash containing the following
446 The normalization_rules value should in turn be a reference to an
447 array, each element of which should be a reference to a
448 normalization subroutine (under C4::Normalize) to be applied
449 to the source string.
455 my ($index, $score, $matchcomponents) = @_;
458 $matchpoint->{'index'} = $index;
459 $matchpoint->{'score'} = $score;
460 $matchpoint->{'components'} = [];
461 foreach my $input_component (@{ $matchcomponents }) {
462 push @{ $matchpoint->{'components'} }, _parse_match_component($input_component);
464 push @{ $self->{'matchpoints'} }, $matchpoint;
467 =head2 add_simple_matchpoint
469 $matcher->add_simple_matchpoint($index, $score, $source_tag,
470 $source_subfields, $source_offset,
471 $source_length, $source_normalizer);
474 Adds a simple matchpoint rule -- after composing a key based on the source tag and subfields,
475 normalized per the normalization fuction, search the index. All records retrieved
476 will receive the assigned score.
480 sub add_simple_matchpoint {
482 my ($index, $score, $source_tag, $source_subfields, $source_offset, $source_length, $source_normalizer) = @_;
484 $self->add_matchpoint($index, $score, [
485 { tag => $source_tag, subfields => $source_subfields,
486 offset => $source_offset, 'length' => $source_length,
487 norms => [ $source_normalizer ]
492 =head2 add_required_check
494 $match->add_required_check($source_matchpoint, $target_matchpoint);
496 Adds a required check definition. A required check means that in
497 order for a match to be considered valid, the key derived from the
498 source (incoming) record must match the key derived from the target
499 (already in DB) record.
501 Unlike a regular matchpoint, only the first repeat of each tag
502 in the source and target match criteria are considered.
504 A typical example of a required check would be verifying that the
505 titles and publication dates match.
507 $source_matchpoint and $target_matchpoint are each a reference to
508 an array of hashes, where each hash follows the same definition
509 as the matchpoint component specification in add_matchpoint, i.e.,
517 The normalization_rules value should in turn be a reference to an
518 array, each element of which should be a reference to a
519 normalization subroutine (under C4::Normalize) to be applied
520 to the source string.
524 sub add_required_check {
526 my ($source_matchpoint, $target_matchpoint) = @_;
529 $matchcheck->{'source_matchpoint'}->{'index'} = '';
530 $matchcheck->{'source_matchpoint'}->{'score'} = 0;
531 $matchcheck->{'source_matchpoint'}->{'components'} = [];
532 $matchcheck->{'target_matchpoint'}->{'index'} = '';
533 $matchcheck->{'target_matchpoint'}->{'score'} = 0;
534 $matchcheck->{'target_matchpoint'}->{'components'} = [];
535 foreach my $input_component (@{ $source_matchpoint }) {
536 push @{ $matchcheck->{'source_matchpoint'}->{'components'} }, _parse_match_component($input_component);
538 foreach my $input_component (@{ $target_matchpoint }) {
539 push @{ $matchcheck->{'target_matchpoint'}->{'components'} }, _parse_match_component($input_component);
541 push @{ $self->{'required_checks'} }, $matchcheck;
544 =head2 add_simple_required_check
546 $matcher->add_simple_required_check($source_tag, $source_subfields,
547 $source_offset, $source_length, $source_normalizer,
548 $target_tag, $target_subfields, $target_offset,
549 $target_length, $target_normalizer);
551 Adds a required check, which requires that the normalized keys made from the source and targets
552 must match for a match to be considered valid.
556 sub add_simple_required_check {
558 my ($source_tag, $source_subfields, $source_offset, $source_length, $source_normalizer,
559 $target_tag, $target_subfields, $target_offset, $target_length, $target_normalizer) = @_;
561 $self->add_required_check(
562 [ { tag => $source_tag, subfields => $source_subfields, offset => $source_offset, 'length' => $source_length,
563 norms => [ $source_normalizer ] } ],
564 [ { tag => $target_tag, subfields => $target_subfields, offset => $target_offset, 'length' => $target_length,
565 norms => [ $target_normalizer ] } ]
571 my @matches = $matcher->get_matches($marc_record, $max_matches);
572 foreach $match (@matches) {
573 # matches already sorted in order of
575 print "record ID: $match->{'record_id'};
576 print "score: $match->{'score'};
579 Identifies all of the records matching the given MARC record. For a record already
580 in the database to be considered a match, it must meet the following criteria:
584 =item 1. Total score from its matching field must exceed the supplied threshold.
586 =item 2. It must pass all required checks.
590 Only the top $max_matches matches are returned. The returned array is sorted
591 in order of decreasing score, i.e., the best match is first.
597 my ($source_record, $max_matches) = @_;
601 foreach my $matchpoint (@{ $self->{'matchpoints'} }) {
602 my @source_keys = _get_match_keys($source_record, $matchpoint);
603 next if scalar(@source_keys) == 0;
605 my $query = join(" or ", map { "$matchpoint->{'index'}=$_" } @source_keys);
606 # FIXME only searching biblio index at the moment
607 my ($error, $searchresults, $total_hits) = SimpleSearch($query, 0, $max_matches);
609 warn "search failed ($query) $error" if $error;
610 foreach my $matched (@$searchresults) {
611 $matches{$matched} += $matchpoint->{'score'};
615 # get rid of any that don't meet the threshold
616 %matches = map { ($matches{$_} >= $self->{'threshold'}) ? ($_ => $matches{$_}) : () } keys %matches;
618 # get rid of any that don't meet the required checks
619 %matches = map { _passes_required_checks($source_record, $_, $self->{'required_checks'}) ? ($_ => $matches{$_}) : () }
623 foreach my $marcblob (keys %matches) {
624 my $target_record = MARC::Record->new_from_usmarc($marcblob);
625 my $result = TransformMarcToKoha(C4::Context->dbh, $target_record, '');
626 # FIXME - again, bibliospecific
627 # also, can search engine be induced to give just the number in the first place?
628 my $record_number = $result->{'biblionumber'};
629 push @results, { 'record_id' => $record_number, 'score' => $matches{$marcblob} };
631 @results = sort { $b->{'score'} cmp $a->{'score'} } @results;
632 if (scalar(@results) > $max_matches) {
633 @results = @results[0..$max_matches-1];
641 $description = $matcher->dump();
643 Returns a reference to a structure containing all of the information
644 in the matcher object. This is mainly a convenience method to
645 aid setting up a HTML editing form.
654 $result->{'matcher_id'} = $self->{'id'};
655 $result->{'code'} = $self->{'code'};
656 $result->{'description'} = $self->{'description'};
658 $result->{'matchpoints'} = [];
659 foreach my $matchpoint (@{ $self->{'matchpoints'} }) {
660 push @{ $result->{'matchpoints'} }, $matchpoint;
662 $result->{'matchchecks'} = [];
663 foreach my $matchcheck (@{ $self->{'required_checks'} }) {
664 push @{ $result->{'matchchecks'} }, $matchcheck;
670 sub _passes_required_checks {
671 my ($source_record, $target_blob, $matchchecks) = @_;
672 my $target_record = MARC::Record->new_from_usmarc($target_blob); # FIXME -- need to avoid parsing record twice
674 # no checks supplied == automatic pass
675 return 1 if $#{ $matchchecks } == -1;
677 foreach my $matchcheck (@{ $matchchecks }) {
678 my $source_key = join "", _get_match_keys($source_record, $matchcheck->{'source_matchpoint'});
679 my $target_key = join "", _get_match_keys($target_record, $matchcheck->{'target_matchpoint'});
680 return 0 unless $source_key eq $target_key;
685 sub _get_match_keys {
686 my $source_record = shift;
687 my $matchpoint = shift;
688 my $check_only_first_repeat = @_ ? shift : 0;
690 # If there is more than one component to the matchpoint (e.g.,
691 # matchpoint includes both 003 and 001), any repeats
692 # of the first component's tag are identified; repeats
693 # of the subsequent components' tags are appended to
694 # each parallel key dervied from the first component,
695 # up to the number of repeats of the first component's tag.
697 # For example, if the record has one 003 and two 001s, only
698 # one key is retrieved because there is only one 003. The key
699 # will consist of the contents of the first 003 and first 001.
701 # If there are two 003s and two 001s, there will be two keys:
702 # first 003 + first 001
703 # second 003 + second 001
706 for (my $i = 0; $i <= $#{ $matchpoint->{'components'} }; $i++) {
707 my $component = $matchpoint->{'components'}->[$i];
709 FIELD: foreach my $field ($source_record->field($component->{'tag'})) {
711 last FIELD if $j > 0 and $check_only_first_repeat;
712 last FIELD if $i > 0 and $j > $#keys;
715 if ($field->is_control_field()) {
716 $string=$field->data();
718 foreach my $subfield ($field->subfields()) {
719 if (exists $component->{'subfields'}->{$subfield->[0]}) {
720 $string .= " " . $subfield->[1];
724 if ($component->{'length'}>0) {
725 $string= substr($string, $component->{'offset'}, $component->{'length'});
726 # FIXME normalize, substr
727 } elsif ($component->{'offset'}) {
728 $string= substr($string, $component->{'offset'});
730 $key = _normalize($string);
732 push @keys, $key if $key;
734 $keys[$j] .= " $key" if $key;
742 sub _parse_match_component {
743 my $input_component = shift;
746 $component->{'tag'} = $input_component->{'tag'};
747 $component->{'subfields'} = { map { $_ => 1 } split(//, $input_component->{'subfields'}) };
748 $component->{'offset'} = exists($input_component->{'offset'}) ? $input_component->{'offset'} : -1;
749 $component->{'length'} = $input_component->{'length'} ? $input_component->{'length'} : 0;
750 $component->{'norms'} = $input_component->{'norms'} ? $input_component->{'norms'} : [];
755 # FIXME - default normalizer
757 my $value = uc shift;
758 $value =~ s/[.;:,\]\[\)\(\/'"]//g;
760 #$value =~ s/^\s+$//;
763 #$value =~ s/[.;,\]\[\)\(\/"']//g;
772 Koha Development Team <http://koha-community.org/>
774 Galen Charlton <galen.charlton@liblime.com>