source: gsdl/trunk/perllib/sorttools.pm@ 14926

Last change on this file since 14926 was 10977, checked in by kjdon, 18 years ago

extended the match for author metadata - can now have Authors, and also e.g. pub.Authors,Author - in this case it will just look at the first metadata name

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1###########################################################################
2#
3# sorttools.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# various subroutines to format strings
27# for sorting
28
29package sorttools;
30
31# moved here from BasClas so import can share it
32sub format_metadata_for_sorting {
33 my ($metaname, $metavalue, $doc_obj) = @_;
34 if (!defined $metaname || $metaname !~ /\S/ || ! defined $metavalue || $metavalue !~ /\S/) {
35 return "";
36 }
37
38 if ($metaname eq "Language") {
39 $metavalue = $iso639::fromiso639{$metavalue};
40 return $metavalue;
41 }
42
43 my $lang;
44 if (defined $doc_obj) {
45 $lang = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Language');
46 }
47 $lang = 'en' unless defined $lang;
48
49 # is this metadata likely to be a name?
50 my $function_name="format_string_name_$lang";
51 if ($metaname =~ /^(?:\w+\.)?(?:Creators?|Authors?|Editors?)(?:[:,].*)?$/
52 && exists &$function_name) {
53 no strict 'refs';
54 &$function_name(\$metavalue);
55 } else {
56 $function_name="format_string_$lang";
57 if (exists &$function_name) {
58 no strict 'refs';
59 &$function_name(\$metavalue);
60 }
61 }
62
63 return $metavalue;
64}
65
66### language-specific sorting functions (called by format_metadata_for_sorting)
67
68## format_string_$lang() converts to lowercase (where appropriate), and
69# removes punctuation, articles from the start of string, etc
70## format_string_name_$lang() converts to lowercase, puts the surname first,
71# removes punctuation, etc
72
73sub format_string_en {
74 my $stringref = shift;
75 $$stringref = lc($$stringref);
76 $$stringref =~ s/&[^\;]+\;//g; # html entities
77 $$stringref =~ s/^\s*(the|a|an)\b//; # articles
78 $$stringref =~ s/[^[:alnum:]]//g;
79 $$stringref =~ s/\s+/ /g;
80 $$stringref =~ s/^\s+//;
81 $$stringref =~ s/\s+$//;
82}
83
84sub format_string_name_en {
85 my ($stringref) = @_;
86 $$stringref =~ tr/A-Z/a-z/;
87 $$stringref =~ s/&\S+;//g;
88
89 my $comma_format = ($$stringref =~ m/^.+,.+$/);
90 $$stringref =~ s/[[:punct:]]//g;
91 $$stringref =~ s/\s+/ /g;
92 $$stringref =~ s/^\s+//;
93 $$stringref =~ s/\s+$//;
94
95
96 if (!$comma_format) {
97 # No commas in name => name in 'firstname surname' format
98 # need to sort by surname
99 my @names = split / /, $$stringref;
100 my $surname = pop @names;
101 while (scalar @names && $surname =~ /^(jnr|snr)$/i) {
102 $surname = pop @names;
103 }
104 $$stringref = $surname . " " . $$stringref;
105 }
106}
107
108
109sub format_string_fr {
110 my $stringref = shift;
111
112 $$stringref = lc($$stringref);
113 $$stringref =~ s/&[^\;]+\;//g; # html entities
114 $$stringref =~ s/[^[:alpha:]]//g;
115 $$stringref =~ s/^\s*(les?|la|une?)\b//; # articles
116 $$stringref =~ s/\s+/ /g;
117 $$stringref =~ s/^\s+//;
118 $$stringref =~ s/\s+$//;
119}
120
121sub format_string_es {
122 my $stringref = shift;
123
124 $$stringref = lc($$stringref);
125 $$stringref =~ s/&[^\;]+\;//g; # html entities
126 $$stringref =~ s/[^[:alpha:]]//g;
127 $$stringref =~ s/^\s*(la|el)\b//; # articles
128 $$stringref =~ s/\s+/ /g;
129 $$stringref =~ s/^\s+//;
130 $$stringref =~ s/\s+$//;
131}
132
133### end of language-specific functions
134
135# takes arguments of day, month, year and converts to
136# date of form yyyymmdd. month may be full (e.g. "January"),
137# abbreviated (e.g. "Jan"), or a number (1-12). Years like "86"
138# will be assumed to be "1986".
139sub format_date {
140 my ($day, $month, $year) = @_;
141
142 my %months = ('january' => '01', 'jan' => '01', 'february' => '02', 'feb' => '02',
143 'march' => '03', 'mar' => '03', 'april' => '04', 'apr' => '04',
144 'may' => '05', 'june' => '06', 'jun' => '06', 'july' => '07',
145 'jul' => '07', 'august' => '08', 'aug' => '08', 'september' => '09',
146 'sep' => '09', 'october' => '10', 'oct' => '10', 'november' => '11',
147 'nov' => '11', 'december' => '12', 'dec' => '12');
148
149 $month =~ tr/A-Z/a-z/;
150
151 if ($day < 1) {
152 print STDERR "sorttools::format_date WARNING day $day out of range\n";
153 $day = "01";
154 } elsif ($day > 31) {
155 print STDERR "sorttools::format_date WARNING day $day out of range\n";
156 $day = "31";
157 }
158
159 $day = "0$day" if (length($day) == 1);
160
161 if ($month =~ /^\d\d?$/) {
162 if ($month < 1) {
163 print STDERR "sorttools::format_date WARNING month $month out of range\n";
164 $month = "01";
165 } elsif ($month > 12) {
166 print STDERR "sorttools::format_date WARNING month $month out of range\n";
167 $month = "12";
168 }
169 if ($month =~ /^\d$/) {
170 $month = "0" . $month;
171 }
172 } elsif (!defined $months{$month}) {
173 print STDERR "sorttools::format_date WARNING month $month out of range\n";
174 $month = "01";
175 } else {
176 $month = $months{$month};
177 }
178
179 if ($year !~ /^\d\d\d\d$/) {
180 if ($year !~ /^\d\d$/) {
181 my $newyear = 1900 + $year;
182 print STDERR "sorttools::format_date WARNING year $year assumed to be $newyear\n";
183 $year=$newyear;
184 } else {
185 print STDERR "sorttools::format_date WARNING year $year out of range - reset to 1900\n";
186 $year = "1900";
187 }
188 }
189
190 return "$year$month$day";
191}
192
193
1941;
Note: See TracBrowser for help on using the repository browser.