| 1 | package Text::Corpus::NewYorkTimes; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use File::Spec::Functions; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use File::Find; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Log::Log4perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Text::Corpus::NewYorkTimes::Document; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Data::Dump qw(dump); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | BEGIN | ||
| 12 | 1 | ▶ | { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Exporter (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | $VERSION = '1.01'; | |
| 16 | 1 | @ISA = qw(Exporter); | |
| 17 | 1 | @EXPORT = qw(); | |
| 18 | 1 | @EXPORT_OK = qw(); | |
| 19 | 1 | %EXPORT_TAGS = (); | |
| 20 | } | ||
| 21 | # To do: | ||
| 22 | # TODO: add method to write file list to a file (make it a script). | ||
| 23 | |||
| 24 | #12345678901234567890123456789012345678901234 | ||
| 25 | #Interface to New York Times corpus. | ||
| 26 | |||
| 27 | =head1 NAME | ||
| 28 | |||
| 29 | C<Text::Corpus::NewYorkTimes> - Interface to New York Times corpus. | ||
| 30 | |||
| 31 | =head1 SYNOPSIS | ||
| 32 | |||
| 33 | use Text::Corpus::NewYorkTimes; | ||
| 34 | use Data::Dump qw(dump); | ||
| 35 | use Log::Log4perl qw(:easy); | ||
| 36 | Log::Log4perl->easy_init ($INFO); | ||
| 37 | my $corpus = Text::Corpus::NewYorkTimes->new (fileList => $fileList, corpusDirectory => $corpusDirectory); | ||
| 38 | dump $corpus->getTotalDocuments; | ||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | C<Text::Corpus::NewYorkTimes> provides an interface for accessing the documents | ||
| 43 | in the New York Times corpus from Linguistic Data Consortium. The categories, description, title, etc... of a specified document | ||
| 44 | are accessed using L<Text::Corpus::NewYorkTimes::Document>. Also, all errors and | ||
| 45 | warnings are logged using L<Log::Log4perl>, which should be L<initialized|Log::Log4perl/How_to_use_it>. | ||
| 46 | |||
| 47 | =head1 CONSTRUCTOR | ||
| 48 | |||
| 49 | =head2 C<new> | ||
| 50 | |||
| 51 | The method C<new> creates an instance of the C<Text::Corpus::NewYorkTimes> class with the following | ||
| 52 | parameters: | ||
| 53 | |||
| 54 | =over | ||
| 55 | |||
| 56 | =item C<corpusDirectory> | ||
| 57 | |||
| 58 | corpusDirectory => '...' | ||
| 59 | |||
| 60 | C<corpusDirectory> is the path to the top most directory of the corpus; | ||
| 61 | it usually is the path to the directory named C<nyt_corpus>. It is needed to | ||
| 62 | locate all the documents in the corpus. If it is not defined, then the enviroment variable | ||
| 63 | C<TEXT_CORPUS_NEWYORKTIMES_CORPUSDIRECTORY> is used if it is defined; if neither of these are | ||
| 64 | defined then all the paths in the | ||
| 65 | file specified by C<fileList> are assumed to be full path names. C<corpusDirectory> and | ||
| 66 | C<fileList> can both be defined to locate the documents in the corpus by having the path | ||
| 67 | names in C<fileList> be defined relative to C<corpusDirectory>. | ||
| 68 | |||
| 69 | =item C<fileList> | ||
| 70 | |||
| 71 | fileList => '...' | ||
| 72 | |||
| 73 | C<fileList> is an optional parameter that can be used to save time when creating the list of documents in the corpus; | ||
| 74 | each line in the file must be the path to an XML document in the corpus. | ||
| 75 | If C<fileList> is not defined, then the environment variable C<TEXT_CORPUS_NEWYORKTIMES_FILELIST> is used if it is defined; | ||
| 76 | otherwise all the XML documents in the corpus are located by searching the directory specified by C<corpusDirectory>. | ||
| 77 | If the file defined by C<fileList> or C<TEXT_CORPUS_NEWYORKTIMES_FILELIST> does | ||
| 78 | not exist, it will be created and the path to each XML document in the corpus, | ||
| 79 | relative to C<corpusDirectory>, will be written to it. This is done to speed-up | ||
| 80 | subsequent invocations of the object. | ||
| 81 | |||
| 82 | =back | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub new | ||
| 87 | { | ||
| 88 | # create the class object. | ||
| 89 | my ($Class, %Parameters) = @_; | ||
| 90 | my $Self = bless {}, ref($Class) || $Class; | ||
| 91 | |||
| 92 | # set the fileList. | ||
| 93 | unless (exists ($Parameters{fileList})) | ||
| 94 | { | ||
| 95 | if (defined (%ENV) && exists ($ENV{TEXT_CORPUS_NEWYORKTIMES_FILELIST})) | ||
| 96 | { | ||
| 97 | $Parameters{fileList} = $ENV{TEXT_CORPUS_NEWYORKTIMES_FILELIST}; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | # set the corpusDirectory. | ||
| 102 | unless (exists ($Parameters{corpusDirectory})) | ||
| 103 | { | ||
| 104 | if (defined (%ENV) && exists ($ENV{TEXT_CORPUS_NEWYORKTIMES_CORPUSDIRECTORY})) | ||
| 105 | { | ||
| 106 | $Parameters{corpusDirectory} = $ENV{TEXT_CORPUS_NEWYORKTIMES_CORPUSDIRECTORY}; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | my $corpusFileList; | ||
| 111 | if (!exists($Parameters{fileList}) || !defined($Parameters{fileList}) || | ||
| 112 | (exists ($Parameters{fileList}) && defined ($Parameters{fileList}) && ! -e $Parameters{fileList})) | ||
| 113 | { | ||
| 114 | # at this point no file containing the list of files exists, so find all the files. | ||
| 115 | |||
| 116 | # corpusDirectory not defined, log and die. | ||
| 117 | unless (exists($Parameters{corpusDirectory})) | ||
| 118 | { | ||
| 119 | my $logger = Log::Log4perl->get_logger(); | ||
| 120 | $logger->logdie ("corpusDirectory not defined.\n"); | ||
| 121 | } | ||
| 122 | |||
| 123 | # if corpusDirectory not a directory, log and die. | ||
| 124 | unless (-d $Parameters{corpusDirectory}) | ||
| 125 | { | ||
| 126 | my $logger = Log::Log4perl->get_logger(); | ||
| 127 | $logger->logdie("base directory for corpus, '" . $Parameters{corpusDirectory} . "', not a directory or does not exist: " . $!); | ||
| 128 | } | ||
| 129 | |||
| 130 | # store the directory of the corpus. | ||
| 131 | $Self->{corpusDirectory} = $Parameters{corpusDirectory}; | ||
| 132 | |||
| 133 | # find all the corpus files in the base directory. | ||
| 134 | $Self->_computeListOfCorpusFiles(corpusDirectory => $Parameters{corpusDirectory}); | ||
| 135 | |||
| 136 | # write the list of files to disk if fileList defined. | ||
| 137 | if (exists ($Parameters{fileList}) && defined ($Parameters{fileList}) && ! -e $Parameters{fileList}) | ||
| 138 | { | ||
| 139 | $Self->{fileList} = $Parameters{fileList}; | ||
| 140 | $Self->_writeListOfCorpusFiles; | ||
| 141 | } | ||
| 142 | |||
| 143 | $corpusFileList = $Self->{corpusFileList}; | ||
| 144 | } | ||
| 145 | else | ||
| 146 | { | ||
| 147 | # at this point fileList exists, so make sure it is a path to a file. | ||
| 148 | my $fileList = $Parameters{fileList}; | ||
| 149 | unless (-f $fileList) | ||
| 150 | { | ||
| 151 | my $logger = Log::Log4perl->get_logger(); | ||
| 152 | $logger->logdie("'$fileList' is not a file."); | ||
| 153 | } | ||
| 154 | |||
| 155 | # open the file of paths for reading. | ||
| 156 | local *IN; | ||
| 157 | unless (open(IN, $fileList)) | ||
| 158 | { | ||
| 159 | my $logger = Log::Log4perl->get_logger(); | ||
| 160 | $logger->logdie("could not open file '" . $fileList . ". for reading: " . $!); | ||
| 161 | } | ||
| 162 | |||
| 163 | # read in the list of file paths and close the file. | ||
| 164 | my @filePathName = map { chomp; $_; } <IN>; | ||
| 165 | close IN; | ||
| 166 | |||
| 167 | # if there are no files, log a warning now. | ||
| 168 | if (@filePathName == 0) | ||
| 169 | { | ||
| 170 | my $logger = Log::Log4perl->get_logger(); | ||
| 171 | $logger->logwarn ("file '" . $fileList . ". is empty.\n"); | ||
| 172 | } | ||
| 173 | |||
| 174 | # add the base directory to each path name. | ||
| 175 | if (exists($Parameters{corpusDirectory})) | ||
| 176 | { | ||
| 177 | unless (-d $Parameters{corpusDirectory}) | ||
| 178 | { | ||
| 179 | my $logger = Log::Log4perl->get_logger(); | ||
| 180 | $logger->logdie ("base directory for corpus, '" . $Parameters{corpusDirectory} . "', does not exist." . $!); | ||
| 181 | } | ||
| 182 | |||
| 183 | foreach my $filePath (@filePathName) | ||
| 184 | { | ||
| 185 | $filePath = File::Spec->catfile($Parameters{corpusDirectory}, $filePath); | ||
| 186 | } | ||
| 187 | } | ||
| 188 | else | ||
| 189 | { | ||
| 190 | # check if the first file exists, if not see if the file fileList is in the base directory. | ||
| 191 | if ((@filePathName > 0) && !(-e $filePathName[0])) | ||
| 192 | { | ||
| 193 | my ($volume, $path, $fileName) = File::Spec->splitpath($Parameters{fileList}); | ||
| 194 | my $maybeBaseDir = File::Spec->catfile($volume, $path); | ||
| 195 | if (-e File::Spec->catfile($maybeBaseDir, $filePathName[0])) | ||
| 196 | { | ||
| 197 | |||
| 198 | # looks like $maybeBaseDir is the base directory, so prefix it each file. | ||
| 199 | foreach my $filePath (@filePathName) | ||
| 200 | { | ||
| 201 | $filePath = File::Spec->catfile($maybeBaseDir, $filePath); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | } | ||
| 205 | } | ||
| 206 | |||
| 207 | $corpusFileList = \@filePathName; | ||
| 208 | } | ||
| 209 | |||
| 210 | $Self->{corpusFileList} = $corpusFileList; | ||
| 211 | $Self->buildCorpusFileHash; | ||
| 212 | return $Self; | ||
| 213 | } | ||
| 214 | |||
| 215 | =head1 METHODS | ||
| 216 | |||
| 217 | =head2 C<getDocument> | ||
| 218 | |||
| 219 | getDocument (index => $documentIndex) | ||
| 220 | getDocument (uri => $uri) | ||
| 221 | |||
| 222 | C<getDocument> returns a L<Text::Corpus::NewYorkTimes::Document> object for the | ||
| 223 | document with index C<$documentIndex> or uri C<$uri>. The document | ||
| 224 | indices range from zero to C<getTotalDocument()-1>; C<getDocument> returns | ||
| 225 | C<undef> if any errors occurred and logs them using L<Log::Log4perl>. | ||
| 226 | |||
| 227 | For example: | ||
| 228 | |||
| 229 | use Text::Corpus::NewYorkTimes; | ||
| 230 | use Data::Dump qw(dump); | ||
| 231 | use Log::Log4perl qw(:easy); | ||
| 232 | Log::Log4perl->easy_init ($INFO); | ||
| 233 | my $corpus = Text::Corpus::NewYorkTimes->new (fileList => $fileList, corpusDirectory => $corpusDirectory); | ||
| 234 | my $document = $corpus->getDocument (index => 0); | ||
| 235 | dump $document->getBody; | ||
| 236 | dump $document->getCategories; | ||
| 237 | dump $document->getContent; | ||
| 238 | dump $document->getDate; | ||
| 239 | dump $document->getTitle; | ||
| 240 | dump $document->getUri; | ||
| 241 | |||
| 242 | =cut | ||
| 243 | |||
| 244 | sub getDocument | ||
| 245 | { | ||
| 246 | my ($Self, %Parameters) = @_; | ||
| 247 | my $corpusFileHash = $Self->{corpusFileHash}; | ||
| 248 | |||
| 249 | # get the documents path. | ||
| 250 | my $documentPath; | ||
| 251 | $documentPath = $Self->getFilePathOfDocument ($Parameters{index}) if exists $Parameters{index}; | ||
| 252 | $documentPath = $Parameters{uri} if (exists $Parameters{uri} && exists $corpusFileHash->{$Parameters{uri}}); | ||
| 253 | return undef unless defined $documentPath; | ||
| 254 | |||
| 255 | # get the document object. | ||
| 256 | my $document; | ||
| 257 | eval | ||
| 258 | { | ||
| 259 | $document = Text::Corpus::NewYorkTimes::Document->new (filename => $documentPath); | ||
| 260 | }; | ||
| 261 | if ($@) | ||
| 262 | { | ||
| 263 | my $logger = Log::Log4perl->get_logger(); | ||
| 264 | $logger->logwarn( "caught exception, probably xml parsing error in file '" | ||
| 265 | . $documentPath . "', skipping over the file: " . $@); | ||
| 266 | $document = undef; | ||
| 267 | } | ||
| 268 | return $document; | ||
| 269 | } | ||
| 270 | |||
| 271 | # find all the corpus files in the base directory. | ||
| 272 | { | ||
| 273 | my @corpusFileList; | ||
| 274 | |||
| 275 | sub _computeListOfCorpusFiles | ||
| 276 | { | ||
| 277 | my ($Self, %Parameters) = @_; | ||
| 278 | @corpusFileList = (); | ||
| 279 | find ({ wanted => \&_storeCorpusFiles, follow => 1 }, $Parameters{corpusDirectory}); | ||
| 280 | @corpusFileList = sort @corpusFileList; | ||
| 281 | $Self->{corpusFileList} = \@corpusFileList; | ||
| 282 | $Self->buildCorpusFileHash; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub _storeCorpusFiles | ||
| 286 | { | ||
| 287 | if ((-f $File::Find::name) && ($File::Find::name =~ /\d+\.xml$/)) | ||
| 288 | { | ||
| 289 | push @corpusFileList, $File::Find::name; | ||
| 290 | } | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | # write the list of corpus files to disk. | ||
| 295 | sub _writeListOfCorpusFiles | ||
| 296 | { | ||
| 297 | my ($Self) = @_; | ||
| 298 | |||
| 299 | # open the fileList for writing. | ||
| 300 | local *OUT; | ||
| 301 | my $fileList = $Self->{fileList}; | ||
| 302 | unless (open (OUT, '>' . $fileList)) | ||
| 303 | { | ||
| 304 | my $logger = Log::Log4perl->get_logger(); | ||
| 305 | $logger->logdie("could not open file '" . $fileList . ". for writing: " . $!); | ||
| 306 | } | ||
| 307 | |||
| 308 | # get the corpus directory. | ||
| 309 | my $corpusDirectory = $Self->{corpusDirectory}; | ||
| 310 | |||
| 311 | # write the list of relative paths to the file list. | ||
| 312 | my @relativeCorpusFileList; | ||
| 313 | my $corpusFileList = $Self->{corpusFileList}; | ||
| 314 | foreach my $filePath (@$corpusFileList) | ||
| 315 | { | ||
| 316 | my $relativePath = File::Spec->abs2rel ($filePath, $corpusDirectory); | ||
| 317 | print OUT $relativePath . "\n"; | ||
| 318 | } | ||
| 319 | close OUT; | ||
| 320 | |||
| 321 | return undef; | ||
| 322 | } | ||
| 323 | |||
| 324 | # builds the hash of the corpus files. | ||
| 325 | sub buildCorpusFileHash | ||
| 326 | { | ||
| 327 | my $Self = shift; | ||
| 328 | |||
| 329 | # build the hash or corpus files. | ||
| 330 | my $corpusFileList = $Self->{corpusFileList}; | ||
| 331 | my %corpusFileHash; | ||
| 332 | for (my $i = 0; $i < @$corpusFileList; $i++) | ||
| 333 | { | ||
| 334 | $corpusFileHash{$corpusFileList->[$i]} = $i; | ||
| 335 | } | ||
| 336 | $Self->{corpusFileHash} = \%corpusFileHash; | ||
| 337 | return; | ||
| 338 | } | ||
| 339 | |||
| 340 | =head2 C<getTotalDocuments> | ||
| 341 | |||
| 342 | getTotalDocuments () | ||
| 343 | |||
| 344 | C<getTotalDocuments> returns the total number of documents in the corpus. The index to the | ||
| 345 | documents in the corpus ranges from zero to C<getTotalDocuments() - 1>. | ||
| 346 | |||
| 347 | =cut | ||
| 348 | |||
| 349 | # returns the total number of documents in the corpus. | ||
| 350 | sub getTotalDocuments | ||
| 351 | { | ||
| 352 | my $Self = shift; | ||
| 353 | return scalar($#{ $Self->{corpusFileList} } + 1); | ||
| 354 | } | ||
| 355 | |||
| 356 | # returns the path of a file given an index. | ||
| 357 | sub getFilePathOfDocument | ||
| 358 | { | ||
| 359 | my $Self = shift; | ||
| 360 | my $indexOfFile = shift; | ||
| 361 | return undef unless defined $indexOfFile; | ||
| 362 | return $Self->{corpusFileList}->[$indexOfFile]; | ||
| 363 | } | ||
| 364 | |||
| 365 | =head2 C<test> | ||
| 366 | |||
| 367 | test () | ||
| 368 | |||
| 369 | C<test> does tests to ensure the documents in the corpus are accessible | ||
| 370 | and can be parsed. It returns true if all tests pass, otherwise a | ||
| 371 | description of the test that failed is logged using L<Log::Log4perl> and | ||
| 372 | false is returned. | ||
| 373 | |||
| 374 | For example: | ||
| 375 | |||
| 376 | use Text::Corpus::NewYorkTimes; | ||
| 377 | use Data::Dump qw(dump); | ||
| 378 | use Log::Log4perl qw(:easy); | ||
| 379 | Log::Log4perl->easy_init ($INFO); | ||
| 380 | my $corpus = Text::Corpus::NewYorkTimes->new (fileList => $fileList, corpusDirectory => $corpusDirectory); | ||
| 381 | dump $corpus->test; | ||
| 382 | |||
| 383 | =cut | ||
| 384 | |||
| 385 | sub test | ||
| 386 | { | ||
| 387 | my ($Self, %Parameters) = @_; | ||
| 388 | |||
| 389 | # make sure a corpus file list created. | ||
| 390 | unless (exists $Self->{corpusFileList}) | ||
| 391 | { | ||
| 392 | my $logger = Log::Log4perl->get_logger(); | ||
| 393 | $logger->logwarn ("list of corpus files not created; were corpusFileList and/or fileList defined correctly?\n"); | ||
| 394 | return 0; | ||
| 395 | } | ||
| 396 | |||
| 397 | # make sure some files were found. | ||
| 398 | my $totalDocuments = $Self->getTotalDocuments (); | ||
| 399 | unless ($totalDocuments) | ||
| 400 | { | ||
| 401 | my $logger = Log::Log4perl->get_logger(); | ||
| 402 | $logger->logwarn ("list of corpus files is empty; were corpusFileList and/or fileList defined correctly?\n"); | ||
| 403 | return 0; | ||
| 404 | } | ||
| 405 | |||
| 406 | # test for missing files. | ||
| 407 | my $numberOfFilesToTest = 20; | ||
| 408 | my @listOfMissingFiles; | ||
| 409 | my $corpusFileList = $Self->{corpusFileList}; | ||
| 410 | for (my $i = 0; $i < $numberOfFilesToTest; $i++) | ||
| 411 | { | ||
| 412 | my $index = int rand scalar @$corpusFileList; | ||
| 413 | my $filePath = $corpusFileList->[$index]; | ||
| 414 | push @listOfMissingFiles, $filePath unless -f $filePath; | ||
| 415 | } | ||
| 416 | |||
| 417 | # make sure some of the files exist. | ||
| 418 | if (@listOfMissingFiles) | ||
| 419 | { | ||
| 420 | my $logger = Log::Log4perl->get_logger(); | ||
| 421 | my $totalMissingFiles = scalar @listOfMissingFiles; | ||
| 422 | my $message = "randomly selected $numberOfFilesToTest files fron list of corpus files and found\n$totalMissingFiles of them are missing:\n"; | ||
| 423 | $message .= join ("\n", @listOfMissingFiles, "were corpusFileList and/or fileList defined correctly?\n"); | ||
| 424 | $logger->logwarn ($message); | ||
| 425 | return 0; | ||
| 426 | } | ||
| 427 | |||
| 428 | # randomly test some of the files for parsing errors. | ||
| 429 | for (my $i = 0; $i < $numberOfFilesToTest; $i++) | ||
| 430 | { | ||
| 431 | my $index = int rand scalar @$corpusFileList; | ||
| 432 | my $totalTextInfo = 0; | ||
| 433 | eval | ||
| 434 | { | ||
| 435 | my $document = $Self->getDocument(index => $index); | ||
| 436 | next unless defined $document; | ||
| 437 | my %documentInfo; | ||
| 438 | $documentInfo{title} = $document->getTitle(); | ||
| 439 | $documentInfo{body} = $document->getBody(); | ||
| 440 | $documentInfo{date} = [$document->getDate()]; | ||
| 441 | $documentInfo{content} = $document->getContent(); | ||
| 442 | $documentInfo{categories} = $document->getCategories(); | ||
| 443 | $documentInfo{description} = $document->getDescription(); | ||
| 444 | $documentInfo{uri} = [$document->getUri()]; | ||
| 445 | foreach my $key (keys %documentInfo) | ||
| 446 | { | ||
| 447 | $totalTextInfo += @{$documentInfo{$key}}; | ||
| 448 | } | ||
| 449 | }; | ||
| 450 | if ($@) | ||
| 451 | { | ||
| 452 | my $logger = Log::Log4perl->get_logger(); | ||
| 453 | my $totalMissingFiles = scalar @listOfMissingFiles; | ||
| 454 | my $message = "parsing of one of the corpus documents through an exception: $@\n"; | ||
| 455 | $logger->logwarn ($message); | ||
| 456 | return 0; | ||
| 457 | } | ||
| 458 | if ($totalTextInfo < 2) | ||
| 459 | { | ||
| 460 | my $logger = Log::Log4perl->get_logger(); | ||
| 461 | my $totalMissingFiles = scalar @listOfMissingFiles; | ||
| 462 | my $message = "parsing of one of the corpus documents returned insufficient information.\nwere corpusFileList and/or fileList defined correctly?\n"; | ||
| 463 | $logger->logwarn ($message); | ||
| 464 | return 0; | ||
| 465 | } | ||
| 466 | } | ||
| 467 | |||
| 468 | return 1; | ||
| 469 | } | ||
| 470 | |||
| 471 | =head1 EXAMPLES | ||
| 472 | |||
| 473 | The example below will print out all the information for each document in the corpus. | ||
| 474 | |||
| 475 | use Text::Corpus::NewYorkTimes; | ||
| 476 | use Data::Dump qw(dump); | ||
| 477 | use Log::Log4perl qw(:easy); | ||
| 478 | Log::Log4perl->easy_init ($INFO); | ||
| 479 | my $corpus = Text::Corpus::NewYorkTimes->new (fileList => $fileList, corpusDirectory => $corpusDirectory); | ||
| 480 | my $totalDocuments = $corpus->getTotalDocuments; | ||
| 481 | |||
| 482 | for (my $i = 0; $i < $totalDocuments; $i++) | ||
| 483 | { | ||
| 484 | eval | ||
| 485 | { | ||
| 486 | my $document = $corpus->getDocument(index => $i); | ||
| 487 | next unless defined $document; | ||
| 488 | my %documentInfo; | ||
| 489 | $documentInfo{title} = $document->getTitle(); | ||
| 490 | $documentInfo{body} = $document->getBody(); | ||
| 491 | $documentInfo{content} = $document->getContent(); | ||
| 492 | $documentInfo{categories} = $document->getCategories(); | ||
| 493 | $documentInfo{description} = $document->getDescription(); | ||
| 494 | $documentInfo{uri} = $document->getUri(); | ||
| 495 | dump \%documentInfo; | ||
| 496 | }; | ||
| 497 | } | ||
| 498 | |||
| 499 | =head1 INSTALLATION | ||
| 500 | |||
| 501 | To install the module set the environment variable | ||
| 502 | C<TEXT_CORPUS_NEWYORKTIMES_CORPUSDIRECTORY> to the path of the | ||
| 503 | New York Times corpus and run the following commands: | ||
| 504 | |||
| 505 | perl Makefile.PL | ||
| 506 | make | ||
| 507 | make test | ||
| 508 | make install | ||
| 509 | |||
| 510 | If you are on a windows box you should use 'nmake' rather than 'make'. | ||
| 511 | |||
| 512 | The module will install if C<TEXT_CORPUS_NEWYORKTIMES_CORPUSDIRECTORY> is not defined, but | ||
| 513 | less testing will be performed. After the New York Times corpus is installed testing | ||
| 514 | of the module can be performed by running: | ||
| 515 | |||
| 516 | use Text::Corpus::NewYorkTimes; | ||
| 517 | use Data::Dump qw(dump); | ||
| 518 | use Log::Log4perl qw(:easy); | ||
| 519 | Log::Log4perl->easy_init ($INFO); | ||
| 520 | my $corpus = Text::Corpus::NewYorkTimes->new (corpusDirectory => $corpusDirectory); | ||
| 521 | dump $corpus->test; | ||
| 522 | |||
| 523 | =head1 AUTHOR | ||
| 524 | |||
| 525 | Jeff Kubina<jeff.kubina@gmail.com> | ||
| 526 | |||
| 527 | =head1 COPYRIGHT | ||
| 528 | |||
| 529 | Copyright (c) 2009 Jeff Kubina. All rights reserved. | ||
| 530 | This program is free software; you can redistribute | ||
| 531 | it and/or modify it under the same terms as Perl itself. | ||
| 532 | |||
| 533 | The full text of the license can be found in the | ||
| 534 | LICENSE file included with this module. | ||
| 535 | |||
| 536 | =head1 KEYWORDS | ||
| 537 | |||
| 538 | nyt, new york times, english corpus, information processing | ||
| 539 | |||
| 540 | =head1 SEE ALSO | ||
| 541 | |||
| 542 | =begin html | ||
| 543 | |||
| 544 | This module requires the <a href="http://www.ldc.upenn.edu/Catalog/CatalogEntry.jsp?catalogId=LDC2008T19">The New York Times Annotated Corpus</a> | ||
| 545 | from the Linguistic Data Consortium; discussions about the corpus are moderated at the | ||
| 546 | Google Group called <a href="http://groups.google.com/group/nytnlp">The New York Times Annotated Corpus Community</a>. | ||
| 547 | |||
| 548 | =end html | ||
| 549 | |||
| 550 | L<Log::Log4perl>, L<Text::Corpus::NewYorkTimes::Document> | ||
| 551 | |||
| 552 | =cut | ||
| 553 | |||
| 554 | 1; | ||
| 555 | # The preceding line will help the module return a true value | ||