| 1 | package Text::Corpus::CNN; | ||||||||||||||||||||||||||||
| 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 Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use CHI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use LWP::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use Digest::MD5 qw(md5_hex); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Path::Class qw(dir file); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use File::Copy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Encode; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use Log::Log4perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use Text::Corpus::CNN::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use URI::Escape; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 1 | ▶ | use HTML::Encoding 'encoding_from_html_document'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 1 | my $el = "\n"; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | use Exporter (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 1 | $VERSION = '1.02'; | |||||||||||||||||||||||||||
| 24 | 1 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 25 | 1 | @EXPORT = qw(); | |||||||||||||||||||||||||||
| 26 | 1 | @EXPORT_OK = qw(); | |||||||||||||||||||||||||||
| 27 | 1 | %EXPORT_TAGS = (); | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | #12345678901234567890123456789012345678901234 | ||||||||||||||||||||||||||||
| 31 | #Make a corpus of CNN documents for research. | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | =head1 NAME | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | C<Text::Corpus::CNN> - Make a corpus of CNN documents for research. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | use Cwd; | ||||||||||||||||||||||||||||
| 40 | use File::Spec; | ||||||||||||||||||||||||||||
| 41 | use Text::Corpus::CNN; | ||||||||||||||||||||||||||||
| 42 | use Data::Dump qw(dump); | ||||||||||||||||||||||||||||
| 43 | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||
| 44 | Log::Log4perl->easy_init ($INFO); | ||||||||||||||||||||||||||||
| 45 | my $corpusDirectory = File::Spec->catfile (getcwd(), 'corpus_cnn'); | ||||||||||||||||||||||||||||
| 46 | my $corpus = Text::Corpus::CNN->new (corpusDirectory => $corpusDirectory); | ||||||||||||||||||||||||||||
| 47 | $corpus->update (verbose => 1); | ||||||||||||||||||||||||||||
| 48 | dump $corpus->getTotalDocuments; | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | C<Text::Corpus::CNN> can be used to create a temporary corpus of CNN news documents | ||||||||||||||||||||||||||||
| 53 | for personal research and testing of information processing methods. Read the | ||||||||||||||||||||||||||||
| 54 | CNN Interactive Service Agreement to ensure you abide by it when using this module. | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | The categories, description, title, etc... of a specified document | ||||||||||||||||||||||||||||
| 57 | are accessed using L<Text::Corpus::CNN::Document>. Also, all errors and | ||||||||||||||||||||||||||||
| 58 | warnings are logged using L<Log::Log4perl>, which should be L<initialized|Log::Log4perl/How_to_use_it>. | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 C<new> | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | The constructor C<new> creates an instance of the C<Text::Corpus::CNN> class | ||||||||||||||||||||||||||||
| 65 | with the following parameters: | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =over | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =item C<corpusDirectory> | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | corpusDirectory => '...' | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | C<corpusDirectory> is the directory that documents are cached into using | ||||||||||||||||||||||||||||
| 74 | L<CHI>. If C<corpusDirectory> is not defined, | ||||||||||||||||||||||||||||
| 75 | then the path specified in the environment variable C<TEXT_CORPUS_CNN_CORPUSDIRECTORY> | ||||||||||||||||||||||||||||
| 76 | is used if it is defined. If the directory defined does not exist, it will be | ||||||||||||||||||||||||||||
| 77 | created. A message is logged and an exception is thrown if no directory is specified. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =back | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | sub new | ||||||||||||||||||||||||||||
| 84 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=13 SCAR=29.9
Pod
documented
| |||||||||||||||||||||||||||||
| 85 | 1 | my ($Class, %Parameters) = @_; | |||||||||||||||||||||||||||
| 86 | 1 | ▶ | my $Self = bless ({}, ref ($Class) || $Class); | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $Class || $Class
MC/DC0%0/2
ref $Class || $Class
ref $Class
$Class
Truth table33%1/3
A || B
A= ref $ClassB= $Class
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # set the corpusDirectory. | ||||||||||||||||||||||||||||
| 89 | 1 | ▶ | unless (exists ($Parameters{corpusDirectory})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | { | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if (%ENV && exists ($ENV{TEXT_CORPUS_CNN_CORPUSDIRECTORY})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
%ENV and exists $ENV{"TEXT_CORPUS_CNN_CORPUSDIRECTORY"}
MC/DC0%0/2
%ENV and exists $ENV{"TEXT_CORPUS_CNN_CORPUSDIRECTORY"}
%ENV
exists $ENV{"TEXT_CORPUS_CNN_CORPUSDIRECTORY"}
Truth table0%0/3
A and B
A= %ENVB= exists $ENV{"TEXT_CORPUS_CNN_CORPUSDIRECTORY"}
| |||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 0 | $Parameters{corpusDirectory} = $ENV{TEXT_CORPUS_CNN_CORPUSDIRECTORY}; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # define the cache path if not defined. | ||||||||||||||||||||||||||||
| 98 | 1 | ▶ | unless (exists ($Parameters{corpusDirectory})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | { | ||||||||||||||||||||||||||||
| 100 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 101 | 0 | $logger->logdie("'corpusDirectory' parameter is not defined.\n"); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 1 | $Self->{corpusDirectory} = $Parameters{corpusDirectory}; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # make the cache directory. | ||||||||||||||||||||||||||||
| 106 | 1 | my $cacheDir = dir ($Parameters{corpusDirectory}, 'cache'); | |||||||||||||||||||||||||||
| 107 | 1 | ▶ | $cacheDir->mkpath (0, 0700) unless -d $cacheDir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | $Self->{cacheDirectory} = sprintf $cacheDir; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # get the caching engine. | ||||||||||||||||||||||||||||
| 111 | 1 | my $cacheEngine = CHI->new (driver => 'File', root_dir => $Self->{cacheDirectory}); | |||||||||||||||||||||||||||
| 112 | 1 | ▶ | croak "could not create cache within '" . $Self->{cacheDirectory} . "': $!\n" unless defined $cacheEngine; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 1 | $Self->{cacheEngine} = $cacheEngine; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # make the sitenews new and old directories. | ||||||||||||||||||||||||||||
| 116 | 1 | my $sitenewsNewDirectory = dir ($Self->{corpusDirectory}, 'sitenews', 'new'); | |||||||||||||||||||||||||||
| 117 | 1 | ▶ | $sitenewsNewDirectory->mkpath (0, 0700) unless -e $sitenewsNewDirectory; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 1 | $Self->{sitenewsNewDirectory} = sprintf $sitenewsNewDirectory; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 1 | my $sitenewsOldDirectory = dir ($Self->{corpusDirectory}, 'sitenews', 'old'); | |||||||||||||||||||||||||||
| 121 | 1 | ▶ | $sitenewsOldDirectory->mkpath (0, 0700) unless -e $sitenewsOldDirectory; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 1 | $Self->{sitenewsOldDirectory} = sprintf $sitenewsOldDirectory; | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # get the site news url. | ||||||||||||||||||||||||||||
| 125 | 1 | $Self->{siteMapNewsURL} = 'http://www.cnn.com/sitemap_news.xml'; | |||||||||||||||||||||||||||
| 126 | 1 | ▶ | $Self->{siteMapNewsURL} = $Parameters{siteMapNewsURL} if exists $Parameters{siteMapNewsURL}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # set the cache expiration. | ||||||||||||||||||||||||||||
| 129 | 1 | $Self->{cacheExpiration} = 'never'; | |||||||||||||||||||||||||||
| 130 | 1 | ▶ | $Self->{cacheExpiration} = $Parameters{cacheExpiration} if defined $Parameters{cacheExpiration}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # set the delay between page fetches in seconds. | ||||||||||||||||||||||||||||
| 133 | 1 | $Self->{delayInSeconds} = 8; | |||||||||||||||||||||||||||
| 134 | 1 | $Self->{timeOfLastPageFetch} = 0; | |||||||||||||||||||||||||||
| 135 | 1 | $Self->{maxFetchAttempts} = 4; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # set the verbosity level. | ||||||||||||||||||||||||||||
| 138 | 1 | $Self->{verbose} = 0; | |||||||||||||||||||||||||||
| 139 | 1 | ▶ | $Self->{verbose} = $Parameters{verbose} if exists $Parameters{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # get the document index from the cache. | ||||||||||||||||||||||||||||
| 142 | 1 | $Self->_getIndex (); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 1 | return $Self; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # used LWP::Simple::getstore to fetch the CNN sitemap_news.xml file. keeps the | ||||||||||||||||||||||||||||
| 148 | # time of the last fetch in the cache. | ||||||||||||||||||||||||||||
| 149 | sub _fetchNewSiteNews | ||||||||||||||||||||||||||||
| 150 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_fetchNewSiteNews: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 151 | 0 | my $Self = shift; | |||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # build the path to the file it will be stored in, the name is | ||||||||||||||||||||||||||||
| 154 | # base on the epoch time. | ||||||||||||||||||||||||||||
| 155 | 0 | my $fetchTime = time; | |||||||||||||||||||||||||||
| 156 | 0 | my $filePathName = file ($Self->{sitenewsNewDirectory}, $fetchTime . '.sitemap_news.xml'); | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | # if the file already exists, return; calling the function way too much. | ||||||||||||||||||||||||||||
| 159 | 0 | ▶ | return undef if -e $filePathName; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # get the cache engine. | ||||||||||||||||||||||||||||
| 162 | 0 | my $cacheEngine = $Self->{cacheEngine}; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # get the time of the last fetch. | ||||||||||||||||||||||||||||
| 165 | 0 | my $lastFetchTime = $cacheEngine->get ('lastFetchTime'); | |||||||||||||||||||||||||||
| 166 | 0 | ▶ | $lastFetchTime = 0 unless defined $lastFetchTime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # if too soon, do not fetch. the sitemap_news.xml file is only | ||||||||||||||||||||||||||||
| 169 | # updated when new documents for indexing are posted. so don't | ||||||||||||||||||||||||||||
| 170 | # access very often. | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | return undef if (time - $lastFetchTime < 10*60); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | # fetch the page and store it in a file. | ||||||||||||||||||||||||||||
| 174 | 0 | my $fetchStatus = getstore ($Self->{siteMapNewsURL}, sprintf $filePathName); | |||||||||||||||||||||||||||
| 175 | 0 | ▶ | if ($fetchStatus != 200) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | { | ||||||||||||||||||||||||||||
| 177 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 178 | 0 | $logger->logdie ("fetch status of '" . $Self->{siteMapNewsURL} . "' is " . $fetchStatus . ".\n"); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | # store the time fetched. | ||||||||||||||||||||||||||||
| 182 | 0 | $cacheEngine->set ('lastFetchTime', $fetchTime, 'never'); | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | return undef; | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | # use an xpath query to pull out the urls of the pages in | ||||||||||||||||||||||||||||
| 188 | # the sitemap file. catches any exceptions thrown, most likely | ||||||||||||||||||||||||||||
| 189 | # due to parsing errors. | ||||||||||||||||||||||||||||
| 190 | sub _getUrlsFromSiteNewsFile | ||||||||||||||||||||||||||||
| 191 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_getUrlsFromSiteNewsFile: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 192 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # holds the urls in the file. | ||||||||||||||||||||||||||||
| 195 | 0 | my @urls; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | # get the file | ||||||||||||||||||||||||||||
| 198 | 0 | my $siteNewsFile = $Parameters{siteNewsFile}; | |||||||||||||||||||||||||||
| 199 | 0 | ▶ | return \@urls unless defined $siteNewsFile; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | eval | ||||||||||||||||||||||||||||
| 202 | 0 | { | |||||||||||||||||||||||||||
| 203 | # get the parser. | ||||||||||||||||||||||||||||
| 204 | 0 | my $libxmlParser = XML::LibXML->new; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | # make sure the network is not accessed. | ||||||||||||||||||||||||||||
| 207 | 0 | $libxmlParser->load_ext_dtd (0); | |||||||||||||||||||||||||||
| 208 | 0 | $libxmlParser->no_network (1); | |||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | # parse the file. | ||||||||||||||||||||||||||||
| 211 | 0 | my $doc = $libxmlParser->parse_file ($siteNewsFile); | |||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # prep the file for xpath searching. | ||||||||||||||||||||||||||||
| 214 | 0 | my $xp = XML::LibXML::XPathContext->new($doc); | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # set the default namespace prefix. | ||||||||||||||||||||||||||||
| 217 | # $xp->registerNs('x', 'http://www.google.com/schemas/sitemap/0.84'); | ||||||||||||||||||||||||||||
| 218 | # TODO: make this a parameter. | ||||||||||||||||||||||||||||
| 219 | 0 | $xp->registerNs ('x', 'http://www.sitemaps.org/schemas/sitemap/0.9'); | |||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | # get the urls | ||||||||||||||||||||||||||||
| 222 | 0 | my @nodes = $xp->findnodes('/x:urlset/x:url/x:loc'); | |||||||||||||||||||||||||||
| 223 | 0 | foreach my $node (@nodes) | |||||||||||||||||||||||||||
| 224 | { | ||||||||||||||||||||||||||||
| 225 | 0 | push @urls, $node->textContent(); | |||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||
| 227 | }; | ||||||||||||||||||||||||||||
| 228 | 0 | ▶ | if ($@) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | { | ||||||||||||||||||||||||||||
| 230 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 231 | 0 | $logger->logwarn ("caught exception, probably xml parsing error in file '" | |||||||||||||||||||||||||||
| 232 | . $siteNewsFile . "', skipping over the file: " . $@); | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # if zero urls, then use regexp. | ||||||||||||||||||||||||||||
| 236 | 0 | ▶ | if (@urls == 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | { | ||||||||||||||||||||||||||||
| 238 | # get the size of the file and log any errors. | ||||||||||||||||||||||||||||
| 239 | 0 | my $fileSize = -s $siteNewsFile; | |||||||||||||||||||||||||||
| 240 | 0 | local *IN; | |||||||||||||||||||||||||||
| 241 | 0 | ▶ | if (!open (IN, $siteNewsFile)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 242 | { | ||||||||||||||||||||||||||||
| 243 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 244 | 0 | $logger->logdie ("could not open file '" . $siteNewsFile . "' for reading: $!\n"); | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | # read the entire file into memory. | ||||||||||||||||||||||||||||
| 248 | 0 | binmode IN; | |||||||||||||||||||||||||||
| 249 | 0 | my $fileContents; | |||||||||||||||||||||||||||
| 250 | 0 | my $bytesRead = read (IN, $fileContents, $fileSize, 0); | |||||||||||||||||||||||||||
| 251 | 0 | ▶ | if ($fileSize != $bytesRead) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | { | ||||||||||||||||||||||||||||
| 253 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 254 | 0 | $logger->logwarn ("problems reading from file '" . $siteNewsFile . "'.\n"); | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | 0 | close IN; | |||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | # parse out the urls of the documents. | ||||||||||||||||||||||||||||
| 259 | 0 | my %urls; | |||||||||||||||||||||||||||
| 260 | 0 | ▶ | while ($fileContents =~ m/<loc>(http[\x00-\xff]*?)<\/loc>/ig) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | { | ||||||||||||||||||||||||||||
| 262 | 0 | $urls{$1} = 1; | |||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | my @newUrls = keys %urls; | |||||||||||||||||||||||||||
| 266 | 0 | my $totalUrlsFound = @newUrls; | |||||||||||||||||||||||||||
| 267 | 0 | push @urls, @newUrls; | |||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||
| 269 | # print warings that urls were found using regexp. | ||||||||||||||||||||||||||||
| 270 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 271 | 0 | $logger->logwarn ("no urls found via XML parsing, $totalUrlsFound found using regular expression.\n"); | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 0 | return \@urls; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head1 METHODS | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | =head2 C<getDocument> | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | getDocument (index => $index, cacheOnly => 0) | ||||||||||||||||||||||||||||
| 282 | getDocument (uri => $uri, cacheOnly => 0) | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | C<getDocument> returns a L<Text::Corpus::CNN::Document> object for the | ||||||||||||||||||||||||||||
| 285 | document with index C<$index> or uri C<$uri>. The document | ||||||||||||||||||||||||||||
| 286 | indices range from zero to C<getTotalDocument()-1>; C<getDocument> returns | ||||||||||||||||||||||||||||
| 287 | C<undef> if any errors occurred and logs them using L<Log::Log4perl>. | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =over | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | =item C<index> | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | index => '...' | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | C<index> should be the number of the document to return. It should be a | ||||||||||||||||||||||||||||
| 296 | non-negative integer less than C<getTotalDocument>. If it is out of range | ||||||||||||||||||||||||||||
| 297 | C<undef> is returned. | ||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | =item C<uri> | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | uri => '...' | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | C<uri> should be the URL of the document to return. If the document is not in the cache, | ||||||||||||||||||||||||||||
| 304 | it is fetched unless C<cacheOnly> evaluates to false, in that case C<undef> is returned. | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | =item C<cacheOnly> | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | cacheOnly => 0 | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | If C<cacheOnly> evaluates to true, then only documents in the cache are returned, otherwise C<undef> is returned. The default is false. | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =back | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | An example: | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | use Cwd; | ||||||||||||||||||||||||||||
| 317 | use File::Spec; | ||||||||||||||||||||||||||||
| 318 | use Text::Corpus::CNN; | ||||||||||||||||||||||||||||
| 319 | use Data::Dump qw(dump); | ||||||||||||||||||||||||||||
| 320 | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||
| 321 | Log::Log4perl->easy_init ($INFO); | ||||||||||||||||||||||||||||
| 322 | my $corpusDirectory = File::Spec->catfile (getcwd(), 'corpus_cnn'); | ||||||||||||||||||||||||||||
| 323 | my $corpus = Text::Corpus::CNN->new (corpusDirectory => $corpusDirectory); | ||||||||||||||||||||||||||||
| 324 | $corpus->update (verbose => 1); | ||||||||||||||||||||||||||||
| 325 | my $document = $corpus->getDocument (index => 0); | ||||||||||||||||||||||||||||
| 326 | dump $document->getBody; | ||||||||||||||||||||||||||||
| 327 | dump $document->getCategories; | ||||||||||||||||||||||||||||
| 328 | dump $document->getContent; | ||||||||||||||||||||||||||||
| 329 | dump $document->getDate; | ||||||||||||||||||||||||||||
| 330 | dump $document->getDescription; | ||||||||||||||||||||||||||||
| 331 | dump $document->getHighlights; | ||||||||||||||||||||||||||||
| 332 | dump $document->getTitle; | ||||||||||||||||||||||||||||
| 333 | dump $document->getUri; | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | =cut | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | sub getDocument | ||||||||||||||||||||||||||||
| 338 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getDocument: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 339 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | # if the parameter uri is defined use it; if index is defined, use it instead. | ||||||||||||||||||||||||||||
| 342 | 0 | my $urlIndex = $Self->{urlIndex}; | |||||||||||||||||||||||||||
| 343 | 0 | my $urlHash = $Self->{urlHash}; | |||||||||||||||||||||||||||
| 344 | 0 | my $indexOfArticle; | |||||||||||||||||||||||||||
| 345 | 0 | ▶ | my $cacheOnly = exists ($Parameters{cacheOnly}) && $Parameters{cacheOnly}; | ||||||||||||||||||||||||||
|
Condition0%0/3
exists $Parameters{'cacheOnly'} && $Parameters{'cacheOnly'}
MC/DC0%0/2
exists $Parameters{'cacheOnly'} && $Parameters{'cacheOnly'}
exists $Parameters{'cacheOnly'}
$Parameters{'cacheOnly'}
Truth table0%0/3
A && B
A= exists $Parameters{'cacheOnly'}B= $Parameters{'cacheOnly'}
| |||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | # if the index is defined and valid, use it. | ||||||||||||||||||||||||||||
| 348 | 0 | ▶ | $indexOfArticle = int abs $Parameters{index} if exists $Parameters{index}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | 0 | ▶ | if ($indexOfArticle >= @$urlIndex) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | { | ||||||||||||||||||||||||||||
| 351 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 352 | 0 | $logger->logwarn ("document index '" . $indexOfArticle . "', has an invalid range.\n"); | |||||||||||||||||||||||||||
| 353 | 0 | $indexOfArticle = undef; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||
| 356 | # if the index is not defined try the uri. | ||||||||||||||||||||||||||||
| 357 | 0 | ▶ | if (!defined ($indexOfArticle) && exists ($Parameters{uri})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $indexOfArticle and exists $Parameters{"uri"}
MC/DC0%0/2
not defined $indexOfArticle and exists $Parameters{"uri"}
not defined $indexOfArticle
exists $Parameters{"uri"}
Truth table0%0/3
not(A) and B
A= not defined $indexOfArticleB= exists $Parameters{"uri"}
| |||||||||||||||||||||||||||||
| 358 | { | ||||||||||||||||||||||||||||
| 359 | 0 | my $url = $Parameters{uri}; | |||||||||||||||||||||||||||
| 360 | 0 | ▶ | return undef unless ($url =~ /^http/); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 361 | 0 | ▶ | if (exists $urlHash->{$Parameters{uri}}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | { | ||||||||||||||||||||||||||||
| 363 | 0 | $indexOfArticle = $urlHash->{$Parameters{uri}}; | |||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | else | ||||||||||||||||||||||||||||
| 366 | { | ||||||||||||||||||||||||||||
| 367 | 0 | my $totalDocuments = $Self->getTotalDocuments (); | |||||||||||||||||||||||||||
| 368 | 0 | $urlIndex->[$totalDocuments] = $url; | |||||||||||||||||||||||||||
| 369 | 0 | $urlHash->{$url} = $totalDocuments; | |||||||||||||||||||||||||||
| 370 | 0 | $indexOfArticle = $totalDocuments; | |||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | # store the urls in the cache. it is really slow to store this to the disk cache each time | ||||||||||||||||||||||||||||
| 373 | # a new document url is added. | ||||||||||||||||||||||||||||
| 374 | 0 | my $cacheEngine = $Self->{cacheEngine}; | |||||||||||||||||||||||||||
| 375 | 0 | $cacheEngine->set ('urlIndex', $urlIndex, $Self->{cacheExpiration}); | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | # if $indexOfArticle is not defined at this point, there is no article to return; | ||||||||||||||||||||||||||||
| 380 | # so log an error and return undef. | ||||||||||||||||||||||||||||
| 381 | 0 | ▶ | unless (defined $indexOfArticle) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 382 | { | ||||||||||||||||||||||||||||
| 383 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 384 | 0 | $logger->logwarn ("document index and/or uri invalid, returned undefined.\n"); | |||||||||||||||||||||||||||
| 385 | 0 | return undef; | |||||||||||||||||||||||||||
| 386 | } | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | # get the articles url. | ||||||||||||||||||||||||||||
| 389 | 0 | my $url = $urlIndex->[$indexOfArticle]; | |||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | # get the html content of the article. | ||||||||||||||||||||||||||||
| 392 | 0 | my $htmlEncoding = $Self->_getArticle (index => $indexOfArticle, cacheOnly => $cacheOnly); | |||||||||||||||||||||||||||
| 393 | 0 | ▶ | return undef unless defined $htmlEncoding; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | # get the document object. | ||||||||||||||||||||||||||||
| 396 | 0 | my $document; | |||||||||||||||||||||||||||
| 397 | eval | ||||||||||||||||||||||||||||
| 398 | 0 | { | |||||||||||||||||||||||||||
| 399 | 0 | $document = Text::Corpus::CNN::Document->new (htmlContent => $htmlEncoding->[0], uri => $url, encoding => $htmlEncoding->[1]); | |||||||||||||||||||||||||||
| 400 | }; | ||||||||||||||||||||||||||||
| 401 | 0 | ▶ | if ($@) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 402 | { | ||||||||||||||||||||||||||||
| 403 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 404 | 0 | $logger->logwarn ("caught exception, probably html parsing error in file '" | |||||||||||||||||||||||||||
| 405 | . $url . "', skipping over the document: " . $@); | ||||||||||||||||||||||||||||
| 406 | 0 | $document = undef; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | 0 | return $document; | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =head2 C<getTotalDocuments> | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | getTotalDocuments () | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | C<getTotalDocuments> returns the total number of documents in the corpus. The index to the | ||||||||||||||||||||||||||||
| 416 | documents in the corpus ranges from zero to C<getTotalDocuments() - 1>. | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =cut | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | # return the total number of articles in the corpus. this should | ||||||||||||||||||||||||||||
| 421 | # increase as update is repeatedly called. | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | sub getTotalDocuments | ||||||||||||||||||||||||||||
| 424 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getTotalDocuments: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 425 | 0 | my $Self = shift; | |||||||||||||||||||||||||||
| 426 | 0 | $Self->_getIndex (); | |||||||||||||||||||||||||||
| 427 | 0 | return scalar (@{$Self->{urlIndex}}); | |||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | =head2 C<getURIsInCorpus> | ||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||
| 432 | getURIsInCorpus () | ||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||
| 434 | C<getURIsInCorpus> returns an array reference of all the URIs in the corpus. | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | For example: | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | use Cwd; | ||||||||||||||||||||||||||||
| 439 | use File::Spec; | ||||||||||||||||||||||||||||
| 440 | use Text::Corpus::CNN; | ||||||||||||||||||||||||||||
| 441 | use Data::Dump qw(dump); | ||||||||||||||||||||||||||||
| 442 | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||
| 443 | Log::Log4perl->easy_init ($INFO); | ||||||||||||||||||||||||||||
| 444 | my $corpusDirectory = File::Spec->catfile (getcwd(), 'corpus_cnn'); | ||||||||||||||||||||||||||||
| 445 | my $corpus = Text::Corpus::CNN->new (corpusDirectory => $corpusDirectory); | ||||||||||||||||||||||||||||
| 446 | dump $corpus->getURIsInCorpus; | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =cut | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | sub getURIsInCorpus | ||||||||||||||||||||||||||||
| 451 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
getURIsInCorpus: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 452 | 0 | my ($Self) = @_; | |||||||||||||||||||||||||||
| 453 | 0 | return [@{$Self->{urlIndex}}]; | |||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||
| 456 | =head2 C<update> | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | update (verbose => 0) | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | This method updates the set of documents in the corpus by fetching | ||||||||||||||||||||||||||||
| 461 | any newly listed documents in the C<sitemap_news.xml> file. | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | =over | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =item C<verbose> | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | verbose => 0 | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | If C<verbose> is positive, then after each new document is fetched a message is | ||||||||||||||||||||||||||||
| 470 | logged stating the number of documents remaining to fetch and the | ||||||||||||||||||||||||||||
| 471 | approximate time to completion. C<update> returns the number of documents fetched. | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =back | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | For example: | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | use Cwd; | ||||||||||||||||||||||||||||
| 478 | use File::Spec; | ||||||||||||||||||||||||||||
| 479 | use Text::Corpus::CNN; | ||||||||||||||||||||||||||||
| 480 | use Data::Dump qw(dump); | ||||||||||||||||||||||||||||
| 481 | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||
| 482 | Log::Log4perl->easy_init ($INFO); | ||||||||||||||||||||||||||||
| 483 | my $corpusDirectory = File::Spec->catfile (getcwd(), 'corpus_cnn'); | ||||||||||||||||||||||||||||
| 484 | my $corpus = Text::Corpus::CNN->new (corpusDirectory => $corpusDirectory); | ||||||||||||||||||||||||||||
| 485 | $corpus->update (verbose => 1); | ||||||||||||||||||||||||||||
| 486 | dump $corpus->getTotalDocuments; | ||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | =cut | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | # TODO: document listOfArticleUrls in pod. | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | sub update # (test => 1) fetches only one document for testing. | ||||||||||||||||||||||||||||
| 493 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
update: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 494 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||
| 496 | # get the previous urls. | ||||||||||||||||||||||||||||
| 497 | 0 | my $cacheEngine = $Self->{cacheEngine}; | |||||||||||||||||||||||||||
| 498 | 0 | my $urlIndex = $Self->{urlIndex}; | |||||||||||||||||||||||||||
| 499 | 0 | my $urlHash = $Self->{urlHash}; | |||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | # if listOfUrls is defined it is used. | ||||||||||||||||||||||||||||
| 502 | 0 | my $listOfArticleUrls; | |||||||||||||||||||||||||||
| 503 | 0 | ▶ | if (exists ($Parameters{listOfArticleUrls})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 504 | { | ||||||||||||||||||||||||||||
| 505 | 0 | $listOfArticleUrls = $Parameters{listOfArticleUrls}; | |||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||
| 507 | else | ||||||||||||||||||||||||||||
| 508 | { | ||||||||||||||||||||||||||||
| 509 | 0 | $listOfArticleUrls = $Self->_getListOfNewArticleUrlsViaSitemap (); | |||||||||||||||||||||||||||
| 510 | } | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | # get the total urls. | ||||||||||||||||||||||||||||
| 513 | 0 | my $totalUrls = scalar (@$urlIndex); | |||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||
| 515 | # put the urls into the hash to remove dupes. | ||||||||||||||||||||||||||||
| 516 | 0 | foreach my $url (@$listOfArticleUrls) | |||||||||||||||||||||||||||
| 517 | { | ||||||||||||||||||||||||||||
| 518 | 0 | ▶ | next unless ($url =~ /^http/i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 519 | 0 | ▶ | next if exists $urlHash->{$url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 520 | 0 | $urlIndex->[$totalUrls] = $url; | |||||||||||||||||||||||||||
| 521 | 0 | $urlHash->{$url} = $totalUrls; | |||||||||||||||||||||||||||
| 522 | 0 | ++$totalUrls; | |||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | # store the urls in the cache. | ||||||||||||||||||||||||||||
| 526 | 0 | $cacheEngine->set ('urlIndex', $urlIndex, $Self->{cacheExpiration}); | |||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | # add new documents to the cache. | ||||||||||||||||||||||||||||
| 529 | 0 | return $Self->_primeCache (%Parameters); | |||||||||||||||||||||||||||
| 530 | } | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | # returns the list of urls from the sitemaps files in $Self->{sitenewsNewDirectory} and | ||||||||||||||||||||||||||||
| 533 | # moves files to $Self->{sitenewsOldDirectory}. | ||||||||||||||||||||||||||||
| 534 | sub _getListOfNewArticleUrlsViaSitemap | ||||||||||||||||||||||||||||
| 535 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_getListOfNewArticleUrlsViaSitemap: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 536 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | # fetch the latest site news file. | ||||||||||||||||||||||||||||
| 539 | 0 | $Self->_fetchNewSiteNews (); | |||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | # get the list of new sitenews files. | ||||||||||||||||||||||||||||
| 542 | 0 | local *DIR; | |||||||||||||||||||||||||||
| 543 | 0 | ▶ | opendir (DIR, $Self->{sitenewsNewDirectory}) || croak "could not read directory '" . $Self->{sitenewsNewDirectory} . "': $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 544 | 0 | my @newSiteNewsFiles = grep {/xml$/} readdir (DIR); | |||||||||||||||||||||||||||
| 545 | 0 | close DIR; | |||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | # foreach file read in the list of urls. | ||||||||||||||||||||||||||||
| 548 | 0 | my %hashOfArticleUrls; | |||||||||||||||||||||||||||
| 549 | 0 | foreach my $siteNewsFile (@newSiteNewsFiles) | |||||||||||||||||||||||||||
| 550 | { | ||||||||||||||||||||||||||||
| 551 | # get the full path to the file. | ||||||||||||||||||||||||||||
| 552 | 0 | my $fullPathSiteNewsFile = sprintf dir($Self->{sitenewsNewDirectory}, $siteNewsFile); | |||||||||||||||||||||||||||
| 553 | |||||||||||||||||||||||||||||
| 554 | # skip the file if it is empty. | ||||||||||||||||||||||||||||
| 555 | 0 | ▶ | unless (-s $fullPathSiteNewsFile) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 556 | { | ||||||||||||||||||||||||||||
| 557 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 558 | 0 | $logger->logwarn ("skipping over file '" . $fullPathSiteNewsFile . "' since it is empty.\n"); | |||||||||||||||||||||||||||
| 559 | 0 | next; | |||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | # get all the urls in the file. | ||||||||||||||||||||||||||||
| 563 | 0 | my $listOfArticleUrls; | |||||||||||||||||||||||||||
| 564 | eval | ||||||||||||||||||||||||||||
| 565 | 0 | { | |||||||||||||||||||||||||||
| 566 | 0 | $listOfArticleUrls = $Self->_getUrlsFromSiteNewsFile (siteNewsFile => $fullPathSiteNewsFile); | |||||||||||||||||||||||||||
| 567 | }; | ||||||||||||||||||||||||||||
| 568 | 0 | ▶ | if ($@) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 569 | { | ||||||||||||||||||||||||||||
| 570 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 571 | 0 | $logger->logwarn ("caught exception, probably xml parsing error in file '" | |||||||||||||||||||||||||||
| 572 | . $fullPathSiteNewsFile . "', skipping over the file: " . $@); | ||||||||||||||||||||||||||||
| 573 | 0 | $listOfArticleUrls = []; | |||||||||||||||||||||||||||
| 574 | } | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | # put the urls into the hash to remove dupes. | ||||||||||||||||||||||||||||
| 577 | 0 | foreach my $url (@$listOfArticleUrls) | |||||||||||||||||||||||||||
| 578 | { | ||||||||||||||||||||||||||||
| 579 | 0 | ▶ | next unless ($url =~ /^http/i); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 580 | 0 | $hashOfArticleUrls{$url} = 1; | |||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | # move the new site maps to the old directory. | ||||||||||||||||||||||||||||
| 585 | 0 | foreach my $siteNewsFile (@newSiteNewsFiles) | |||||||||||||||||||||||||||
| 586 | { | ||||||||||||||||||||||||||||
| 587 | # get the full path to the file. | ||||||||||||||||||||||||||||
| 588 | 0 | my $fullPathSiteNewsFileNew = sprintf dir($Self->{sitenewsNewDirectory}, $siteNewsFile); | |||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | # get the full path to where the file is to be moved to. | ||||||||||||||||||||||||||||
| 591 | 0 | my $fullPathSiteNewsFileOld = sprintf dir($Self->{sitenewsOldDirectory}, $siteNewsFile); | |||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | 0 | move ($fullPathSiteNewsFileNew, $fullPathSiteNewsFileOld); | |||||||||||||||||||||||||||
| 594 | } | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | 0 | return [sort keys %hashOfArticleUrls]; | |||||||||||||||||||||||||||
| 597 | } | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | # fetches the html page of an document given its url. first it looks in the | ||||||||||||||||||||||||||||
| 600 | # cache, if not there it fetches it and and places the contents in the cache. | ||||||||||||||||||||||||||||
| 601 | # returns undef if problems, otherwise a string reference to the pages html | ||||||||||||||||||||||||||||
| 602 | # content. | ||||||||||||||||||||||||||||
| 603 | sub _getArticle | ||||||||||||||||||||||||||||
| 604 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_getArticle: not called CC=19 SCAR=59.4
| |||||||||||||||||||||||||||||
| 605 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | # get the url to fetch. | ||||||||||||||||||||||||||||
| 608 | 0 | ▶ | my $md5OfUrl = $Parameters{md5OfUrl} if exists $Parameters{md5OfUrl}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 609 | 0 | ▶ | my $url = $Self->{urlIndex}->[$Parameters{index}] if exists $Parameters{index}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | # need to convert url to md5hex since cache uses it as filename | ||||||||||||||||||||||||||||
| 612 | # and urls could be too long. | ||||||||||||||||||||||||||||
| 613 | 0 | ▶ | $md5OfUrl = md5_hex ($url) if defined $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | # if the page is in the cache, return it from there. | ||||||||||||||||||||||||||||
| 616 | 0 | my $cacheEngine = $Self->{cacheEngine}; | |||||||||||||||||||||||||||
| 617 | 0 | my $pageContents = $cacheEngine->get ($md5OfUrl); | |||||||||||||||||||||||||||
| 618 | 0 | ▶ | if (defined $pageContents) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 619 | { | ||||||||||||||||||||||||||||
| 620 | # log the cache hit. | ||||||||||||||||||||||||||||
| 621 | 0 | ▶ | if ($Parameters{logCacheHitsMisses}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 622 | { | ||||||||||||||||||||||||||||
| 623 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 624 | 0 | $logger->info ("cache hit: $md5OfUrl: $url\n"); | |||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | # return its contents, decoded. | ||||||||||||||||||||||||||||
| 628 | 0 | my $encoding = encoding_from_html_document($pageContents, xhtml => 0); | |||||||||||||||||||||||||||
| 629 | 0 | ▶ | $encoding = encoding_from_html_document($pageContents, xhtml => 1) unless defined $encoding; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 630 | 0 | ▶ | unless (defined $encoding) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 631 | { | ||||||||||||||||||||||||||||
| 632 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 633 | 0 | $logger->logwarn ("could not determine encoding, defaulting to iso-8859-1: $md5OfUrl: $url\n"); | |||||||||||||||||||||||||||
| 634 | } | ||||||||||||||||||||||||||||
| 635 | 0 | ▶ | $encoding = 'iso-8859-1' unless defined $encoding; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 636 | 0 | $pageContents = decode ($encoding, $pageContents); | |||||||||||||||||||||||||||
| 637 | 0 | return [$pageContents, $encoding]; | |||||||||||||||||||||||||||
| 638 | } | ||||||||||||||||||||||||||||
| 639 | |||||||||||||||||||||||||||||
| 640 | # log the cache hit. | ||||||||||||||||||||||||||||
| 641 | 0 | ▶ | if ($Parameters{logCacheHitsMisses}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 642 | { | ||||||||||||||||||||||||||||
| 643 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 644 | 0 | $logger->info ("cache miss: $md5OfUrl: $url\n"); | |||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | # return now if only getting from the cache. | ||||||||||||||||||||||||||||
| 648 | 0 | ▶ | return undef if (exists ($Parameters{cacheOnly}) && $Parameters{cacheOnly}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $Parameters{"cacheOnly"} and $Parameters{"cacheOnly"}
MC/DC0%0/2
exists $Parameters{"cacheOnly"} and $Parameters{"cacheOnly"}
exists $Parameters{"cacheOnly"}
$Parameters{"cacheOnly"}
Truth table0%0/3
A and B
A= exists $Parameters{"cacheOnly"}B= $Parameters{"cacheOnly"}
| |||||||||||||||||||||||||||||
| 649 | |||||||||||||||||||||||||||||
| 650 | # if we tried to fetch it too many times already don't try again. | ||||||||||||||||||||||||||||
| 651 | 0 | ▶ | return undef if (exists ($Self->{problemUrls}->{$url}) && ($Self->{problemUrls}->{$url} >= $Self->{maxFetchAttempts})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $Self->{"problemUrls"}{$url} and $Self->{"problemUrls"}{$url} >= $Self->{"maxFetchAttempts"}
MC/DC0%0/2
exists $Self->{"problemUrls"}{$url} and $Self->{"problemUrls"}{$url} >= $Self->{"maxFetchAttempts"}
exists $Self->{"problemUrls"}{$url}
$Self->{"problemUrls"}{$url} >= $Self->{"maxFetchAttempts"}
Truth table0%0/3
A and B
A= exists $Self->{"problemUrls"}{$url}B= $Self->{"problemUrls"}{$url} >= $Self->{"maxFetchAttempts"}
| |||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | # wait before fetching the page. | ||||||||||||||||||||||||||||
| 654 | 0 | my $delay = $Self->{delayInSeconds} - (time - $Self->{timeOfLastPageFetch}); | |||||||||||||||||||||||||||
| 655 | 0 | ▶ | sleep $delay if ($delay > 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | # fetch the page. | ||||||||||||||||||||||||||||
| 658 | 0 | $pageContents = get ($url); | |||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | 0 | $Self->{timeOfLastPageFetch} = time; | |||||||||||||||||||||||||||
| 661 | 0 | ▶ | unless (defined ($pageContents)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 662 | { | ||||||||||||||||||||||||||||
| 663 | 0 | $Self->{problemUrls}->{$url} += 5; | |||||||||||||||||||||||||||
| 664 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 665 | 0 | $logger->logwarn ("problems fetching page '$url'.\n"); | |||||||||||||||||||||||||||
| 666 | 0 | return undef; | |||||||||||||||||||||||||||
| 667 | } | ||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | # cache the page | ||||||||||||||||||||||||||||
| 670 | 0 | $cacheEngine->set ($md5OfUrl, $pageContents, $Self->{cacheExpiration}); | |||||||||||||||||||||||||||
| 671 | 0 | $Self->{urlsInCache}->{$url} = 1; | |||||||||||||||||||||||||||
| 672 | |||||||||||||||||||||||||||||
| 673 | # return its contents, decoded. | ||||||||||||||||||||||||||||
| 674 | 0 | my $encoding = encoding_from_html_document($pageContents, xhtml => 0); | |||||||||||||||||||||||||||
| 675 | 0 | ▶ | $encoding = encoding_from_html_document($pageContents, xhtml => 1) unless defined $encoding; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 676 | 0 | ▶ | unless (defined $encoding) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 677 | { | ||||||||||||||||||||||||||||
| 678 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 679 | 0 | $logger->logwarn ("could not determine encoding, defaulting to iso-8859-1: $md5OfUrl: $url\n"); | |||||||||||||||||||||||||||
| 680 | } | ||||||||||||||||||||||||||||
| 681 | 0 | ▶ | $encoding = 'iso-8859-1' unless defined $encoding; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 682 | 0 | $pageContents = decode ($encoding, $pageContents); | |||||||||||||||||||||||||||
| 683 | 0 | return [$pageContents, $encoding]; | |||||||||||||||||||||||||||
| 684 | } | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | # adds list of articles of the for listOfArticles => [[url, html], ..., [url, html]] | ||||||||||||||||||||||||||||
| 687 | # to the index and cache. if an article is already in the cache it is not added. | ||||||||||||||||||||||||||||
| 688 | # returns the total number of articles added. | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | sub addListOfArticles # (listOfArticles => [[url, html], ..., [url, html]]) | ||||||||||||||||||||||||||||
| 691 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
addListOfArticles: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 692 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 693 | |||||||||||||||||||||||||||||
| 694 | # get the uri index/hash. | ||||||||||||||||||||||||||||
| 695 | 0 | my $urlIndex = $Self->{urlIndex}; | |||||||||||||||||||||||||||
| 696 | 0 | my $urlHash = $Self->{urlHash}; | |||||||||||||||||||||||||||
| 697 | 0 | my $totalDocuments = $Self->getTotalDocuments (); | |||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | # get the caching object. | ||||||||||||||||||||||||||||
| 700 | 0 | my $cacheEngine = $Self->{cacheEngine}; | |||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||
| 702 | # get the list of articles to add. | ||||||||||||||||||||||||||||
| 703 | 0 | my $listOfArticles = $Parameters{listOfArticles}; | |||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | 0 | my $totalAdded = 0; | |||||||||||||||||||||||||||
| 706 | 0 | foreach my $urlHtmlPair (@$listOfArticles) | |||||||||||||||||||||||||||
| 707 | { | ||||||||||||||||||||||||||||
| 708 | # get the url of the article. | ||||||||||||||||||||||||||||
| 709 | 0 | my $url = $urlHtmlPair->[0]; | |||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||
| 711 | # skip the article if already in the cache. | ||||||||||||||||||||||||||||
| 712 | 0 | ▶ | if ($Self->_isDocumentInCache (url => $url) && exists ($urlHash->{$url})) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$Self->_isDocumentInCache("url", $url) and exists $urlHash->{$url}
MC/DC0%0/2
$Self->_isDocumentInCache("url", $url) and exists $urlHash->{$url}
$Self->_isDocumentInCache("url", $url)
exists $urlHash->{$url}
Truth table0%0/3
A and B
A= $Self->_isDocumentInCache("url", $url)B= exists $urlHash->{$url}
| |||||||||||||||||||||||||||||
| 713 | { | ||||||||||||||||||||||||||||
| 714 | 0 | ▶ | if ($Self->{verbose} > 1) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 715 | { | ||||||||||||||||||||||||||||
| 716 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 717 | 0 | $logger->info ("cache hit: $url\n"); | |||||||||||||||||||||||||||
| 718 | } | ||||||||||||||||||||||||||||
| 719 | 0 | next; | |||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||
| 722 | 0 | ▶ | if ($Self->{verbose}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 723 | { | ||||||||||||||||||||||||||||
| 724 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 725 | 0 | $logger->info ("cache miss: $url\n"); | |||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||
| 728 | # get the html content of the page. | ||||||||||||||||||||||||||||
| 729 | 0 | my $htmlContent = $urlHtmlPair->[1]; | |||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||
| 731 | # need to convert url to md5hex since cache uses it as filename | ||||||||||||||||||||||||||||
| 732 | # and urls could be too long. | ||||||||||||||||||||||||||||
| 733 | 0 | my $md5OfUrl = md5_hex ($url); | |||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | # cache the page. | ||||||||||||||||||||||||||||
| 736 | 0 | $cacheEngine->set ($md5OfUrl, $htmlContent, $Self->{cacheExpiration}); | |||||||||||||||||||||||||||
| 737 | |||||||||||||||||||||||||||||
| 738 | # update the index to the documents. | ||||||||||||||||||||||||||||
| 739 | 0 | $urlIndex->[$totalDocuments] = $url; | |||||||||||||||||||||||||||
| 740 | 0 | $urlHash->{$url} = $totalDocuments; | |||||||||||||||||||||||||||
| 741 | 0 | ++$totalDocuments; | |||||||||||||||||||||||||||
| 742 | 0 | ++$totalAdded; | |||||||||||||||||||||||||||
| 743 | } | ||||||||||||||||||||||||||||
| 744 | |||||||||||||||||||||||||||||
| 745 | # store the urls in the cache. it is really slow to store this to the disk cache each time | ||||||||||||||||||||||||||||
| 746 | # a new document url is added. | ||||||||||||||||||||||||||||
| 747 | 0 | $cacheEngine->set ('urlIndex', $urlIndex, $Self->{cacheExpiration}); | |||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||
| 749 | 0 | return $totalAdded; | |||||||||||||||||||||||||||
| 750 | } | ||||||||||||||||||||||||||||
| 751 | |||||||||||||||||||||||||||||
| 752 | # writes the contents of the cache to the file csvFile => '...' in | ||||||||||||||||||||||||||||
| 753 | # csv format where each line is of the form url,html | ||||||||||||||||||||||||||||
| 754 | # all formating and higher order bits are escaped using | ||||||||||||||||||||||||||||
| 755 | # URI::Escape::uri_escape. | ||||||||||||||||||||||||||||
| 756 | |||||||||||||||||||||||||||||
| 757 | sub exportCacheToCsvFile | ||||||||||||||||||||||||||||
| 758 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
exportCacheToCsvFile: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 759 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 760 | |||||||||||||||||||||||||||||
| 761 | # get the total documents in the cache. | ||||||||||||||||||||||||||||
| 762 | 0 | my $totalDocuments = $Self->getTotalDocuments(); | |||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | # get the path of the csv file. | ||||||||||||||||||||||||||||
| 765 | 0 | my $csvFile = $Parameters{csvFile}; | |||||||||||||||||||||||||||
| 766 | 0 | my $fh; | |||||||||||||||||||||||||||
| 767 | 0 | ▶ | unless (open ($fh, ">$csvFile")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 768 | { | ||||||||||||||||||||||||||||
| 769 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 770 | 0 | $logger->logdie ("could no open file '$csvFile' for writing: $!\n"); | |||||||||||||||||||||||||||
| 771 | } | ||||||||||||||||||||||||||||
| 772 | |||||||||||||||||||||||||||||
| 773 | 0 | my $charsToEscape = "\x00-\x1f\x7f-\xff,"; | |||||||||||||||||||||||||||
| 774 | 0 | ▶ | for (my $i = 0; $i < $totalDocuments; $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 775 | { | ||||||||||||||||||||||||||||
| 776 | eval | ||||||||||||||||||||||||||||
| 777 | 0 | { | |||||||||||||||||||||||||||
| 778 | # fetch the document. | ||||||||||||||||||||||||||||
| 779 | 0 | my $document = $Self->getDocument(index => $i, cacheOnly => 1); | |||||||||||||||||||||||||||
| 780 | 0 | ▶ | if (defined ($document)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 781 | { | ||||||||||||||||||||||||||||
| 782 | # get the raw html of the document. | ||||||||||||||||||||||||||||
| 783 | 0 | my $html = $document->getHtml(); | |||||||||||||||||||||||||||
| 784 | 0 | ▶ | next unless defined $html; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||
| 786 | # get the uri of the document. | ||||||||||||||||||||||||||||
| 787 | 0 | my $uri = $document->getUri(); | |||||||||||||||||||||||||||
| 788 | 0 | ▶ | next unless defined $html; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | # escape all but letters and numbers. | ||||||||||||||||||||||||||||
| 791 | 0 | $html = uri_escape ($html, $charsToEscape); | |||||||||||||||||||||||||||
| 792 | 0 | $uri = uri_escape ($uri, $charsToEscape); | |||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||
| 794 | # write the escaped uri and html to the file. | ||||||||||||||||||||||||||||
| 795 | 0 | print $fh $uri . ',' . $html . "\n"; | |||||||||||||||||||||||||||
| 796 | } | ||||||||||||||||||||||||||||
| 797 | else | ||||||||||||||||||||||||||||
| 798 | { | ||||||||||||||||||||||||||||
| 799 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 800 | 0 | $logger->logwarn ('problems with document number ' . $i . "; document skipped.\n"); | |||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||
| 802 | }; | ||||||||||||||||||||||||||||
| 803 | } | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | 0 | close $fh; | |||||||||||||||||||||||||||
| 806 | } | ||||||||||||||||||||||||||||
| 807 | |||||||||||||||||||||||||||||
| 808 | sub importCsvFileIntoCache | ||||||||||||||||||||||||||||
| 809 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
importCsvFileIntoCache: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 810 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 811 | |||||||||||||||||||||||||||||
| 812 | # get the path of the csv file. | ||||||||||||||||||||||||||||
| 813 | 0 | my $csvFile = $Parameters{csvFile}; | |||||||||||||||||||||||||||
| 814 | 0 | local *IN; | |||||||||||||||||||||||||||
| 815 | 0 | ▶ | unless (open (IN, "$csvFile")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 816 | { | ||||||||||||||||||||||||||||
| 817 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 818 | 0 | $logger->logdie ("could no open file '$csvFile' for reading: $!\n"); | |||||||||||||||||||||||||||
| 819 | } | ||||||||||||||||||||||||||||
| 820 | |||||||||||||||||||||||||||||
| 821 | 0 | my @listOfArticles; | |||||||||||||||||||||||||||
| 822 | 0 | ▶ | while (my $line = <IN>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 823 | { | ||||||||||||||||||||||||||||
| 824 | # remove the end line chars. | ||||||||||||||||||||||||||||
| 825 | 0 | chop $line; | |||||||||||||||||||||||||||
| 826 | |||||||||||||||||||||||||||||
| 827 | # split the line at the comma. | ||||||||||||||||||||||||||||
| 828 | 0 | my ($uri, $htmlContent) = split (/,/, $line); | |||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||
| 830 | # unescape the uri and content. | ||||||||||||||||||||||||||||
| 831 | 0 | $uri = uri_unescape ($uri); | |||||||||||||||||||||||||||
| 832 | 0 | $htmlContent = uri_unescape ($htmlContent); | |||||||||||||||||||||||||||
| 833 | 0 | push @listOfArticles, [$uri, $htmlContent]; | |||||||||||||||||||||||||||
| 834 | |||||||||||||||||||||||||||||
| 835 | # add the list of articles to the cache. | ||||||||||||||||||||||||||||
| 836 | 0 | ▶ | if (@listOfArticles > 100) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 837 | { | ||||||||||||||||||||||||||||
| 838 | 0 | $Self->addListOfArticles (listOfArticles => \@listOfArticles); | |||||||||||||||||||||||||||
| 839 | 0 | @listOfArticles = (); | |||||||||||||||||||||||||||
| 840 | } | ||||||||||||||||||||||||||||
| 841 | } | ||||||||||||||||||||||||||||
| 842 | 0 | close IN; | |||||||||||||||||||||||||||
| 843 | |||||||||||||||||||||||||||||
| 844 | # add the remaining list of articles to the cache. | ||||||||||||||||||||||||||||
| 845 | 0 | ▶ | if (@listOfArticles > 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 846 | { | ||||||||||||||||||||||||||||
| 847 | 0 | $Self->addListOfArticles (listOfArticles => \@listOfArticles); | |||||||||||||||||||||||||||
| 848 | 0 | @listOfArticles = (); | |||||||||||||||||||||||||||
| 849 | } | ||||||||||||||||||||||||||||
| 850 | |||||||||||||||||||||||||||||
| 851 | 0 | return undef; | |||||||||||||||||||||||||||
| 852 | } | ||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||
| 854 | # reads the index of the documents from the cache. | ||||||||||||||||||||||||||||
| 855 | sub _getIndex | ||||||||||||||||||||||||||||
| 856 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_getIndex: called CC=6 SCAR=18.6
| |||||||||||||||||||||||||||||
| 857 | 1 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 858 | |||||||||||||||||||||||||||||
| 859 | # return if already set. | ||||||||||||||||||||||||||||
| 860 | 1 | ▶ | return undef if exists $Self->{urlIndex}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||
| 862 | # get the index. | ||||||||||||||||||||||||||||
| 863 | 1 | my $urlIndex = $Self->{cacheEngine}->get ('urlIndex'); | |||||||||||||||||||||||||||
| 864 | 1 | my $problemUrls = $Self->{cacheEngine}->get ('problemUrls'); | |||||||||||||||||||||||||||
| 865 | 1 | my $urlsInCache = $Self->{cacheEngine}->get ('urlsInCache'); | |||||||||||||||||||||||||||
| 866 | |||||||||||||||||||||||||||||
| 867 | # set the index to empty if it did not exist. | ||||||||||||||||||||||||||||
| 868 | 1 | ▶ | $urlIndex = [] unless defined $urlIndex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 869 | 1 | ▶ | $problemUrls = {} unless defined $problemUrls; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 870 | 1 | ▶ | $urlsInCache = {} unless defined $urlsInCache; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 871 | |||||||||||||||||||||||||||||
| 872 | # create the reverse hash of the index. | ||||||||||||||||||||||||||||
| 873 | 1 | my %urlHash; | |||||||||||||||||||||||||||
| 874 | 1 | ▶ | for (my $i = 0; $i < @$urlIndex; $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 875 | { | ||||||||||||||||||||||||||||
| 876 | 0 | $urlHash{$urlIndex->[$i]} = $i; | |||||||||||||||||||||||||||
| 877 | } | ||||||||||||||||||||||||||||
| 878 | |||||||||||||||||||||||||||||
| 879 | # store the index and exit. | ||||||||||||||||||||||||||||
| 880 | 1 | $Self->{urlIndex} = $urlIndex; | |||||||||||||||||||||||||||
| 881 | 1 | $Self->{urlHash} = \%urlHash; | |||||||||||||||||||||||||||
| 882 | 1 | $Self->{problemUrls} = $problemUrls; | |||||||||||||||||||||||||||
| 883 | 1 | $Self->{urlsInCache} = $urlsInCache; | |||||||||||||||||||||||||||
| 884 | 1 | return undef; | |||||||||||||||||||||||||||
| 885 | } | ||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||
| 887 | sub _primeCache | ||||||||||||||||||||||||||||
| 888 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_primeCache: not called CC=13 SCAR=52.0
| |||||||||||||||||||||||||||||
| 889 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 890 | |||||||||||||||||||||||||||||
| 891 | # get the logger, will print a warning if not initialized. | ||||||||||||||||||||||||||||
| 892 | 0 | my $logger = Log::Log4perl->get_logger(); | |||||||||||||||||||||||||||
| 893 | |||||||||||||||||||||||||||||
| 894 | # set default verbose level. | ||||||||||||||||||||||||||||
| 895 | 0 | ▶ | $Parameters{verbose} = 0 unless exists $Parameters{verbose}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 896 | |||||||||||||||||||||||||||||
| 897 | # count the articles to fetch. | ||||||||||||||||||||||||||||
| 898 | 0 | my $totalDocuments = $Self->getTotalDocuments (); | |||||||||||||||||||||||||||
| 899 | 0 | my @documentsToFetch; | |||||||||||||||||||||||||||
| 900 | 0 | ▶ | for (my $i = 0; $i < $totalDocuments; $i++) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 901 | { | ||||||||||||||||||||||||||||
| 902 | 0 | my $url = $Self->{urlIndex}->[$i]; | |||||||||||||||||||||||||||
| 903 | 0 | ▶ | next if (exists ($Self->{problemUrls}->{$url}) && ($Self->{problemUrls}->{$url} >= $Self->{maxFetchAttempts})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
exists $Self->{"problemUrls"}{$url} and $Self->{"problemUrls"}{$url} >= $Self->{"maxFetchAttempts"}
MC/DC0%0/2
exists $Self->{"problemUrls"}{$url} and $Self->{"problemUrls"}{$url} >= $Self->{"maxFetchAttempts"}
exists $Self->{"problemUrls"}{$url}
$Self->{"problemUrls"}{$url} >= $Self->{"maxFetchAttempts"}
Truth table0%0/3
A and B
A= exists $Self->{"problemUrls"}{$url}B= $Self->{"problemUrls"}{$url} >= $Self->{"maxFetchAttempts"}
| |||||||||||||||||||||||||||||
| 904 | 0 | ▶ | next if exists $Self->{urlsInCache}->{$url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 905 | 0 | ▶ | if ($Self->_isDocumentInCache (index => $i)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 906 | { | ||||||||||||||||||||||||||||
| 907 | 0 | $Self->{urlsInCache}->{$url} = 1; | |||||||||||||||||||||||||||
| 908 | 0 | next; | |||||||||||||||||||||||||||
| 909 | } | ||||||||||||||||||||||||||||
| 910 | 0 | push @documentsToFetch, $i; | |||||||||||||||||||||||||||
| 911 | } | ||||||||||||||||||||||||||||
| 912 | |||||||||||||||||||||||||||||
| 913 | # fetch the documents. | ||||||||||||||||||||||||||||
| 914 | 0 | my $documentsLeft = scalar @documentsToFetch; | |||||||||||||||||||||||||||
| 915 | 0 | my $totalDocumentsFetched = 0; | |||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||
| 917 | 0 | foreach my $i (@documentsToFetch) | |||||||||||||||||||||||||||
| 918 | { | ||||||||||||||||||||||||||||
| 919 | # compute the time remaining. | ||||||||||||||||||||||||||||
| 920 | 0 | ▶ | if ($Parameters{verbose}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 921 | { | ||||||||||||||||||||||||||||
| 922 | 0 | my $timeRemaining = $documentsLeft * $Self->{delayInSeconds}; | |||||||||||||||||||||||||||
| 923 | 0 | ▶ | $timeRemaining = 1 if ($timeRemaining < 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 924 | 0 | $timeRemaining = _convertDurationInSecondsToWords ($timeRemaining); | |||||||||||||||||||||||||||
| 925 | 0 | my $message = $documentsLeft . ' documents left to fetch; time remaining about ' . | |||||||||||||||||||||||||||
| 926 | $timeRemaining . ".\n"; | ||||||||||||||||||||||||||||
| 927 | 0 | $logger->info ($message); | |||||||||||||||||||||||||||
| 928 | } | ||||||||||||||||||||||||||||
| 929 | |||||||||||||||||||||||||||||
| 930 | # fetch the document and put it in the cache. | ||||||||||||||||||||||||||||
| 931 | 0 | my $content; | |||||||||||||||||||||||||||
| 932 | 0 | eval {$content = $Self->_getArticle (index => $i, %Parameters);}; | |||||||||||||||||||||||||||
| 933 | 0 | ▶ | $Self->{problemUrls}->{$Self->{urlIndex}->[$i]} += 1 unless defined $content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||
| 935 | # keep track of the documents left. | ||||||||||||||||||||||||||||
| 936 | 0 | --$documentsLeft; | |||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||
| 938 | # count documents fetched. | ||||||||||||||||||||||||||||
| 939 | 0 | ++$totalDocumentsFetched; | |||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||
| 941 | # if just testing fetch only one document. | ||||||||||||||||||||||||||||
| 942 | 0 | ▶ | last if ($totalDocumentsFetched && exists ($Parameters{testing})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$totalDocumentsFetched and exists $Parameters{"testing"}
MC/DC0%0/2
$totalDocumentsFetched and exists $Parameters{"testing"}
$totalDocumentsFetched
exists $Parameters{"testing"}
Truth table0%0/3
A and B
A= $totalDocumentsFetchedB= exists $Parameters{"testing"}
| |||||||||||||||||||||||||||||
| 943 | } | ||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||
| 945 | # update the problemUrls data in the cache. | ||||||||||||||||||||||||||||
| 946 | 0 | $Self->{cacheEngine}->set ('problemUrls', $Self->{problemUrls}, $Self->{cacheExpiration}); | |||||||||||||||||||||||||||
| 947 | 0 | $Self->{cacheEngine}->set ('urlsInCache', $Self->{urlsInCache}, $Self->{cacheExpiration}); | |||||||||||||||||||||||||||
| 948 | |||||||||||||||||||||||||||||
| 949 | 0 | return $totalDocumentsFetched; | |||||||||||||||||||||||||||
| 950 | } | ||||||||||||||||||||||||||||
| 951 | |||||||||||||||||||||||||||||
| 952 | # returns true of the document with specified md5OfUrl, url, or index is in the cache. | ||||||||||||||||||||||||||||
| 953 | sub _isDocumentInCache # (index => ...) | ||||||||||||||||||||||||||||
| 954 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_isDocumentInCache: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 955 | 0 | my ($Self, %Parameters) = @_; | |||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||
| 957 | # get the url to check. | ||||||||||||||||||||||||||||
| 958 | 0 | my $md5OfUrl; | |||||||||||||||||||||||||||
| 959 | 0 | ▶ | $md5OfUrl = $Parameters{md5OfUrl} if exists $Parameters{md5OfUrl}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 960 | |||||||||||||||||||||||||||||
| 961 | 0 | my $url; | |||||||||||||||||||||||||||
| 962 | 0 | ▶ | $url = $Parameters{url} if exists $Parameters{url}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 963 | 0 | ▶ | $url = $Self->{urlIndex}->[$Parameters{index}] if (!defined ($url) && exists ($Parameters{index})); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $url and exists $Parameters{"index"}
MC/DC0%0/2
not defined $url and exists $Parameters{"index"}
not defined $url
exists $Parameters{"index"}
Truth table0%0/3
not(A) and B
A= not defined $urlB= exists $Parameters{"index"}
| |||||||||||||||||||||||||||||
| 964 | |||||||||||||||||||||||||||||
| 965 | # need to convert url to md5hex since cache uses it as filename | ||||||||||||||||||||||||||||
| 966 | # and urls could be too long. | ||||||||||||||||||||||||||||
| 967 | 0 | ▶ | $md5OfUrl = md5_hex ($url) if (!defined ($md5OfUrl) && defined ($url)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $md5OfUrl and defined $url
MC/DC0%0/2
not defined $md5OfUrl and defined $url
not defined $md5OfUrl
defined $url
Truth table0%0/3
not(A) and B
A= not defined $md5OfUrlB= defined $url
| |||||||||||||||||||||||||||||
| 968 | |||||||||||||||||||||||||||||
| 969 | # if the page is in the cache, return it from there. | ||||||||||||||||||||||||||||
| 970 | 0 | my $cacheEngine = $Self->{cacheEngine}; | |||||||||||||||||||||||||||
| 971 | |||||||||||||||||||||||||||||
| 972 | 0 | return $cacheEngine->is_valid ($md5OfUrl); | |||||||||||||||||||||||||||
| 973 | } | ||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||
| 975 | sub _convertDurationInSecondsToWords | ||||||||||||||||||||||||||||
| 976 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_convertDurationInSecondsToWords: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 977 | 0 | my $DurationInSeconds = shift; | |||||||||||||||||||||||||||
| 978 | |||||||||||||||||||||||||||||
| 979 | 0 | my $duration = $DurationInSeconds; | |||||||||||||||||||||||||||
| 980 | 0 | my $timeInPast = ''; | |||||||||||||||||||||||||||
| 981 | 0 | ▶ | if ($duration < 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 982 | { | ||||||||||||||||||||||||||||
| 983 | 0 | $duration = -$duration; | |||||||||||||||||||||||||||
| 984 | 0 | $timeInPast = ' in the past'; | |||||||||||||||||||||||||||
| 985 | } | ||||||||||||||||||||||||||||
| 986 | |||||||||||||||||||||||||||||
| 987 | 0 | my @timeUnits = ([60, 'seconds'], [60, 'minutes'], [24, 'hours'], [7, 'days'], [365.25/(12*7), 'weeks'], [12, 'months'], [10, 'years']); | |||||||||||||||||||||||||||
| 988 | |||||||||||||||||||||||||||||
| 989 | 0 | foreach my $timeUnit (@timeUnits) | |||||||||||||||||||||||||||
| 990 | { | ||||||||||||||||||||||||||||
| 991 | 0 | ▶ | if ($duration < $timeUnit->[0]) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 992 | { | ||||||||||||||||||||||||||||
| 993 | 0 | $duration = int ($duration * 100) / 100; | |||||||||||||||||||||||||||
| 994 | 0 | return $duration . ' ' . $timeUnit->[1] . $timeInPast; | |||||||||||||||||||||||||||
| 995 | } | ||||||||||||||||||||||||||||
| 996 | 0 | $duration /= $timeUnit->[0]; | |||||||||||||||||||||||||||
| 997 | } | ||||||||||||||||||||||||||||
| 998 | 0 | return $duration . ' decades' . $timeInPast; | |||||||||||||||||||||||||||
| 999 | } | ||||||||||||||||||||||||||||
| 1000 | |||||||||||||||||||||||||||||
| 1001 | =head1 EXAMPLES | ||||||||||||||||||||||||||||
| 1002 | |||||||||||||||||||||||||||||
| 1003 | The example below will print out all the information for each document in the corpus. | ||||||||||||||||||||||||||||
| 1004 | |||||||||||||||||||||||||||||
| 1005 | use Cwd; | ||||||||||||||||||||||||||||
| 1006 | use File::Spec; | ||||||||||||||||||||||||||||
| 1007 | use Text::Corpus::CNN; | ||||||||||||||||||||||||||||
| 1008 | use Data::Dump qw(dump); | ||||||||||||||||||||||||||||
| 1009 | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||
| 1010 | Log::Log4perl->easy_init ($INFO); | ||||||||||||||||||||||||||||
| 1011 | my $corpusDirectory = File::Spec->catfile (getcwd(), 'corpus_cnn'); | ||||||||||||||||||||||||||||
| 1012 | my $corpus = Text::Corpus::CNN->new (corpusDirectory => $corpusDirectory); | ||||||||||||||||||||||||||||
| 1013 | my $totalDocuments = $corpus->getTotalDocuments; | ||||||||||||||||||||||||||||
| 1014 | for (my $i = 0; $i < $totalDocuments; $i++) | ||||||||||||||||||||||||||||
| 1015 | { | ||||||||||||||||||||||||||||
| 1016 | eval | ||||||||||||||||||||||||||||
| 1017 | { | ||||||||||||||||||||||||||||
| 1018 | my $document = $corpus->getDocument(index => $i); | ||||||||||||||||||||||||||||
| 1019 | next unless defined $document; | ||||||||||||||||||||||||||||
| 1020 | my %documentInfo; | ||||||||||||||||||||||||||||
| 1021 | $documentInfo{title} = $document->getTitle(); | ||||||||||||||||||||||||||||
| 1022 | $documentInfo{body} = $document->getBody(); | ||||||||||||||||||||||||||||
| 1023 | $documentInfo{content} = $document->getContent(); | ||||||||||||||||||||||||||||
| 1024 | $documentInfo{categories} = $document->getCategories(); | ||||||||||||||||||||||||||||
| 1025 | $documentInfo{description} = $document->getDescription(); | ||||||||||||||||||||||||||||
| 1026 | $documentInfo{highlights} = $document->getHighlights(); | ||||||||||||||||||||||||||||
| 1027 | $documentInfo{uri} = $document->getUri(); | ||||||||||||||||||||||||||||
| 1028 | dump \%documentInfo; | ||||||||||||||||||||||||||||
| 1029 | }; | ||||||||||||||||||||||||||||
| 1030 | } | ||||||||||||||||||||||||||||
| 1031 | |||||||||||||||||||||||||||||
| 1032 | The example below will print some of the most frequent categories of all the articles in the corpus. | ||||||||||||||||||||||||||||
| 1033 | |||||||||||||||||||||||||||||
| 1034 | use Cwd; | ||||||||||||||||||||||||||||
| 1035 | use File::Spec; | ||||||||||||||||||||||||||||
| 1036 | use Text::Corpus::CNN; | ||||||||||||||||||||||||||||
| 1037 | use Data::Dump qw(dump); | ||||||||||||||||||||||||||||
| 1038 | use Log::Log4perl qw(:easy); | ||||||||||||||||||||||||||||
| 1039 | Log::Log4perl->easy_init ($INFO); | ||||||||||||||||||||||||||||
| 1040 | my $corpusDirectory = File::Spec->catfile (getcwd(), 'corpus_cnn'); | ||||||||||||||||||||||||||||
| 1041 | my $corpus = Text::Corpus::CNN->new (corpusDirectory => $corpusDirectory); | ||||||||||||||||||||||||||||
| 1042 | my $totalDocuments = $corpus->getTotalDocuments; | ||||||||||||||||||||||||||||
| 1043 | my %allCategories; | ||||||||||||||||||||||||||||
| 1044 | for (my $i = 0; $i < $totalDocuments; $i++) | ||||||||||||||||||||||||||||
| 1045 | { | ||||||||||||||||||||||||||||
| 1046 | eval | ||||||||||||||||||||||||||||
| 1047 | { | ||||||||||||||||||||||||||||
| 1048 | my $document = $corpus->getDocument(index => $i); | ||||||||||||||||||||||||||||
| 1049 | next unless defined $document; | ||||||||||||||||||||||||||||
| 1050 | my $categories = $document->getCategories(); | ||||||||||||||||||||||||||||
| 1051 | foreach my $category (@$categories) | ||||||||||||||||||||||||||||
| 1052 | { | ||||||||||||||||||||||||||||
| 1053 | my $categoryNormalized = lc $category; | ||||||||||||||||||||||||||||
| 1054 | $allCategories{$categoryNormalized} = [0, $category] unless exists $allCategories{$categoryNormalized}; | ||||||||||||||||||||||||||||
| 1055 | $allCategories{$categoryNormalized}->[0]++; | ||||||||||||||||||||||||||||
| 1056 | } | ||||||||||||||||||||||||||||
| 1057 | }; | ||||||||||||||||||||||||||||
| 1058 | } | ||||||||||||||||||||||||||||
| 1059 | my @allCategories = sort {$b->[0] <=> $a->[0]} values %allCategories; | ||||||||||||||||||||||||||||
| 1060 | my $topCategories = 10; | ||||||||||||||||||||||||||||
| 1061 | $topCategories = @allCategories if (@allCategories < $topCategories); | ||||||||||||||||||||||||||||
| 1062 | for (my $i = 0; $i < $topCategories; $i++) | ||||||||||||||||||||||||||||
| 1063 | { | ||||||||||||||||||||||||||||
| 1064 | print join (' ', @{$allCategories[$i]}) . "\n"; | ||||||||||||||||||||||||||||
| 1065 | } | ||||||||||||||||||||||||||||
| 1066 | |||||||||||||||||||||||||||||
| 1067 | =head1 INSTALLATION | ||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||
| 1069 | To install the module set C<TEXT_CORPUS_CNN_FULL_TESTING> to true and | ||||||||||||||||||||||||||||
| 1070 | run the following commands: | ||||||||||||||||||||||||||||
| 1071 | |||||||||||||||||||||||||||||
| 1072 | perl Makefile.PL | ||||||||||||||||||||||||||||
| 1073 | make | ||||||||||||||||||||||||||||
| 1074 | make test | ||||||||||||||||||||||||||||
| 1075 | make install | ||||||||||||||||||||||||||||
| 1076 | |||||||||||||||||||||||||||||
| 1077 | If you are on a windows box you should use 'nmake' rather than 'make'. | ||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||
| 1079 | The module will install if C<TEXT_CORPUS_CNN_FULL_TESTING> is not defined | ||||||||||||||||||||||||||||
| 1080 | or false, but little testing will be performed. | ||||||||||||||||||||||||||||
| 1081 | |||||||||||||||||||||||||||||
| 1082 | =head1 BUGS | ||||||||||||||||||||||||||||
| 1083 | |||||||||||||||||||||||||||||
| 1084 | This module uses xpath expressions to extract links and text which may become invalid | ||||||||||||||||||||||||||||
| 1085 | as the format of various pages change, causing a lot of bugs. | ||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||
| 1087 | Please email bugs reports or feature requests to C<text-corpus-cnn@rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 1088 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Text-Corpus-CNN>. The author | ||||||||||||||||||||||||||||
| 1089 | will be notified and you can be automatically notified of progress on the bug fix or feature request. | ||||||||||||||||||||||||||||
| 1090 | |||||||||||||||||||||||||||||
| 1091 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 1092 | |||||||||||||||||||||||||||||
| 1093 | Jeff Kubina<jeff.kubina@gmail.com> | ||||||||||||||||||||||||||||
| 1094 | |||||||||||||||||||||||||||||
| 1095 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 1096 | |||||||||||||||||||||||||||||
| 1097 | Copyright (c) 2009 Jeff Kubina. All rights reserved. | ||||||||||||||||||||||||||||
| 1098 | This program is free software; you can redistribute | ||||||||||||||||||||||||||||
| 1099 | it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 1100 | |||||||||||||||||||||||||||||
| 1101 | The full text of the license can be found in the | ||||||||||||||||||||||||||||
| 1102 | LICENSE file included with this module. | ||||||||||||||||||||||||||||
| 1103 | |||||||||||||||||||||||||||||
| 1104 | =head1 KEYWORDS | ||||||||||||||||||||||||||||
| 1105 | |||||||||||||||||||||||||||||
| 1106 | cnn, cable news network, corpus, english corpus, information processing | ||||||||||||||||||||||||||||
| 1107 | |||||||||||||||||||||||||||||
| 1108 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||
| 1110 | =begin html | ||||||||||||||||||||||||||||
| 1111 | |||||||||||||||||||||||||||||
| 1112 | Read the <a href="http://www.cnn.com/interactive_legal.html">CNN Interactive Service Agreement</a> to ensure you abide by it | ||||||||||||||||||||||||||||
| 1113 | when using this module. | ||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||
| 1115 | =end html | ||||||||||||||||||||||||||||
| 1116 | |||||||||||||||||||||||||||||
| 1117 | L<CHI>, L<Log::Log4perl>, L<Text::Corpus::CNN::Document> | ||||||||||||||||||||||||||||
| 1118 | |||||||||||||||||||||||||||||
| 1119 | =cut | ||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||
| 1121 | 1 | 1; | |||||||||||||||||||||||||||
| 1122 | # The preceding line will help the module return a true value | ||||||||||||||||||||||||||||