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

Last change on this file since 4845 was 4841, checked in by davidb, 21 years ago

Modification to sorting metadata when metadata=Creator. Now tries
to use last word of name as surname is there are no commas in
string

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 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
32# format an english string for sorting
33# i.e. convert to lowercase, remove the, a or an
34# from beginning of string etc.
35sub format_string_english {
36 my ($stringref) = @_;
37
38 $$stringref =~ tr/A-Z/a-z/;
39 $$stringref =~ s/&\S+;//g;
40 $$stringref =~ s/[^a-z0-9 ]//g;
41 $$stringref =~ s/^\s*(the|a|an)\b//;
42 $$stringref =~ s/^\s+//;
43}
44
45# format an english name for sorting
46# i.e. convert to lowercase, put surname before
47# first names etc.
48sub format_string_name_english {
49 my ($stringref) = @_;
50
51 $$stringref =~ tr/A-Z/a-z/;
52 $$stringref =~ s/&\S+;//g;
53
54 my $comma_format = ($$stringref =~ m/^.+,.+$/);
55
56 $$stringref =~ s/[^a-z0-9 ]//g;
57 $$stringref =~ s/\s+/ /g;
58 $$stringref =~ s/^\s+//;
59
60
61 if (!$comma_format) {
62 # No commas in name => name in 'firstname surname' format
63 # need to sort by surname
64 my @names = split / /, $$stringref;
65 my $surname = pop @names;
66 while (scalar @names && $surname =~ /^(jnr|snr)$/i) {
67 $surname = pop @names;
68 }
69 $$stringref = $surname . " " . $$stringref;
70 }
71}
72
73# takes arguments of day, month, year and converts to
74# date of form yyyymmdd. month may be full (e.g. "January"),
75# abbreviated (e.g. "Jan"), or a number (1-12). Years like "86"
76# will be assumed to be "1986".
77sub format_date {
78 my ($day, $month, $year) = @_;
79
80 my %months = ('january' => '01', 'jan' => '01', 'february' => '02', 'feb' => '02',
81 'march' => '03', 'mar' => '03', 'april' => '04', 'apr' => '04',
82 'may' => '05', 'june' => '06', 'jun' => '06', 'july' => '07',
83 'jul' => '07', 'august' => '08', 'aug' => '08', 'september' => '09',
84 'sep' => '09', 'october' => '10', 'oct' => '10', 'november' => '11',
85 'nov' => '11', 'december' => '12', 'dec' => '12');
86
87 $month =~ tr/A-Z/a-z/;
88
89 if ($day < 1) {
90 print STDERR "sorttools::format_date WARNING day $day out of range\n";
91 $day = "01";
92 } elsif ($day > 31) {
93 print STDERR "sorttools::format_date WARNING day $day out of range\n";
94 $day = "31";
95 }
96
97 $day = "0$day" if (length($day) == 1);
98
99 if ($month =~ /^\d\d?$/) {
100 if ($month < 1) {
101 print STDERR "sorttools::format_date WARNING month $month out of range\n";
102 $month = "01";
103 } elsif ($month > 12) {
104 print STDERR "sorttools::format_date WARNING month $month out of range\n";
105 $month = "12";
106 }
107 if ($month =~ /^\d$/) {
108 $month = "0" . $month;
109 }
110 } elsif (!defined $months{$month}) {
111 print STDERR "sorttools::format_date WARNING month $month out of range\n";
112 $month = "01";
113 } else {
114 $month = $months{$month};
115 }
116
117 if ($year !~ /^\d\d\d\d$/) {
118 if ($year !~ /^\d\d$/) {
119 my $newyear = 1900 + $year;
120 print STDERR "sorttools::format_date WARNING year $year assumed to be $newyear\n";
121 $year=$newyear;
122 } else {
123 print STDERR "sorttools::format_date WARNING year $year out of range - reset to 1900\n";
124 $year = "1900";
125 }
126 }
127
128 return "$year$month$day";
129}
130
131
1321;
Note: See TracBrowser for help on using the repository browser.