]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/MFHDParser.pm
Merging acq-experiment to trunk, since rel_1_4 has been branched.
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / MFHDParser.pm
1 #!/usr/bin/perl -w
2 use strict;
3 use Date::Manip;
4
5 # Parse MFHD patterns for http://www.loc.gov/marc/holdings/hd853855.html
6
7 # Primary goal:
8 # Expected input: a chunk of MFHD, a start date, and # of issuances to project
9 # Expected output: a set of issuances projected forward from the start date,
10 #   with issue/volume/dates/captions conforming to what the MFHD actually says
11
12 # The thought had been to use Date::Manip to generate the actual dates for
13 # each issuance, like:
14 #
15 #   # To find the 2nd Tuesday of every month
16 #   @date = ParseRecur("0:1*2:2:0:0:0",$base,$start,$stop);
17
18 # Secondary goal: generate automatic summary holdings
19 #   (a la http://www.loc.gov/marc/holdings/hd863865.html)
20
21 # Compressability comes from first indicator
22 sub parse_compressability {
23         my $c = shift || return undef;
24
25         my %compressability = (
26                 '0' => 'Cannot compress or expand',
27                 '1' => 'Can compress but cannot expand',
28                 '2' => 'Can compress or expand',
29                 '3' => 'Unknown',
30                 '#' => 'Undefined'
31         );
32
33         if (exists $compressability{$c}) {
34                 return $compressability{$c};
35         }
36         # 'Unknown compressability indicator - expected one of (0,1,2,3,#)';
37         return undef;
38 }
39
40 # Caption evaluation comes from second indicator
41 sub caption_evaluation {
42         my $ce = shift || return undef;
43
44         my %caption_evaluation = (
45                 '0' => 'Captions verified; all levels present',
46                 '1' => 'Captions verified; all levels may not be present',
47                 '2' => 'Captions unverified; all levels present',
48                 '3' => 'Captions unverified; all levels may not be present',
49                 '#' => 'Undefined',
50         );
51
52         if (exists $caption_evaluation{$ce}) {
53                 return $caption_evaluation{$ce};
54         }
55         # 'Unknown caption evaluation indicator - expected one of (0,1,2,3,#)';
56         return undef;
57 }
58
59 # Start with frequency ($w)
60 # then overlay number of pieces of issuance ($p)
61 # then regularity pattern ($y)
62 my %frequency = (
63         'a' => 'annual',
64         'b' => 'bimonthly',
65         'c' => 'semiweekly',
66         'd' => 'daily',
67         'e' => 'biweekly',
68         'f' => 'semiannual',
69         'g' => 'biennial',
70         'h' => 'triennial',
71         'i' => 'three times a week',
72         'j' => 'three times a month',
73         'k' => 'continuously updated',
74         'm' => 'monthly',
75         'q' => 'quarterly',
76         's' => 'semimonthly',
77         't' => 'three times a year',
78         'w' => 'weekly',
79         'x' => 'completely irregular',
80 );
81
82 sub parse_frequency {
83         my $freq = shift || return undef;
84
85         if ($freq =~ m/^\d+$/) {
86                 return "$freq times a year";
87         } elsif (exists $frequency{$freq}) {
88                 return $frequency{$freq};
89         }
90         # unrecognized frequency specification
91         return undef;
92 }
93
94 # $x - Point at which the highest level increments or changes
95 # Interpretation of two-digit numbers in the 01-12 range depends on the publishing frequency
96 # More than one change can be passed in the subfield and are delimited by commas
97 sub chronology_change {
98         my $chronology_change = shift || return undef;
99         my @c_changes = split /,/, $chronology_change;
100         foreach my $change (@c_changes) {
101                 if ($change == 21) {
102                         
103                 }
104         }
105         return undef;
106 }
107
108 # Publication code : first character in regularity pattern ($y)
109 sub parse_publication_code {
110         my $c = shift || return undef;
111
112         my %publication_code = (
113                 'c' => 'combined',
114                 'o' => 'omitted',
115                 'p' => 'published',
116                 '#' => 'undefined',
117         );
118
119         if (exists $publication_code{$c}) {
120                 return $publication_code{$c};
121         }
122         return undef;
123 }
124
125 # Chronology code : part of regularity pattern ($y)
126 sub parse_chronology_code {
127         my $c = shift || return undef;
128
129         my %chronology_code = (
130                 'd' => 'Day',
131                 'm' => 'Month',
132                 's' => 'Season',
133                 'w' => 'Week',
134                 'y' => 'Year',
135                 'e' => 'Enumeration',
136         );
137
138         if (exists $chronology_code{$c}) {
139                 return $chronology_code{$c};
140         }
141         return undef;
142 }
143
144 sub parse_regularity_pattern {
145         my $pattern = shift;
146         my ($pc, $cc, $cd) = $pattern =~ m{^(\w)(\w)(.+)$};
147         
148         my $pub_code = parse_publication_code($pc);
149         my $chron_code = parse_chronology_code($cc);
150         my $chron_def = parse_chronology_definition($cd);
151         
152         return ($pub_code, $chron_code, $chron_def);
153 }
154
155 sub parse_chronology_definition {
156         my $chron_def = shift || return undef;
157         # Well, this is where it starts to get hard, doesn't it?
158         return $chron_def;
159 }
160
161 print parse_regularity_pattern("cddd");
162 print "\n";
163 print parse_regularity_pattern("38dd");
164 print "\n";
165
166 1;
167
168 # :vim:noet:ts=4:sw=4: