Line 0
Link Here
|
0 |
- |
1 |
package Koha::Filter::MARC::Punctuation; |
|
|
2 |
|
3 |
# Copyright 2024 join2 |
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 |
=head1 NAME |
21 |
|
22 |
Koha::Filter::MARC::Punctuation - Automatically add punctuation to |
23 |
Marc records if the leader specifies they were catalogued without it. |
24 |
|
25 |
Documentation for proper punctuation: |
26 |
https://www.loc.gov/aba/pcc/documents/isbdmarc2016.pdf |
27 |
|
28 |
=head1 DESCRIPTION |
29 |
|
30 |
If the Marc leader 18 specifies, that the record was catalogued |
31 |
without punctuation add the proper punctuation chars to the individual |
32 |
fields before passing the record on to fruther processing. Once this |
33 |
filter is applied it should be safe for further steps to assume that |
34 |
the record holds Marc punctuation. (The records in the database are |
35 |
_not_ changed.) |
36 |
|
37 |
Trigger to add punctuation: leader 18 is either `n` or `c`. |
38 |
|
39 |
Currently handled fields: |
40 |
|
41 |
- 245 |
42 |
- 246 |
43 |
- 247 |
44 |
|
45 |
=cut |
46 |
|
47 |
use strict; |
48 |
use warnings; |
49 |
|
50 |
use base qw(Koha::RecordProcessor::Base); |
51 |
our $NAME = 'Punctuation'; |
52 |
|
53 |
=head2 _decorate_245 |
54 |
|
55 |
Add punctuation to MARC field 245 (title statement). The function |
56 |
assumes that it is only called if no punctuation is part of the |
57 |
record. |
58 |
=cut |
59 |
|
60 |
sub _decorate_245 { |
61 |
|
62 |
my ($field) = @_; |
63 |
|
64 |
# simple chars _preceeding_ a subfield |
65 |
my %pchrs = ( |
66 |
'b' => ' : ', |
67 |
'c' => ' / ', |
68 |
'd' => ' ; ', |
69 |
'e' => '. ', |
70 |
'f' => ', ', |
71 |
'g' => ', ', |
72 |
'k' => ' : ', |
73 |
'n' => '. ', |
74 |
'p' => ', ', |
75 |
's' => '. ', |
76 |
); |
77 |
|
78 |
my @new_subfields = (); |
79 |
foreach my $subfield ( $field->subfields ) { |
80 |
my $sf = $subfield->[0]; |
81 |
my $value = $subfield->[1]; |
82 |
|
83 |
if ( exists $pchrs{$sf} ) { |
84 |
$value = $pchrs{$sf} . $value; |
85 |
} |
86 |
|
87 |
# media type needs to be enclosed |
88 |
if ( $sf eq 'h' ) { |
89 |
$value = '[' . $value . ']'; |
90 |
} |
91 |
|
92 |
push( @new_subfields, $sf, $value ); |
93 |
|
94 |
# TBD does titles always end with a `.`? |
95 |
} |
96 |
return @new_subfields; |
97 |
} |
98 |
|
99 |
=head2 _decorate_246_247 |
100 |
|
101 |
Add punctuation to MARC field 246 (varying form title), 247 (former |
102 |
title). The function assumes that it is only called if no punctuation |
103 |
is part of the record. |
104 |
|
105 |
=cut |
106 |
|
107 |
sub _decorate_246_247 { |
108 |
|
109 |
my ($field) = @_; |
110 |
|
111 |
my %pchrs = ( |
112 |
'b' => ' : ', |
113 |
'f' => ', ', |
114 |
'n' => '. ', |
115 |
'q' => ', ', |
116 |
'r' => ' = ', |
117 |
't' => ' = ', |
118 |
); |
119 |
|
120 |
my @new_subfields = (); |
121 |
foreach my $subfield ( $field->subfields ) { |
122 |
my $sf = $subfield->[0]; |
123 |
my $value = $subfield->[1]; |
124 |
|
125 |
if ( exists $pchrs{$sf} ) { |
126 |
$value = $pchrs{$sf} . $value; |
127 |
} |
128 |
|
129 |
if ( $sf eq 'i' ) { |
130 |
|
131 |
# _append_ a char to i |
132 |
$value .= ': '; |
133 |
} |
134 |
if ( $sf eq 'g' ) { |
135 |
$value = '(' . $value . ')'; |
136 |
} |
137 |
|
138 |
# media type needs to be enclosed |
139 |
if ( $sf eq 'h' ) { |
140 |
$value = '[' . $value . ']'; |
141 |
} |
142 |
|
143 |
push( @new_subfields, $sf, $value ); |
144 |
|
145 |
# TBD does titles always end with a `.`? |
146 |
} |
147 |
return @new_subfields; |
148 |
} |
149 |
|
150 |
=head2 filter |
151 |
|
152 |
my $newrecord = $filter->filter($record); |
153 |
|
154 |
Return title fields with proper punctuation either as the record held |
155 |
them already or as they were added. |
156 |
|
157 |
=cut |
158 |
|
159 |
sub filter { |
160 |
my ( $self, $record ) = @_; |
161 |
|
162 |
return |
163 |
unless $record and ref($record) eq 'MARC::Record'; |
164 |
|
165 |
my $leader = $record->leader(); |
166 |
my $punctuation_omitted = |
167 |
( ( substr( $leader, 18, 1 ) eq 'c' ) or ( substr( $leader, 18, 1 ) eq 'n' ) ); |
168 |
|
169 |
if ($punctuation_omitted) { |
170 |
my @new_subfields = (); |
171 |
foreach my $field ( $record->fields ) { |
172 |
@new_subfields = (); |
173 |
|
174 |
# decorate fields by individual functions: |
175 |
if ( $field->tag() eq "245" ) { |
176 |
@new_subfields = _decorate_245($field); |
177 |
} |
178 |
|
179 |
if ( ( $field->tag() eq "246" ) |
180 |
or ( $field->tag() eq "247" ) ) |
181 |
{ |
182 |
@new_subfields = _decorate_246_247($field); |
183 |
} |
184 |
|
185 |
# If filtered, replace the field content |
186 |
if (@new_subfields) { |
187 |
$field->replace_with( |
188 |
MARC::Field->new( |
189 |
$field->tag(), |
190 |
$field->indicator(1), |
191 |
$field->indicator(2), |
192 |
@new_subfields |
193 |
) |
194 |
); |
195 |
} |
196 |
} |
197 |
} |
198 |
return $record; |
199 |
} |
200 |
|
201 |
1; |