| 1 | package WWW::Extractor; | ||
| 2 | use strict; | ||
| 3 | $WWW::Extractor::VERSION = '0.8'; | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | WWW::Extractor - Semi-automated extraction of records from WWW pages | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use strict; | ||
| 12 | use WWW::Extractor; | ||
| 13 | |||
| 14 | my($extractor) = WWW::Extractor->new(); | ||
| 15 | |||
| 16 | $extractor->process($string); | ||
| 17 | |||
| 18 | =head1 DESCRIPTION | ||
| 19 | |||
| 20 | WWW::Extractor is a tool for semi automated extraction of records from | ||
| 21 | a string containing HTML. One record within the string is marked up | ||
| 22 | with extraction markups and the modules uses a pattern matching | ||
| 23 | algorithm to match up the remaining records. | ||
| 24 | |||
| 25 | =head2 Extraction markup | ||
| 26 | |||
| 27 | The user markups up one record withing the HTML stream with the | ||
| 28 | following symbols. | ||
| 29 | |||
| 30 | =over 4 | ||
| 31 | |||
| 32 | =item (((BEGIN))) | ||
| 33 | |||
| 34 | Begin a record | ||
| 35 | |||
| 36 | =item (((fieldname))) | ||
| 37 | |||
| 38 | Begin a field named fieldname | ||
| 39 | |||
| 40 | =item [[[literal string]]] | ||
| 41 | |||
| 42 | This identifies a block of text that the | ||
| 43 | extractor attempts to match. This string is dumped out when the | ||
| 44 | records are extracted. | ||
| 45 | |||
| 46 | =item {{{literal string}}} | ||
| 47 | |||
| 48 | This identifies a block of text that the | ||
| 49 | extractor attempts to match. This string is not dumped out when | ||
| 50 | the records are extracted. | ||
| 51 | |||
| 52 | =item (((nodump))) | ||
| 53 | |||
| 54 | This marks an area of text that is not to be dumped out. | ||
| 55 | |||
| 56 | =item (((/nodump))) | ||
| 57 | |||
| 58 | This ends a section of text that is not to be dumped out. | ||
| 59 | |||
| 60 | =item (((END))) | ||
| 61 | |||
| 62 | End a record. | ||
| 63 | |||
| 64 | =back | ||
| 65 | |||
| 66 | =head1 ALGORITHM | ||
| 67 | |||
| 68 | The algorithm used is based on the edit distance wrapper generation | ||
| 69 | method described in | ||
| 70 | |||
| 71 | @inproceedings{ chidlovskii00automatic, | ||
| 72 | author = "Boris Chidlovskii and Jon Ragetli and Maarten de Rijke", | ||
| 73 | title = "Automatic Wrapper Generation for Web Search Engines", | ||
| 74 | booktitle = "Web-Age Information Management", | ||
| 75 | pages = "399-410", | ||
| 76 | year = "2000", | ||
| 77 | url = "citeseer.nj.nec.com/chidlovskii00automatic.html" } | ||
| 78 | |||
| 79 | but with two major enhancements. | ||
| 80 | |||
| 81 | =over | ||
| 82 | |||
| 83 | =item 1 Before calculating edit distance, the system divides the tokens | ||
| 84 | into different classification groups. | ||
| 85 | |||
| 86 | =item 2 Instead of creating a general grammar from all of the records in a | ||
| 87 | file, the data extractor creates one grammar from the sample entry and | ||
| 88 | then matches the rest of the text to that one grammar. | ||
| 89 | |||
| 90 | =back | ||
| 91 | |||
| 92 | =head1 METHODS | ||
| 93 | |||
| 94 | =over 4 | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | use PDL::Lite; | ||
| 99 | use Data::Dumper; | ||
| 100 | use strict; | ||
| 101 | use integer; | ||
| 102 | use English; | ||
| 103 | |||
| 104 | =pod | ||
| 105 | |||
| 106 | =item $self->new() | ||
| 107 | |||
| 108 | Standard constructor | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | |||
| 113 | sub new { | ||
| 114 | my $proto = shift; | ||
| 115 | my $class = ref($proto) || $proto; | ||
| 116 | my $self = {}; | ||
| 117 | bless ($self, $class); | ||
| 118 | $self->{'debug'} = 0; | ||
| 119 | $self->{'exact_tables'} = 1; | ||
| 120 | $self->{'start_tags'} = 2; | ||
| 121 | $self->{'end_tags'} = 1; | ||
| 122 | $self->{'expand_hrefs'} = 0; | ||
| 123 | $self->{'tokens'} = []; | ||
| 124 | $self->{'finish_tag'} = []; | ||
| 125 | $self->{'grammar'} = []; | ||
| 126 | $self->{'capture_regexp'} = ""; | ||
| 127 | $self->{'capture_field'} = ""; | ||
| 128 | $self->{'capture_string'} = ""; | ||
| 129 | return $self; | ||
| 130 | } | ||
| 131 | |||
| 132 | =pod | ||
| 133 | |||
| 134 | =item $self->open($string) | ||
| 135 | |||
| 136 | Opens a string for processing. | ||
| 137 | |||
| 138 | =cut | ||
| 139 | |||
| 140 | sub open { | ||
| 141 | my ($self, $lp, $grammar) = @_; | ||
| 142 | if ($grammar eq undef) { | ||
| 143 | $self->{'tokens'} = $self->tokenize($lp); | ||
| 144 | my ($g, $context) = $self->extract_grammar($self->{'tokens'}); | ||
| 145 | $self->{'grammar'} = $g; | ||
| 146 | if ($self->{'debug'} > 250) { | ||
| 147 | print "Initial grammar: ", Dumper($g), "\n"; | ||
| 148 | print "Initial content: ", Dumper($context), "\n"; | ||
| 149 | } | ||
| 150 | } else { | ||
| 151 | $self->{'grammar'} = $grammar; | ||
| 152 | $self->{'tokens'} = $self->tokenize($lp); | ||
| 153 | } | ||
| 154 | |||
| 155 | my (@ap) = @{$self->{'tokens'}}; | ||
| 156 | my ($item); | ||
| 157 | $self->{'tokens'} = []; | ||
| 158 | foreach $item (@ap) { | ||
| 159 | if ($self->_classify($item) !~ /^\(\(\(.*?\)\)\)$/s) { | ||
| 160 | push(@{$self->{'tokens'}}, $item); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | =pod | ||
| 169 | |||
| 170 | =item $self->read($hashref) | ||
| 171 | |||
| 172 | Read the next processed entry into a hash pointer. | ||
| 173 | |||
| 174 | =cut | ||
| 175 | |||
| 176 | sub read { | ||
| 177 | my ($self, $f) = @_; | ||
| 178 | my ($i, $gp, $w); | ||
| 179 | my($score, $i) = $self->_find_next_item(); | ||
| 180 | |||
| 181 | %{$f} = (); | ||
| 182 | |||
| 183 | if ($self->{'debug'} > 150) { | ||
| 184 | print "Next item: ", $score, Dumper($i); | ||
| 185 | } | ||
| 186 | if (!$i) { | ||
| 187 | return 0; | ||
| 188 | } | ||
| 189 | my($context) = $self->_incorporate_item($i); | ||
| 190 | |||
| 191 | $self->_dump_hash($context, $f); | ||
| 192 | return 1; | ||
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | =pod | ||
| 197 | |||
| 198 | =item $self->close() | ||
| 199 | |||
| 200 | Closes off the string | ||
| 201 | |||
| 202 | =cut | ||
| 203 | |||
| 204 | sub close { | ||
| 205 | my ($self) = @_; | ||
| 206 | } | ||
| 207 | |||
| 208 | =pod | ||
| 209 | |||
| 210 | =item $self->capture($field, $regexp) | ||
| 211 | |||
| 212 | Captures a string that is outside of an entry and inserts it into an entry hash. The | ||
| 213 | routine takes two arguments. One is the field that the entry hash is to be included | ||
| 214 | in. The other is the regular expression which matches the field. | ||
| 215 | |||
| 216 | =cut | ||
| 217 | |||
| 218 | sub capture { | ||
| 219 | my ($self, $capture_field, $capture_regexp) = @_; | ||
| 220 | $self->{'capture_field'} = $capture_field; | ||
| 221 | $self->{'capture_regexp'} = $capture_regexp; | ||
| 222 | $self->{'capture_string'} = ""; | ||
| 223 | } | ||
| 224 | |||
| 225 | |||
| 226 | =pod | ||
| 227 | |||
| 228 | =item $self->debug(i) | ||
| 229 | |||
| 230 | Set the debug level. Higher numbers turn on more debug levels. | ||
| 231 | |||
| 232 | =cut | ||
| 233 | |||
| 234 | sub debug { | ||
| 235 | my($self, $debug) = @_; | ||
| 236 | if (defined($debug)) { | ||
| 237 | $self->{'debug'} = $debug; | ||
| 238 | } | ||
| 239 | return $self->{'debug'}; | ||
| 240 | } | ||
| 241 | |||
| 242 | =pod | ||
| 243 | |||
| 244 | =item $self->expand_hrefs(i) | ||
| 245 | |||
| 246 | If set to one then expand out the internal A tags and IMG tags to | ||
| 247 | moving the href outside the tag. | ||
| 248 | |||
| 249 | =cut | ||
| 250 | |||
| 251 | sub expand_hrefs { | ||
| 252 | my($self, $d1) = @_; | ||
| 253 | if (defined($d1)) { | ||
| 254 | $self->{'expand_hrefs'} = $d1; | ||
| 255 | } | ||
| 256 | return $self->{'expand_hrefs'}; | ||
| 257 | } | ||
| 258 | |||
| 259 | =pod | ||
| 260 | |||
| 261 | =item $self->start_tags(i) | ||
| 262 | |||
| 263 | Set to the number of tags to match at the beginning of each entry. | ||
| 264 | Default is 2. | ||
| 265 | |||
| 266 | =cut | ||
| 267 | |||
| 268 | sub start_tags { | ||
| 269 | my($self, $d1) = @_; | ||
| 270 | if (defined($d1)) { | ||
| 271 | $self->{'start_tags'} = $d1; | ||
| 272 | } | ||
| 273 | return $self->{'start_tags'}; | ||
| 274 | } | ||
| 275 | |||
| 276 | |||
| 277 | =pod | ||
| 278 | |||
| 279 | =item $self->end_tags(i) | ||
| 280 | |||
| 281 | Number of tags to match at the end of an entry, The default is 1. | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | sub end_tags { | ||
| 286 | my($self, $d1) = @_; | ||
| 287 | if (defined($d1)) { | ||
| 288 | $self->{'end_tags'} = $d1; | ||
| 289 | } | ||
| 290 | return $self->{'end_tags'}; | ||
| 291 | } | ||
| 292 | |||
| 293 | |||
| 294 | =pod | ||
| 295 | =item $self->exact_tables(i) | ||
| 296 | |||
| 297 | If set to one then match table tags exactly. Otherwise ignore internal | ||
| 298 | table items. | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | sub exact_tables { | ||
| 303 | my($self, $d1) = @_; | ||
| 304 | if (defined($d1)) { | ||
| 305 | $self->{'exact_tables'} = $d1; | ||
| 306 | } | ||
| 307 | return $self->{'exact_tables'}; | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | sub process { | ||
| 312 | my ($self, $lp) = @_; | ||
| 313 | my (%f); | ||
| 314 | $self->open($lp); | ||
| 315 | |||
| 316 | while ($self->read(\%f)) { | ||
| 317 | my ($item); | ||
| 318 | foreach $item (keys %f) { | ||
| 319 | print "$item $f{$item}\n"; | ||
| 320 | } | ||
| 321 | print "\n"; | ||
| 322 | } | ||
| 323 | $self->close(); | ||
| 324 | } | ||
| 325 | |||
| 326 | sub tokenize { | ||
| 327 | my($self, $lp) = @_; | ||
| 328 | my($i) = $lp; | ||
| 329 | my (@match_token) = (); | ||
| 330 | my(@imatch_token) = (); | ||
| 331 | |||
| 332 | while ($i =~ m/\[\[\[([^\]]+)\]\]\]/s) { | ||
| 333 | if ($self->{'debug'} > 1000) { | ||
| 334 | print "added $1 to match tokens\n"; | ||
| 335 | } | ||
| 336 | push(@match_token, $1); | ||
| 337 | $i = $POSTMATCH; | ||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | $i = $lp; | ||
| 342 | while ($i =~ m/\{\{\{([^\}]+)\}\}\}/s) { | ||
| 343 | if ($self->{'debug'} > 1000) { | ||
| 344 | print "added $1 to imatch tokens\n"; | ||
| 345 | } | ||
| 346 | push(@imatch_token, $1); | ||
| 347 | $i = $POSTMATCH; | ||
| 348 | } | ||
| 349 | |||
| 350 | if ($self->{'grammar'} ne undef) { | ||
| 351 | foreach $i (@{$self->{'grammar'}}) { | ||
| 352 | if ($i =~ m/\[\[\[([^\]]+)\]\]\]/s) {push(@match_token, $1);} | ||
| 353 | if ($i =~ m/\{\{\{([^\}]+)\}\}\}/s) {push(@imatch_token, $1);} | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | if ($self->{'debug'} > 500) { | ||
| 358 | print Dumper(\@match_token); | ||
| 359 | print Dumper(\@imatch_token); | ||
| 360 | } | ||
| 361 | |||
| 362 | $lp =~ s/\[\[\[(.*?)\]\]\]/$1/gs; | ||
| 363 | $lp =~ s/\{\{\{(.*?)\}\}\}/$1/gs; | ||
| 364 | |||
| 365 | foreach $i (@match_token) { | ||
| 366 | my ($iin) = $i; | ||
| 367 | $iin =~ s/([\[\(\)\]])/\\$1/gs; | ||
| 368 | $lp =~ s/$iin/[[[$i]]]/gs; | ||
| 369 | } | ||
| 370 | |||
| 371 | foreach $i (@imatch_token) { | ||
| 372 | my ($iin) = $i; | ||
| 373 | $iin =~ s/([\[\(\)\]])/\\$1/gs; | ||
| 374 | $lp =~ s/$iin/{{{$i}}}/gs; | ||
| 375 | } | ||
| 376 | |||
| 377 | push(@match_token, @imatch_token); | ||
| 378 | my ($insert_string) = ""; | ||
| 379 | if ($self->{'capture_regexp'} ne "") { | ||
| 380 | $insert_string = $self->{'capture_regexp'} . "|"; | ||
| 381 | } else { | ||
| 382 | $insert_string = ""; | ||
| 383 | } | ||
| 384 | $lp =~ s/ / /gi; | ||
| 385 | my (@lp) = split(/\s*(${insert_string}\(\(\([^\)]+?\)\)\)|\[\[\[[^\]]+?\]\]\]|\{\{\{[^\}]+?\}\}\}|<[^>]+>|\-\s+|\n\s+|\&\#183|\;|\|)\s*/, $lp); | ||
| 386 | @lp = grep {$_ ne ""} @lp; | ||
| 387 | return \@lp; | ||
| 388 | } | ||
| 389 | |||
| 390 | sub _classify { | ||
| 391 | my ($self, $item) = @_; | ||
| 392 | if ($item =~ /^\s+$/is) { | ||
| 393 | return "B"; | ||
| 394 | } | ||
| 395 | |||
| 396 | $item =~ s/\s+$//i; | ||
| 397 | if ($item =~ /^<t.*>$/is && $self->{'exact_tables'}) { | ||
| 398 | return lc($item); | ||
| 399 | } | ||
| 400 | if ($item =~ /^\[\[\[.*?\]\]\]$/s) { | ||
| 401 | return $item; | ||
| 402 | } | ||
| 403 | |||
| 404 | if ($item =~ /^\(\(\(.*?\)\)\)$/s) { | ||
| 405 | return $item; | ||
| 406 | } | ||
| 407 | |||
| 408 | if ($item =~ /^\{\{\{.*?\}\}\}$/s) { | ||
| 409 | return $item; | ||
| 410 | } | ||
| 411 | |||
| 412 | if ($item =~ /^<([^>\s]+)\s*.*>$/is) { | ||
| 413 | return "<" . lc($1) . ">"; | ||
| 414 | } | ||
| 415 | if ($item =~ /^\(/) { | ||
| 416 | return "("; | ||
| 417 | } | ||
| 418 | if ($item =~ /^&/) { | ||
| 419 | return $item; | ||
| 420 | } | ||
| 421 | if ($item =~ /^\)/) { | ||
| 422 | return ")"; | ||
| 423 | } | ||
| 424 | if ($item =~ /^\|/) { | ||
| 425 | return "|"; | ||
| 426 | } | ||
| 427 | if ($item =~ /^\;/) { | ||
| 428 | return ";"; | ||
| 429 | } | ||
| 430 | return "C"; | ||
| 431 | } | ||
| 432 | |||
| 433 | sub extract_grammar { | ||
| 434 | my ($self, $ap) = @_; | ||
| 435 | if ($self->{'debug'} > 50) { | ||
| 436 | print "initializing\n"; | ||
| 437 | } | ||
| 438 | my ($start, $finish) = $self->_find_indices($ap, [q/(((BEGIN)))/, | ||
| 439 | q/(((END)))/]); | ||
| 440 | |||
| 441 | if ($self->{'debug'} > 100) { | ||
| 442 | print "start finish $start $finish\n"; | ||
| 443 | } | ||
| 444 | my (@out) = @{$ap}[($start+1)..($finish-1)]; | ||
| 445 | my (@out_grammar) = @out; | ||
| 446 | return (\@out_grammar, \@out); | ||
| 447 | } | ||
| 448 | |||
| 449 | |||
| 450 | sub _edit_distance { | ||
| 451 | my ($self, $s1, $s2) = @_; | ||
| 452 | my ($item, @s1p, @s2p); | ||
| 453 | @s1p = grep {$self->_classify($_) !~ /\(\(\(.*\)\)\)/} @$s1; | ||
| 454 | @s2p = grep {$self->_classify($_) !~ /\(\(\(.*\)\)\)/} @$s2; | ||
| 455 | |||
| 456 | my ($m) = $self->_edit_distance_matrix(\@s1p, \@s2p); | ||
| 457 | my (@a) = $m->dims(); | ||
| 458 | return $m->at($a[0] - 1, $a[1] - 1); | ||
| 459 | } | ||
| 460 | |||
| 461 | sub _edit_distance_matrix { | ||
| 462 | my ($self, $s1p, $s2p) = @_; | ||
| 463 | if ($self->{'debug'} > 10) { | ||
| 464 | print "**** Edit distance matrix " . | ||
| 465 | (scalar(@$s1p) + 1) . " by " . (scalar(@$s2p) + 1) . "\n"; | ||
| 466 | } | ||
| 467 | my ($j, $i); | ||
| 468 | my ($m) = PDL->zeroes (scalar(@$s1p) + 1, scalar(@$s2p) + 1); | ||
| 469 | PDL::set($m, 0, 0, 0); | ||
| 470 | for ($j=1; $j <= scalar(@$s2p); $j++) { | ||
| 471 | PDL::set ($m, 0, $j, $m->at(0, $j-1) - 0 + 1); | ||
| 472 | } | ||
| 473 | for ($i=1; $i <= scalar(@$s1p) ; $i++) { | ||
| 474 | PDL::set ($m, $i, 0, $m->at($i-1, 0) - 0 +1); | ||
| 475 | for ($j=1; $j <= scalar(@$s2p) ; $j++) { | ||
| 476 | |||
| 477 | my ($diag) = $m->at($i-1, $j-1); | ||
| 478 | if ($self->_classify($s1p->[$i-1]) ne | ||
| 479 | $self->_classify($s2p->[$j-1])) { | ||
| 480 | $diag++; | ||
| 481 | } | ||
| 482 | my ($item) = $diag; | ||
| 483 | if ($item > $m->at($i-1, $j) + 1) { | ||
| 484 | $item = $m->at($i-1, $j) + 1; | ||
| 485 | } | ||
| 486 | if ($item > $m->at($i, $j-1) + 1) { | ||
| 487 | $item = $m->at($i, $j-1) + 1; | ||
| 488 | } | ||
| 489 | PDL::set($m, $i, $j, $item); | ||
| 490 | } | ||
| 491 | } | ||
| 492 | return $m; | ||
| 493 | } | ||
| 494 | |||
| 495 | sub _find_next_item { | ||
| 496 | my ($self) = @_; | ||
| 497 | my ($ap) = $self->{'tokens'}; | ||
| 498 | my ($g) = $self->{'grammar'}; | ||
| 499 | my ($dnewlocal, $dlocal, $dbest) = | ||
| 500 | (998, 999, 1000); | ||
| 501 | my ($ib, $ie, $newib) = (-1, -1, -1); | ||
| 502 | my ($local_best_item, $best_item) = | ||
| 503 | ("", ""); | ||
| 504 | my (@gprocessed) = grep {$self->_classify($_) !~ /\(\(\(.*\)\)\)/} @{$g}; | ||
| 505 | my (@start_tags) = @gprocessed[0..($self->{'start_tags'})-1]; | ||
| 506 | my ($glim) = scalar(@gprocessed); | ||
| 507 | my (@end_tags) = @gprocessed[$glim-$self->{'end_tags'}..$glim-1]; | ||
| 508 | |||
| 509 | while ($dlocal < $dbest) { | ||
| 510 | $dbest = $dlocal; | ||
| 511 | |||
| 512 | if ($self->{'debug'} > 200) { | ||
| 513 | print "Start find indices for ", | ||
| 514 | Dumper(\@start_tags), "\n"; | ||
| 515 | } | ||
| 516 | if ($self->{'debug'} > 1000) { | ||
| 517 | print "Match with ", | ||
| 518 | Dumper($ap), "\n"; | ||
| 519 | } | ||
| 520 | |||
| 521 | ($newib) = $self->_find_indices ($ap, [\@start_tags], $ib+1); | ||
| 522 | if ($self->{'debug'} > 500) { | ||
| 523 | print "Newib: $newib\n"; | ||
| 524 | } | ||
| 525 | if ($newib < 0) { | ||
| 526 | last; | ||
| 527 | } | ||
| 528 | $ie = $newib; | ||
| 529 | while(1) { | ||
| 530 | if ($self->{'debug'} > 500) { | ||
| 531 | print "Entering loop starting at $ie. Searching for ", | ||
| 532 | Dumper(\@end_tags), "\n"; | ||
| 533 | } | ||
| 534 | my($newie) = $self->_find_indices($ap, [\@end_tags], $ie+1); | ||
| 535 | if ($self->{'debug'} > 500) { | ||
| 536 | print "newie: $newie\n"; | ||
| 537 | } | ||
| 538 | if ($newie < 0) { | ||
| 539 | last; | ||
| 540 | } | ||
| 541 | $dlocal = $dnewlocal; | ||
| 542 | |||
| 543 | my(@new_local_best_item) = | ||
| 544 | @{$ap}[$newib .. $newie]; | ||
| 545 | my($new_local_best_item) = | ||
| 546 | \@new_local_best_item; | ||
| 547 | if ($self->{'debug'} > 250) { | ||
| 548 | print Dumper($new_local_best_item); | ||
| 549 | } | ||
| 550 | |||
| 551 | $dnewlocal = $self->_edit_distance($g, | ||
| 552 | $new_local_best_item); | ||
| 553 | if ($self->{'debug'} > 150) { | ||
| 554 | print "Edit distance ", $dnewlocal, "\n"; | ||
| 555 | } | ||
| 556 | if ($dnewlocal > $dlocal) { | ||
| 557 | last; | ||
| 558 | } | ||
| 559 | $ie = $newie; | ||
| 560 | if ($self->{'debug'} > 500) { | ||
| 561 | print "ie: $ie\n"; | ||
| 562 | } | ||
| 563 | |||
| 564 | $local_best_item = $new_local_best_item; | ||
| 565 | } | ||
| 566 | $best_item = $local_best_item; | ||
| 567 | } | ||
| 568 | |||
| 569 | my (@ap) = @$ap; | ||
| 570 | my ($scan_capture) = ""; | ||
| 571 | $self->{'capture_string'} = ""; | ||
| 572 | foreach $scan_capture (@ap[0..$ie]) { | ||
| 573 | if ($scan_capture =~ m!$self->{'capture_regexp'}!) { | ||
| 574 | if ($self->{'debug'} > 50) { | ||
| 575 | print "Capturing $1\n"; | ||
| 576 | } | ||
| 577 | $self->{'capture_string'} = | ||
| 578 | $1; | ||
| 579 | } | ||
| 580 | } | ||
| 581 | |||
| 582 | @{$ap} = @ap[($ie+1)..$#ap]; | ||
| 583 | return ($dbest, $best_item); | ||
| 584 | } | ||
| 585 | |||
| 586 | sub _incorporate_item { | ||
| 587 | my ($self, $item) = @_; | ||
| 588 | my ($g) = $self->{'grammar'}; | ||
| 589 | my ($i, $j, $gitem, $iprocess); | ||
| 590 | my (@greturn) = (); | ||
| 591 | my (@gprocessed) = (); | ||
| 592 | my (@lbrace) = (); | ||
| 593 | my (@rbrace) = (); | ||
| 594 | my (@tag) = (); | ||
| 595 | my ($ginopt) = (0, 0); | ||
| 596 | my ($newginopt) = 0; | ||
| 597 | my(@g) = @{$g}; | ||
| 598 | my(@item) = grep {$self->_classify($_) !~ /\(\(\(.*?\)\)\)/} @{$item}; | ||
| 599 | if ($self->{'debug'} > 10) { | ||
| 600 | print Dumper(\@g); | ||
| 601 | } | ||
| 602 | |||
| 603 | $iprocess = 0; | ||
| 604 | foreach $gitem (@g) { | ||
| 605 | if ($gitem =~ /\(\(\((.*?)\)\)\)/) { | ||
| 606 | $tag[$iprocess] = $gitem; | ||
| 607 | } else { | ||
| 608 | push (@gprocessed, $gitem); | ||
| 609 | $iprocess++; | ||
| 610 | } | ||
| 611 | } | ||
| 612 | my (@gblock, @iblock, $addtag); | ||
| 613 | my ($m) = $self->_edit_distance_matrix(\@gprocessed, \@item); | ||
| 614 | if ($self->{'debug'} > 10) { | ||
| 615 | print $m, "\n"; | ||
| 616 | } | ||
| 617 | $i = scalar(@gprocessed); | ||
| 618 | $j = scalar(@item); | ||
| 619 | |||
| 620 | while ($i > 0 && $j > 0) { | ||
| 621 | my ($direction) = ""; | ||
| 622 | my ($dump_block) = 0; | ||
| 623 | my ($oldi) = $i; | ||
| 624 | if ($m->at($i-1, $j) + 1 == $m->at($i, $j)) { | ||
| 625 | $direction = "w"; | ||
| 626 | $i--; | ||
| 627 | if ($tag[$oldi]) { | ||
| 628 | @greturn = ($tag[$oldi], @greturn); | ||
| 629 | } | ||
| 630 | |||
| 631 | } elsif ($m->at($i, $j-1) + 1 == $m->at($i, $j)) { | ||
| 632 | $direction = "n"; | ||
| 633 | $j--; | ||
| 634 | @greturn = ($item[$j], @greturn); | ||
| 635 | } elsif ($m->at($i-1, $j-1) + 1 == $m->at($i, $j)) { | ||
| 636 | $direction = "nw"; | ||
| 637 | $i--; | ||
| 638 | $j--; | ||
| 639 | if ($tag[$oldi]) { | ||
| 640 | @greturn = ($tag[$oldi], @greturn); | ||
| 641 | } | ||
| 642 | @greturn = ($item[$j], @greturn); | ||
| 643 | } elsif ($m->at($i-1, $j-1) == $m->at($i, $j) && | ||
| 644 | $self->_classify($gprocessed[$i-1]) eq | ||
| 645 | $self->_classify($item[$j-1])) { | ||
| 646 | $direction = "eq"; | ||
| 647 | $i--; | ||
| 648 | $j--; | ||
| 649 | if ($tag[$oldi]) { | ||
| 650 | @greturn = ($tag[$oldi], @greturn); | ||
| 651 | } | ||
| 652 | @greturn = ($item[$j], @greturn); | ||
| 653 | } else { | ||
| 654 | print "ERROR:"; | ||
| 655 | } | ||
| 656 | } | ||
| 657 | |||
| 658 | return (\@greturn); | ||
| 659 | } | ||
| 660 | |||
| 661 | sub _find_indices { | ||
| 662 | my ($self, $list, $itemref, $index) = @_; | ||
| 663 | my ($i, $j, @out); | ||
| 664 | my ($current_item) = 0; | ||
| 665 | |||
| 666 | if ($self->{'debug'} > 100) { | ||
| 667 | print "start find index\n index: ", $index, | ||
| 668 | Dumper($itemref), "\n"; | ||
| 669 | } | ||
| 670 | |||
| 671 | if ($index eq undef) { | ||
| 672 | $index = 0; | ||
| 673 | } | ||
| 674 | |||
| 675 | loop: | ||
| 676 | for ($i=$index; $i < scalar(@$list); $i++) { | ||
| 677 | if (ref($itemref->[$current_item]) eq "ARRAY") { | ||
| 678 | for ($j = 0; $j < scalar(@{$itemref->[$current_item]}); $j++) { | ||
| 679 | if (($i + $j) >= scalar(@$list)) { | ||
| 680 | next loop; | ||
| 681 | } | ||
| 682 | if ($self->{'debug'} > 1024) { | ||
| 683 | print "Comparing ", | ||
| 684 | $itemref->[$current_item]->[$j], | ||
| 685 | " and ", $list->[$i + $j], " at $i $j\n"; | ||
| 686 | } | ||
| 687 | if ($self->_classify($itemref->[$current_item]->[$j]) | ||
| 688 | ne $self->_classify($list->[$i + $j])) { | ||
| 689 | next loop; | ||
| 690 | } | ||
| 691 | } | ||
| 692 | if ($self->{'debug'} > 500) { | ||
| 693 | print "Match at $i\n"; | ||
| 694 | } | ||
| 695 | push (@out, $i); | ||
| 696 | $current_item++; | ||
| 697 | if ($current_item > scalar(@{$itemref})) { | ||
| 698 | last loop; | ||
| 699 | } | ||
| 700 | } else { | ||
| 701 | if ($self->{'debug'} > 1024) { | ||
| 702 | print "Comparing ", | ||
| 703 | $itemref->[$current_item], | ||
| 704 | " and ", $list->[$i], " at $i\n"; | ||
| 705 | } | ||
| 706 | |||
| 707 | if ($self->_classify($list->[$i]) eq | ||
| 708 | $self->_classify($itemref->[$current_item])) { | ||
| 709 | push (@out, $i); | ||
| 710 | $current_item++; | ||
| 711 | if ($current_item > scalar(@{$itemref})) { | ||
| 712 | last loop; | ||
| 713 | } | ||
| 714 | } | ||
| 715 | } | ||
| 716 | } | ||
| 717 | for ($i=$current_item; $i <= scalar(@{$itemref}); $i++) { | ||
| 718 | push (@out, -1); | ||
| 719 | } | ||
| 720 | return @out; | ||
| 721 | } | ||
| 722 | |||
| 723 | sub _dump_hash { | ||
| 724 | my ($self, $context, $r) = @_; | ||
| 725 | if ($self->{'debug'} > 200) { | ||
| 726 | print "Dumping: "; | ||
| 727 | print Dumper($context); | ||
| 728 | } | ||
| 729 | my ($item); | ||
| 730 | my ($dump) = 1; | ||
| 731 | my ($current_field) = ""; | ||
| 732 | my ($returnval) = ""; | ||
| 733 | |||
| 734 | foreach $item (@{$context}) { | ||
| 735 | my ($class) = $self->_classify($item); | ||
| 736 | if ($class eq "(((nodump)))") { | ||
| 737 | $dump = 0; | ||
| 738 | } | ||
| 739 | if ($self->{'debug'} > 500) { | ||
| 740 | print "Item: ", $item, "\n"; | ||
| 741 | print "Class: ", $class, "\n"; | ||
| 742 | print "\n\n"; | ||
| 743 | } | ||
| 744 | |||
| 745 | if ($dump) { | ||
| 746 | if ($class =~ /\(\(\((.*?)\)\)\)/) { | ||
| 747 | my($tag) = $1; | ||
| 748 | if ($current_field ne "") { | ||
| 749 | $returnval =~ s/\s+$//gi; | ||
| 750 | $r->{$current_field} = $returnval; | ||
| 751 | $returnval = ""; | ||
| 752 | } | ||
| 753 | $current_field = $tag; | ||
| 754 | } elsif ($class =~ /\[\[\[(.*?)\]\]\]/) { | ||
| 755 | $returnval .= "$1"; | ||
| 756 | } elsif ($class =~ /<a>/) { | ||
| 757 | if ($item =~ /href/i && $self->{'expand_hrefs'}) { | ||
| 758 | if ($self->{'debug'} > 500) { | ||
| 759 | print "Expand hrefs\n"; | ||
| 760 | } | ||
| 761 | $item =~ /href=\"(.*?)\"/i; | ||
| 762 | $returnval .= " $1 "; | ||
| 763 | } | ||
| 764 | } elsif ($class =~ /<img>/) { | ||
| 765 | if ($item =~ /src/i && | ||
| 766 | $self->{'expand_hrefs'}) { | ||
| 767 | if ($self->{'debug'} > 500) { | ||
| 768 | print "Expand hrefs\n"; | ||
| 769 | } | ||
| 770 | $item =~ /src=\"(.*?)\"/i; | ||
| 771 | $returnval .= " $1 "; | ||
| 772 | } | ||
| 773 | } elsif ($class eq "C") { | ||
| 774 | $item =~ s/\n/\n /gis; | ||
| 775 | $returnval .= $item; | ||
| 776 | } elsif ($class =~ /<.*?>/) { | ||
| 777 | $returnval .= " "; | ||
| 778 | } elsif ($class eq "B") { | ||
| 779 | $returnval .= " "; | ||
| 780 | } elsif ($class !~ /\{\{\{(.*?)\}\}\}/s) { | ||
| 781 | $returnval .= $item; | ||
| 782 | } | ||
| 783 | } | ||
| 784 | if ($class eq "(((/nodump)))") { | ||
| 785 | $dump = 1; | ||
| 786 | } | ||
| 787 | } | ||
| 788 | if ($current_field ne "") { | ||
| 789 | $returnval =~ s/\s+$//gi; | ||
| 790 | $r->{$current_field} = $returnval; | ||
| 791 | } | ||
| 792 | if ($self->{'capture_field'} ne "" && | ||
| 793 | $self->{'capture_string'} ne "") { | ||
| 794 | $r->{$self->{'capture_field'}} = | ||
| 795 | $self->{'capture_string'}; | ||
| 796 | } | ||
| 797 | } | ||
| 798 | =pod | ||
| 799 | |||
| 800 | =back | ||
| 801 | |||
| 802 | =head1 EXAMPLES | ||
| 803 | |||
| 804 | The distribution contains a sample driver application and test data in | ||
| 805 | the examples directory. To look at the markup for the test data, | ||
| 806 | search for the (((BEGIN))) tag. | ||
| 807 | |||
| 808 | =head2 Direct mode | ||
| 809 | |||
| 810 | To run | ||
| 811 | |||
| 812 | ./learn.wrapper < ./sample1.html | ||
| 813 | |||
| 814 | To run an example with --expand-hrefs | ||
| 815 | |||
| 816 | ./learn.wrapper --expand-hrefs < ./sample1.html | ||
| 817 | |||
| 818 | =head2 Saving a grammar | ||
| 819 | |||
| 820 | To save off a grammar | ||
| 821 | |||
| 822 | ./learn.wrapper --extract-grammar < ./sample1.html > /tmp/saved.grammar | ||
| 823 | |||
| 824 | You can then use this grammar to do future parsing | ||
| 825 | |||
| 826 | ./learn.wrapper --load-grammar /tmp/saved.grammar < ./sample1.html | ||
| 827 | |||
| 828 | =head1 DISCUSSION AND DEVELOPMENT | ||
| 829 | |||
| 830 | A wiki on this module is located at | ||
| 831 | |||
| 832 | http://www.gnacademy.org/twiki/bin/view/Gna/AutomatedDataExtraction | ||
| 833 | |||
| 834 | Please contact gna@gnacademy.org for ideas on improvements. | ||
| 835 | |||
| 836 | =head1 COPYRIGHT AND LICENSE | ||
| 837 | |||
| 838 | Copyright (C) 2002-2004 Globewide Network Academy | ||
| 839 | |||
| 840 | Redistributed under the terms of the Lesser GNU Public License | ||
| 841 | |||
| 842 | =cut |