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

Last change on this file since 9747 was 9581, checked in by jrm21, 19 years ago

couple of extra languages for formatting

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 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
35 if ($metaname eq "Language") {
36 $metavalue = $iso639::fromiso639{$metavalue};
37 return $metavalue;
38 }
39
40 my $lang;
41 if (defined $doc_obj) {
42 $lang = $doc_obj->get_metadata_element ($doc_obj->get_top_section(), 'Language');
43 }
44 $lang = 'en' unless defined $lang;
45
46 # is this metadata likely to be a name?
47 my $function_name="format_string_name_$lang";
48 if ($metaname =~ /^(?:\w+\.)?(?:Creator|Author|Editor)(?::.*)?$/
49 && exists &$function_name) {
50 no strict 'refs';
51 &$function_name(\$metavalue);
52 } else {
53 $function_name="format_string_$lang";
54 if (exists &$function_name) {
55 no strict 'refs';
56 &$function_name(\$metavalue);
57 }
58 }
59
60 return $metavalue;
61}
62
63### language-specific sorting functions (called by format_metadata_for_sorting)
64
65## format_string_$lang() converts to lowercase (where appropriate), and
66# removes punctuation, articles from the start of string, etc
67## format_string_name_$lang() converts to lowercase, puts the surname first,
68# removes punctuation, etc
69
70sub format_string_en {
71 my $stringref = shift;
72
73 $$stringref = lc($$stringref);
74 $$stringref =~ s/&[^\;]+\;//g; # html entities
75 $$stringref =~ s/[^[:alnum:]]//g;
76 $$stringref =~ s/^\s*(the|a|an)\b//; # articles
77 $$stringref =~ s/\s+/ /g;
78 $$stringref =~ s/^\s+//;
79 $$stringref =~ s/\s+$//;
80}
81
82sub format_string_name_en {
83 my ($stringref) = @_;
84
85 $$stringref =~ tr/A-Z/a-z/;
86 $$stringref =~ s/&\S+;//g;
87
88 my $comma_format = ($$stringref =~ m/^.+,.+$/);
89 $$stringref =~ s/[[:punct:]]//g;
90 $$stringref =~ s/\s+/ /g;
91 $$stringref =~ s/^\s+//;
92 $$stringref =~ s/\s+$//;
93
94
95 if (!$comma_format) {
96 # No commas in name => name in 'firstname surname' format
97 # need to sort by surname
98 my @names = split / /, $$stringref;
99 my $surname = pop @names;
100 while (scalar @names && $surname =~ /^(jnr|snr)$/i) {
101 $surname = pop @names;
102 }
103 $$stringref = $surname . " " . $$stringref;
104 }
105 print "name: $$stringref\n";
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.