[1412] | 1 | package DateExtract;
|
---|
| 2 |
|
---|
[2018] | 3 | ##use BasPlug; ## no, DON'T use BasPlug, BasPlug uses us....
|
---|
[1412] | 4 | use sorttools;
|
---|
[15895] | 5 | use strict;
|
---|
[1412] | 6 | use util;
|
---|
| 7 |
|
---|
| 8 | #75% of the instances of the word century use full name ordinals
|
---|
| 9 | my %ordinals = ("first" => 1, "second" => 2, "third" => 3, "fourth" => 4,
|
---|
[1954] | 10 | "fifth" => 5, "sixth" => 6, "seventh" => 7, "eighth" => 8,
|
---|
| 11 | "ninth" => 9, "tenth" => 10, "eleventh" => 11, "twelfth" => 12,
|
---|
| 12 | "thirteenth" => 13, "fourteenth" => 14, "fifteenth" => 15,
|
---|
| 13 | "sixteenth" => 16, "seventeenth" => 17, "eighteenth" => 18,
|
---|
| 14 | "nineteenth" => 19, "twentieth" => 20);
|
---|
[1412] | 15 |
|
---|
| 16 |
|
---|
[1954] | 17 |
|
---|
[1412] | 18 |
|
---|
| 19 | #definitions for a date grammar.
|
---|
| 20 | my $fulOrd = join('|',(keys %ordinals));
|
---|
| 21 |
|
---|
| 22 | my @months = ("january","february","march","april","may","june","july",
|
---|
[1954] | 23 | "august","september","october","november","december");
|
---|
[1412] | 24 |
|
---|
| 25 | my $shortmth = "";
|
---|
[15894] | 26 | foreach my $m (@months) { $shortmth .= (substr($m,0,3)."\\.?|"); }
|
---|
[1412] | 27 | chop($shortmth);
|
---|
| 28 |
|
---|
| 29 | my $longmth = join('|',@months);
|
---|
| 30 |
|
---|
| 31 |
|
---|
| 32 | my $Qualifier = "(B(\\.)?C(\\.)?(E(\\.)?)?)|(A(\\.)?D(\\.)?)|(C(\\.)E(\\.)?)";
|
---|
| 33 | my $Century = "Cent(\\.|ur(y|ies))";
|
---|
| 34 | my $Ord = "st|nd|rd|th";
|
---|
| 35 | my $seasonref = "(spring|fall|autumn|winter|summer)";
|
---|
| 36 | my $sep = " ?- ?";
|
---|
| 37 | my $tri_digit = "(in|since|(the year)|($seasonref of)) \\d{3}\\D( ($Qualifier))?";
|
---|
| 38 | my $centurydate = "(((\\d{1,2})($Ord))|($fulOrd)) ($Century)( ($Qualifier))?";
|
---|
| 39 | my $qualified = "(($Qualifier) ?(\\d{1,4}))|((\\d{1,4}) ?($Qualifier))";
|
---|
| 40 | my $millenium = "[1-9]\\d{3}";
|
---|
| 41 | my $range = "(($millenium)($sep)($millenium))|(($millenium)($sep)(\\d{1,2}))|(($qualified)($sep)($qualified))|(($Qualifier) ?(\\d{1,3})($sep)(\\d{1,3}))|((\\d{1,3})($sep)(\\d{1,3}) ?($Qualifier))";
|
---|
| 42 |
|
---|
| 43 | my $pgnum = "(p(p|g)?\\.? ?\\d+((-|,)(\\d+))?)|(page \\d+)|(pages \\d+((-|,)(\\d+))?)";
|
---|
| 44 | my $lgnum = "\\d{1,3},(\\d{3})+";
|
---|
| 45 | my $colon = ":\\d+";
|
---|
| 46 | my $money = "\$(\\d{1,3}(((,\\d{3})+)|\\d+))"; $money = "\\" . $money;
|
---|
| 47 | my $microfilm = " reel([^\\.\\)])*(\\.|\\))";
|
---|
| 48 | my $lastaltered = "last (edited|updated)";
|
---|
| 49 | my $references = "reference(s?)(:?)\\n";
|
---|
| 50 | my $cited = "work(s?) cited";
|
---|
| 51 | my $biblio = "bibliography(:?)( ?)\\n";
|
---|
| 52 |
|
---|
| 53 | my $direction = "(N(\\.|o\\.|th\\.|orth))|(S(\\.|th\\.|outh))|(E(\\.|ast))|(W(\\.|est))";
|
---|
| 54 | my $street_id = "(st\\.)|(street[^A-Za-z])|(ave\\.)|(avenue[^A-Za-z])|(boulevard[^A-Za-z])|(blvd\\.)|(rd\\.)|(road[^A-Za-z])";
|
---|
| 55 | my $streetname = "(\\d{1,2}(<SUP>)?($Ord)(</SUP>)?)|([A-Za-z]+( [A-Za-z]+)?)|([A-Za-z]+-[A-Za-z]+($Ord))";
|
---|
| 56 | my $address = "\\d{1,4} (($direction) )?($streetname) ($street_id)";
|
---|
| 57 |
|
---|
| 58 | my $bracket = "\\($millenium\\)";
|
---|
| 59 | my $ref_end = ", ?$millenium\\)";
|
---|
| 60 | my $colonsp = ": $millenium";
|
---|
| 61 | my $reprint = "[Rr]eprint of \\d{4} edition";
|
---|
| 62 | my $comma = ", $millenium\\.";
|
---|
| 63 | my $fullstop = "\\. $millenium\\.";
|
---|
| 64 | my $semi = "; $millenium\\.";
|
---|
| 65 |
|
---|
| 66 | my $lookalikes = "($pgnum)|($lgnum)|($colon)|($money)|($microfilm)|($address)";
|
---|
| 67 | my $spurious = "($lastaltered)";
|
---|
| 68 | my $bibheader = "($references)|($cited)|($biblio)";
|
---|
| 69 |
|
---|
| 70 |
|
---|
| 71 | sub get_date_metadata {
|
---|
| 72 | #get the text of the document, the "document object" concerned,
|
---|
| 73 | #and the current section within the document
|
---|
[15895] | 74 | my ($text, $doc, $cursection, $keep_bib, $max_year, $max_century) = @_;
|
---|
[1412] | 75 |
|
---|
| 76 | #format a prechristian maximum century value to be negative so that it can
|
---|
| 77 | #be used in numeric comparison
|
---|
| 78 | if($max_century =~ /B/)
|
---|
| 79 | {
|
---|
[1954] | 80 | $max_century = $`;
|
---|
| 81 | $max_century =~ /\d+/;
|
---|
| 82 | $max_century = $&;
|
---|
| 83 | $max_century *=-1
|
---|
[1412] | 84 | }
|
---|
| 85 |
|
---|
[15895] | 86 | my $extr = &remove_excess($text);
|
---|
[1412] | 87 | #print "EXTRACTION TEXT:\n $extr";
|
---|
| 88 | $extr = &remove_tags($extr);
|
---|
| 89 | if(!$keep_bib){
|
---|
[1954] | 90 | $extr = &remove_biblio($extr);
|
---|
[1412] | 91 | }
|
---|
| 92 |
|
---|
| 93 |
|
---|
[15895] | 94 | my @datelist = ();
|
---|
[1412] | 95 | while($extr =~ m!($range)|($millenium)|($qualified)|($centurydate)|($tri_digit)!i)
|
---|
| 96 | {
|
---|
[1954] | 97 | $extr = $';
|
---|
| 98 | my $fulldate = $&;
|
---|
| 99 | if ($fulldate =~ /$centurydate/i)
|
---|
| 100 | {
|
---|
| 101 | if($max_century!=-1)
|
---|
| 102 | {
|
---|
[1412] | 103 |
|
---|
[15895] | 104 | my $date = $fulldate; if($date =~ /\d+/) {$date = $&;}
|
---|
[1954] | 105 | else
|
---|
| 106 | {
|
---|
| 107 | $date=$fulldate; $date =~ m! ($Century)!i; $date = $`;
|
---|
| 108 | $date =~ tr/A-Z/a-z/;
|
---|
| 109 | $date = $ordinals{$date};
|
---|
| 110 | }
|
---|
| 111 | if($max_century >= $date){
|
---|
| 112 | $date = ($date-1)*100 +1;
|
---|
| 113 | #if it BC, make it negative
|
---|
| 114 | $date = &convert_bc($fulldate,$date);
|
---|
[15895] | 115 | my $end = $date + 99;
|
---|
| 116 | my @century = ($date..$end);
|
---|
[1954] | 117 | @datelist = (@datelist,@century);
|
---|
| 118 | }
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | elsif($fulldate =~ /$range/)
|
---|
| 123 | {
|
---|
| 124 | $fulldate =~ /$sep/;
|
---|
| 125 | my @addlist = ();
|
---|
| 126 | #print "Range: $fulldate\n";
|
---|
[15895] | 127 | my $fullfirst = $`;
|
---|
| 128 | my $fullsecond = $';
|
---|
| 129 | $fullfirst =~ /\d+/;
|
---|
| 130 | my $first = $&;
|
---|
| 131 | $fullsecond =~ /\d+/;
|
---|
| 132 | my $second = $&;
|
---|
| 133 | my $len1 = length($first);
|
---|
| 134 | my $len2 = length($second);
|
---|
[1954] | 135 | $second = (substr($first,0,($len1-$len2))).$second;
|
---|
| 136 | $first = &convert_bc($fullfirst,$first);
|
---|
| 137 | $second = &convert_bc($fullsecond,$second);
|
---|
| 138 | @addlist = ($first..$second);
|
---|
| 139 | @datelist = (@datelist,@addlist);
|
---|
| 140 |
|
---|
| 141 | }
|
---|
| 142 | else {
|
---|
| 143 |
|
---|
| 144 | my $date = $fulldate; $date =~ /\d+/; $date = $&;
|
---|
| 145 | $date = &convert_bc($fulldate,$date);
|
---|
| 146 | #add the date metadata
|
---|
| 147 | push(@datelist,$date);
|
---|
| 148 | #print "datelist @datelist\n"
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[1412] | 151 | }
|
---|
| 152 |
|
---|
| 153 | if(@datelist){
|
---|
[1954] | 154 | @datelist = sort { $a <=> $b } @datelist;
|
---|
| 155 | @datelist = &post_process($max_year, @datelist);
|
---|
[15895] | 156 | foreach my $date (@datelist)
|
---|
[1954] | 157 | {
|
---|
| 158 | if($date>0){
|
---|
| 159 | $doc->add_metadata($cursection,"Coverage",$date);}
|
---|
| 160 | else{
|
---|
| 161 | $doc->add_metadata($cursection,"Coverage","bc".(-1*$date));}
|
---|
| 162 |
|
---|
| 163 | }
|
---|
[1412] | 164 | }
|
---|
| 165 | }
|
---|
| 166 | sub convert_bc {
|
---|
[15895] | 167 | my ($full,$num) = @_;
|
---|
[1412] | 168 | if ($full =~ /B/) { $num *= -1; }
|
---|
| 169 | $num;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | sub post_process {
|
---|
[15895] | 173 | my ($max_year, @list) = @_;
|
---|
| 174 | my @cleanlist = ();
|
---|
| 175 | my $prev = 0;
|
---|
| 176 | foreach my $e (@list) {
|
---|
[1954] | 177 | if ($e!=$prev && $e <= $max_year) {
|
---|
| 178 | push(@cleanlist, $e);
|
---|
| 179 | }
|
---|
| 180 | $prev = $e;
|
---|
[1412] | 181 | }
|
---|
| 182 | @cleanlist;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 |
|
---|
| 186 | #removes all html tags from that data, as they will not contain dates which
|
---|
| 187 | #are part of the content of the document, and therefore interesting, but do
|
---|
| 188 | #contain date lookalikes
|
---|
| 189 | sub remove_tags {
|
---|
[15895] | 190 | my ($tmp) = @_;
|
---|
[1412] | 191 |
|
---|
[15895] | 192 | my $parsed = "";
|
---|
[1412] | 193 | #while there is still text to be parsed and tags are still found
|
---|
| 194 | while($tmp=~ m!<([^>])*(>|$)! && $tmp ne "")
|
---|
| 195 | {
|
---|
[1954] | 196 | $parsed .= $`;#keep all that is not in a tag
|
---|
| 197 | $tmp = $'; #restart the search after then end of the tag
|
---|
[1412] | 198 | }
|
---|
| 199 | $parsed .= $tmp; #add anything after the last match
|
---|
| 200 | $parsed;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 |
|
---|
| 204 | sub remove_excess {
|
---|
[15895] | 205 | my ($tmp) = @_;
|
---|
| 206 | my $parsed = "";
|
---|
[1412] | 207 |
|
---|
| 208 |
|
---|
| 209 | if(($tmp =~ m!($spurious)|($lookalikes)!i) == 0 )
|
---|
| 210 | {
|
---|
[1954] | 211 | $parsed = $tmp;
|
---|
[1412] | 212 | }
|
---|
| 213 | else {
|
---|
[1954] | 214 | while ($tmp =~ m!($spurious)|($lookalikes)!i
|
---|
| 215 | && $tmp ne "")
|
---|
| 216 | {
|
---|
| 217 | $parsed .= $`;
|
---|
[15895] | 218 | my $storage = $&;
|
---|
[1954] | 219 | $tmp = $';
|
---|
| 220 | #match the pattern which indicates most recent alteration
|
---|
| 221 | if ($storage =~ m!$lastaltered!i)
|
---|
| 222 | {
|
---|
| 223 | #match a four digit year or up until the first /
|
---|
| 224 | #(as in last edited 3/97).
|
---|
| 225 | $tmp =~ m!($millenium)|(\/)!;
|
---|
| 226 | $tmp = $';
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | $parsed .= $tmp;
|
---|
| 232 |
|
---|
[1412] | 233 | }
|
---|
| 234 | #print "Parsed:\n $parsed\n\n";
|
---|
| 235 | $parsed;
|
---|
| 236 |
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | sub remove_biblio{
|
---|
[15895] | 240 | my ($tmp) = @_;
|
---|
| 241 | my $parsed = "";
|
---|
[1412] | 242 |
|
---|
| 243 | if($tmp =~ m!$bibheader!i)
|
---|
| 244 | {
|
---|
[1954] | 245 | $tmp=$`;
|
---|
[1412] | 246 | }
|
---|
| 247 |
|
---|
| 248 | $tmp =~ s/( |\t)+/ /g;
|
---|
| 249 | if(($tmp =~ m!($ref_end)|($bracket)|($colonsp)|($reprint)|($comma)|($fullstop)|($semi)|($seasonref) ($millenium)!i) == 0)
|
---|
| 250 | {
|
---|
[1954] | 251 | $parsed = $tmp;
|
---|
[1412] | 252 | }
|
---|
| 253 | else{
|
---|
| 254 |
|
---|
[1954] | 255 | #print "removing bib\n";
|
---|
| 256 | while ($tmp =~ m!($ref_end)|($bracket)|($colonsp)|($reprint)|($comma)|($fullstop)|($semi)|(($seasonref) ($millenium))|($bibheader)!i && $tmp ne "")
|
---|
| 257 | {
|
---|
| 258 |
|
---|
| 259 | $parsed .= $`;
|
---|
| 260 | $tmp = $';
|
---|
| 261 | if($&=~m!($comma)|($fullstop)!)
|
---|
| 262 | {
|
---|
| 263 |
|
---|
[15895] | 264 | my $date = $&;
|
---|
[1954] | 265 | if($parsed =~ m!((\d)($Ord)$)|(($shortmth)$)|(($longmth)$)!i)
|
---|
| 266 | {
|
---|
| 267 | $parsed .= $date;
|
---|
| 268 | }
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | }
|
---|
| 272 | $parsed .= $tmp;
|
---|
[1412] | 273 | }
|
---|
| 274 | $parsed;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 |
|
---|
| 278 | 1;
|
---|