PDFLibrarian/TitleAbbr.pm untested

filter: Statementstmt 0.0% 0 / 150 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 236 CC 23 SCAR 51.8
CC
23
Cov
0%
abbr_iso4_title
380.0
INIT
6.0
INIT
6.0
CRAP 178.0
1 # Copyright (C) 2016--2026 Karl Wette
2 #
3 # This file is part of App::PDFLibrarian.
4 #
5 # App::PDFLibrarian is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or (at
8 # your option) any later version.
9 #
10 # App::PDFLibrarian is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with App::PDFLibrarian. If not, see <http://www.gnu.org/licenses/>.
17  
18 use v5.18;
19 use strict;
20 use warnings;
21 use utf8;
22 use open qw(:std :encoding(UTF-8));
23  
24 package App::PDFLibrarian::TitleAbbr;
25 $App::PDFLibrarian::TitleAbbr::VERSION = '6.3.4';
26 use parent 'Exporter';
27  
28 use Carp::Assert;
29 use Carp;
30 use File::ShareDir qw(dist_file);
31 use File::Spec;
32 use FindBin qw($Script);
33 use Text::CSV::Encoded;
34  
35 use App::PDFLibrarian qw($cfgdir);
36  
37 our @EXPORT_OK = qw(get_aas_macros abbr_iso4_title);
38  
39 my %aas_macros;
40  
41 my @iso4_word_abbr;
42 my $iso4_title_abbr_cachefile = undef;
43 my %iso4_title_abbr_cache;
44 my $iso4_title_abbr_cache_new = 0;
45  
46 1;
47  
48 INIT {
49  
50 # get location of AAS macro/journal title data
51 my $aasdata = dist_file('App-PDFLibrarian', 'title_abbr_aas.csv');
52 croak "$Script: missing AAS data '$aasdata'" unless -f $aasdata;
53  
54 # load AAS macro/journal title data
55 my $csv = Text::CSV::Encoded->new({ encoding_in => 'utf-8', encoding_out => 'utf-8', sep_char => ';' });
56 open(my $fh, '<:encoding(utf-8)', $aasdata) or croak "$Script: could not open file '$aasdata': $!";
57  
58 # check columns
59 my $row = $csv->getline($fh);
60 assert(@{$row} == 2);
61 assert($row->[0] eq 'MACRO');
62 assert($row->[1] eq 'JOURNAL TITLE');
63  
64 # parse data
65 while ($row = $csv->getline($fh)) {
66 $aas_macros{$row->[0]} = $row->[1];
67 }
68  
69 close($fh);
70  
71 }
72  
73 INIT {
74  
75 # get location of ISO4 word abbreviation data
76 my $iso4data = dist_file('App-PDFLibrarian', 'title_abbr_iso4.csv');
77 croak "$Script: missing ISO4 data '$iso4data'" unless -f $iso4data;
78  
79 # load ISO4 word abbreviation data
80 my $csv = Text::CSV::Encoded->new({ encoding_in => 'utf-8', encoding_out => 'utf-8', sep_char => ';' });
81 open(my $fh, '<:encoding(utf-8)', $iso4data) or croak "$Script: could not open file '$iso4data': $!";
82  
83 # check columns
84 my $row = $csv->getline($fh);
85 assert(@{$row} == 3);
86 assert($row->[0] eq 'WORDS');
87 assert($row->[1] eq 'ABBREVIATIONS');
88 assert($row->[2] eq 'LANGUAGES');
89  
90 # parse data
91 while ($row = $csv->getline($fh)) {
92 push @iso4_word_abbr, [$row->[0], $row->[1]];
93 }
94  
95 close($fh);
96  
97 # sort from longest to shortest
98 @iso4_word_abbr = sort { length($b->[0]) <=> length($a->[0]) } @iso4_word_abbr;
99  
100 }
101  
102 INIT {
103  
104 # load ISO4 title abbreviation cache
105 $iso4_title_abbr_cachefile = File::Spec->catfile($cfgdir, "iso4_title_abbr_cache.txt");
106 if (-f $iso4_title_abbr_cachefile) {
107 open(my $fh, '<:encoding(utf-8)', $iso4_title_abbr_cachefile) or croak "$Script: could not open file '$iso4_title_abbr_cachefile': $!";
108 while (<$fh>) {
109 chomp;
110 s/^\s+//;
111 s/\s+$//;
112 my ($title, $words) = split(/\s*=\s*/);
113 my @abbr_words = split(/\s+/, $words);
114 $iso4_title_abbr_cache{$title} = \@abbr_words;
115 }
116 close($fh);
117 }
118  
119 }
120  
121 END {
122  
123 # save ISO4 title abbreviation cache
124 open(my $fh, '>:encoding(utf-8)', $iso4_title_abbr_cachefile) or croak "$Script: could not open file '$iso4_title_abbr_cachefile': $!";
125 foreach my $title (sort(keys(%iso4_title_abbr_cache))) {
126 print $fh "$title = " . join(" ", @{$iso4_title_abbr_cache{$title}}) . "\n";
127 }
128 close($fh);
129 if ($iso4_title_abbr_cache_new > 0) {
130 printf STDERR "$Script: cached $iso4_title_abbr_cache_new new ISO4 title abbreviations in '$iso4_title_abbr_cachefile'\n";
131 }
132  
133 }
134  
135 sub get_aas_macros {
136 return %aas_macros;
137 }
138  
139 sub abbr_iso4_title {
140 my ($separator, $title, $cache) = @_;
141 $cache = 1 if !defined($cache);
142  
143 my @abbr_words;
144  
145 if (defined($iso4_title_abbr_cache{$title})) {
146  
147 # return cached abbreviated titles
148 @abbr_words = @{$iso4_title_abbr_cache{$title}};
149  
150 } else {
151  
152 # split title
153 my @words = split(/\s+/, $title);
154 if (@words == 1) {
155 @abbr_words = @words;
156 } else {
157  
158 # abbreviate words
159 while (@words) {
160 my $word = shift @words;
161  
162 # delete lowercase words
163 next if $word =~ /^\p{Ll}/;
164  
165 # delete non-alphabetic words
166 next if $word =~ /^\W+$/;
167  
168 # keep single uppercase letters as last word
169 if ($word =~ /^\p{Lu}$/ && @words == 0) {
170 push @abbr_words, $word;
171 next;
172 }
173  
174 # loop over abbreviations (sorted from longest to shortest)
175 foreach my $entry (@iso4_word_abbr) {
176 my ($patt, $abbr) = @{$entry};
177  
178 # skip patterns without an abbreviation
179 next if $abbr eq 'n.a.';
180  
181 # apply suffix patterns
182 if ($patt =~ /^-(.*)$/) {
183 my $regex = '^.*' . $1 . '$';
184 last if $word =~ s/$regex/$abbr/;
185 }
186  
187 # apply prefix patterns
188 if ($patt =~ /^(.)(.*)-$/) {
189 my $p1 = $1;
190 my $pr = $2;
191 my $regex = '^' . $p1 . $pr . '.*$';
192 last if $word =~ s/$regex/$abbr/;
193 if ($word =~ /^\p{Lu}/) {
194 my $regex_uc = '^' . uc($p1) . $pr . '.*$';
195 my $abbr_uc = $abbr;
196 $abbr_uc =~ s{^(.)}{ uc($1) }e;
197 last if $word =~ s/$regex_uc/$abbr_uc/;
198 }
199 }
200  
201 # apply whole word patterns
202 if ($patt =~ /^([^-])(.*[^-])$/) {
203 my $p1 = $1;
204 my $pr = $2;
205 my $regex = '^' . $p1 . $pr . '$';
206 last if $word =~ s/$regex/$abbr/;
207 if ($word =~ /^\p{Lu}/) {
208 my $regex_uc = '^' . uc($p1) . $pr . '$';
209 my $abbr_uc = $abbr;
210 $abbr_uc =~ s{^(.)}{ uc($1) }e;
211 last if $word =~ s/$regex_uc/$abbr_uc/;
212 }
213 }
214  
215 }
216  
217 push @abbr_words, $word;
218  
219 }
220  
221 }
222  
223 # add to cache
224 if ($cache) {
225 $iso4_title_abbr_cache{$title} = \@abbr_words;
226 ++$iso4_title_abbr_cache_new;
227 printf STDERR "$Script: ISO4 title abbreviation for '$title': '" . join(" ", @abbr_words) . "'\n";
228 }
229  
230 }
231  
232 # rejoin words
233 my $abbr_title = join($separator, @abbr_words);
234 return $abbr_title;
235  
236 }