NewYorkTimes/Document.pm

filter: Statementstmt 17.1% 33 / 193 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 37.5% 9 / 24 Podpod 57.1% 8 / 14 totaltotal 14.4% 50 / 345 CC 58 SCAR 39.3
CC
58
Cov
11%
new
132.0
getDate
110.0
getBody
56.0
CRAP 51.0
↑ Summary NewYorkTimes.pm »
1 package Text::Corpus::NewYorkTimes::Document;
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 XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Path::Class qw(dir file);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Date::Manip;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 BEGIN
11 1 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶ use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 $VERSION = '1.01';
15 1 @ISA = qw();
16 1 @EXPORT = qw();
17 1 @EXPORT_OK = qw();
18 1 %EXPORT_TAGS = ();
19 }
20  
21 #12345678901234567890123456789012345678901234
22 #Parse NYT article for research.
23  
24 =head1 NAME
25  
26 C<Text::Corpus::NewYorkTimes::Document> - Parse NYT article for research.
27  
28 =head1 SYNOPSIS
29  
30 use Text::Corpus::NewYorkTimes;
31 use Data::Dump qw(dump);
32 use Log::Log4perl qw(:easy);
33 Log::Log4perl->easy_init ($INFO);
34 my $corpus = Text::Corpus::NewYorkTimes->new (fileList => $fileList, corpusDirectory => $corpusDirectory);
35 my $document = $corpus->getDocument (index => 0);
36 dump $document->getBody;
37 dump $document->getCategories;
38 dump $document->getContent;
39 dump $document->getDate;
40 dump $document->getDescription;
41 dump $document->getTitle;
42 dump $document->getUri;
43  
44 =head1 DESCRIPTION
45  
46 C<Text::Corpus::NewYorkTimes::Document> provides methods for accessing specific
47 portions of news articles from the New York Times corpus.
48  
49 =head1 CONSTRUCTOR
50  
51 =head2 C<new>
52  
53 The constructor C<new> creates an instance of the C<Text::Corpus::NewYorkTimes::Document> class with the following
54 parameters:
55  
56 =over
57  
58 =item C<filename>
59  
60 filename => '...'
61  
62 C<filename> is the path name to the XML document that is to be parsed.
63  
64 =item C<dtdname>
65  
66 dtdname => '...'
67  
68 C<dtdname> is the path name to the data type definition file provided with
69 the corpus; it is usually something like C<.../nyt_corpus/dtd/nitf-3-3.dtd>. If
70 not defined an attempt is made to located it using the path provided by
71 C<filename>.
72  
73 =back
74  
75 =cut
76  
77 # create the XPath object.
78 # filename => path to object.
79 {
80 1 my $libxmlParser;
81 1 my $dtdloaded;
82  
83 sub new
84 0 ▶ {
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
85 # create the class object.
86 0 my ($Class, %Parameters) = @_;
87 0 ▶ my $Self = bless {}, ref($Class) || $Class;
Condition0%0/3
ref $Class || $Class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $Class || $Class
ref $Class $Class
Truth table0%0/3
A || B
A= ref $ClassB= $Class
ABresult
1X1
011
000
88  
89 # make sure a file path is defined.
90 0 my $filename;
91 0 ▶ $filename = $Parameters{filename} if exists $Parameters{filename};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"filename"} 0 0 0
92 0 ▶ $filename = $Parameters{uri} if exists $Parameters{uri};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"uri"} 0 0 0
93
94 # create the parser just once, not thread safe.
95 0 ▶ unless (defined ($libxmlParser))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $libxmlParser) 0 0 0
96 {
97 0 $libxmlParser = XML::LibXML->new();
98 0 $libxmlParser->load_ext_dtd(0);
99 0 $libxmlParser->no_network(1);
100  
101 0 ▶ if (!defined ($dtdloaded) && exists ($Parameters{dtdname}) && -f $Parameters{dtdname})
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $dtdloaded and exists $Parameters{'dtdname'} and -f $Parameters{'dtdname'}) { } 0 0 0
elsif (not defined $dtdloaded) { } 0 0 0
Condition0%0/6
not defined $dtdloaded and exists $Parameters{"dtdname"} and -f $Parameters{"dtdname"}
!ll&&!rl&&r
000
not defined $dtdloaded and exists $Parameters{"dtdname"}
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $dtdloaded and exists $Parameters{"dtdname"} and -f $Parameters{"dtdname"}
not defined $dtdloaded exists $Parameters{"dtdname"} -f $Parameters{"dtdname"}
Truth table0%0/4
not(A) and B and C
A= not defined $dtdloadedB= exists $Parameters{"dtdname"}C= -f $Parameters{"dtdname"}
ABCresult
0XX0
10X0
1100
1111
102 {
103 0 $libxmlParser->load_catalog($Parameters{dtdname});
104 0 $dtdloaded = 1;
105 }
106 elsif (!defined ($dtdloaded))
107 {
108 0 my $dtdFiles = findDtdFiles ($filename);
109 0 foreach my $file (@$dtdFiles)
110 {
111 0 $libxmlParser->load_catalog ($file);
112 }
113 0 $dtdloaded = 1;
114 }
115 }
116
117 0 ▶ if (defined ($filename))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename) 0 0 0
118 {
119 0 $Self->{filePath} = $filename;
120 0 my $doc = $libxmlParser->parse_file($filename);
121 0 $Self->{xpathEngine} = XML::LibXML::XPathContext->new($doc);
122 }
123  
124 0 return $Self;
125 }
126 }
127  
128 # attempts to locate the dtd files given the location of one of the xml files
129 # in the corpus. assumes that dtd directory has not been moved after the
130 # corpus was extracted.
131 sub findDtdFiles
132 1 ▶{
Subroutine findDtdFiles: not called CC=7 SCAR=39.3
Pod undocumented
133 0 my $XmlFilename = dir ($_[0])->absolute;
134 0 my @dirs = $XmlFilename->dir_list;
135 0 ▶ while (@dirs)
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 0 0 0
136 {
137 0 ▶ if ($dirs[-1] eq 'data')
Branch: 1 branch
BranchTrueFalseTotal
if ($dirs[-1] eq "data") 0 0 0
138 {
139 0 pop @dirs;
140 0 last;
141 }
142 0 pop @dirs;
143 }
144 0 my $dataFolder = Path::Class::Dir->new(@dirs, 'dtd');
145  
146 0 my @dtdFiles;
147 0 ▶ if (-d $dataFolder)
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dataFolder) 0 0 0
148 {
149 0 ▶ while (my $file = $dataFolder->next)
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $dataFolder->next) 0 0 0
150 {
151 0 ▶ next unless -f $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 0 0
152 0 my $fileStr = $file->stringify;
153 0 ▶ push @dtdFiles, $fileStr if ($fileStr =~ /\.dtd$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $fileStr =~ /\.dtd$/i 0 0 0
154 }
155 }
156 0 return \@dtdFiles;
157 }
158  
159 =head1 METHODS
160  
161 =head2 C<getBody>
162  
163 getBody ()
164  
165 C<getBody> returns an array reference of strings of sentences that are the body of the article.
166  
167 =cut
168  
169 sub getBody
170 0 ▶{
Subroutine getBody: not called CC=7 SCAR=40.3
Pod documented
171 0 my $Self = shift;
172  
173 # if already computed return the body.
174 0 ▶ return $Self->{body} if exists $Self->{body};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"body"} 0 0 0
175  
176 # get the article body.
177 0 my @linesOfText;
178 0 my @nodes = $Self->{xpathEngine}->findnodes('/nitf/body/body.content/block[@class="full_text"]/p');
179 0 foreach my $node (@nodes)
180 {
181 0 push @linesOfText, $node->textContent();
182 }
183 0 $Self->{body} = \@linesOfText;
184 0 ▶ return $Self->{body} unless (@linesOfText);
Branch: 1 branch
BranchTrueFalseTotal
unless @linesOfText 0 0 0
185  
186 # copy the first paragraph.
187 0 my $leadParagraph = $linesOfText[0];
188  
189 # pull off the word LEAD if it exists.
190 0 $leadParagraph =~ s/^\s*LEAD\s*:*\s*//i;
191  
192 # if the lead paragraph reoccurs, delete it, assuming it is exact.
193 0 my $leadParagraphReoccures = 0;
194 0 ▶ for (my $i = 1 ; $i < @linesOfText ; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @linesOfText) 0 0 0
195 {
196 0 ▶ if (index($linesOfText[$i], $leadParagraph) > -1)
Branch: 1 branch
BranchTrueFalseTotal
if (index($linesOfText[$i], $leadParagraph) != -1) 0 0 0
197 {
198 0 $leadParagraphReoccures = 1;
199 0 last;
200 }
201 }
202  
203 # pop off the lead paragraph if it repeats in the text body.
204 0 ▶ shift @linesOfText if ($leadParagraphReoccures);
Branch: 1 branch
BranchTrueFalseTotal
if $leadParagraphReoccures 0 0 0
205  
206 # return the title and lines of body text.
207 0 $Self->{body} = \@linesOfText;
208 0 return $Self->{body};
209 }
210  
211 =head2 C<getCategories>
212  
213 getCategories (type => 'all')
214  
215 The method C<getCategories> returns the categories of C<type> assigned to the document, where
216 C<type> must be C<'all'>, C<'controlled'>, or C<'uncontrolled'>. The C<'uncontrolled'>
217 categories are those assigned to the
218 document by an editor without machine assistance, the C<'controlled'> categories
219 are those assigned with machine assistance. The type C<'all'> returns the union of the
220 categories from C<'controlled'> and C<'uncontrolled'>. The default is C<'all'>.
221  
222 =cut
223  
224 # get the categories assigned to the document.
225 sub getCategories
226 0 ▶{
Subroutine getCategories: not called CC=6 SCAR=37.4
Pod documented
227 0 my ($Self, %Parameters) = shift;
228  
229 # set the category type.
230 0 my $type = 'a';
231 0 ▶ $type = lc substr ($Parameters{type}, 0, 1) if (exists ($Parameters{type}) && defined ($Parameters{type}) && length ($Parameters{type}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"type"} and defined $Parameters{"type"} and length $Parameters{"type"} 0 0 0
Condition0%0/6
exists $Parameters{"type"} and defined $Parameters{"type"} and length $Parameters{"type"}
!ll&&!rl&&r
000
exists $Parameters{"type"} and defined $Parameters{"type"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $Parameters{"type"} and defined $Parameters{"type"} and length $Parameters{"type"}
exists $Parameters{"type"} defined $Parameters{"type"} length $Parameters{"type"}
Truth table0%0/4
A and B and C
A= exists $Parameters{"type"}B= defined $Parameters{"type"}C= length $Parameters{"type"}
ABCresult
0XX0
10X0
1100
1111
232  
233 # return the list of categories.
234 0 ▶ if ($type eq 'c')
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'c') { } 0 0 0
elsif ($type eq 'u') { } 0 0 0
235 {
236 0 return $Self->getCategoriesControlled;
237 }
238 elsif ($type eq 'u')
239 {
240 0 return $Self->getCategoriesUncontrolled;
241 }
242 else
243 {
244 0 return $Self->getCategoriesAll;
245 }
246 }
247  
248 # get the categories assigned to the document by a human editor.
249 sub getCategoriesUncontrolled
250 0 ▶{
Subroutine getCategoriesUncontrolled: not called CC=4 SCAR=30.0
Pod undocumented
251 0 my $Self = shift;
252  
253 # return the categories if previously computed.
254 0 ▶ return $Self->{categories_uncontrolled} if exists $Self->{categories_uncontrolled};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"categories_uncontrolled"} 0 0 0
255  
256 # below is a list of the xpath expressions to get all the hand assigned categories.
257 0 my @xpathExpressions =
258 (
259 '/nitf/head/docdata/identified-content/classifier[@class="indexing_service" and @type="biographical_categories"]', # 2.2.3 biographic categories
260 '/nitf/head/docdata/identified-content/classifier[@class="indexing_service" and @type="descriptor"]', # 2.2.15 descriptors
261 '/nitf/head/docdata/identified-content/location[@class="indexing_service"]', # 2.2.22 locations
262 '/nitf/head/docdata/identified-content/classifier[@class="indexing_service" and @type="names"]', # 2.2.23 names
263 '/nitf/head/docdata/identified-content/org[@class="indexing_service"]', # 2.2.34 organizations
264 '/nitf/head/docdata/identified-content/person[@class="indexing_service"]', # 2.2.36 people
265 '/nitf/head/docdata/identified-content/object.title[@class="indexing_service"]', # 2.2.45 titles
266 );
267  
268 # get the categories.
269 0 my @categories;
270 0 foreach my $xpathExpression (@xpathExpressions)
271 {
272 0 my @nodeset = $Self->{xpathEngine}->findnodes($xpathExpression);
273 0 foreach my $node (@nodeset)
274 {
275 0 push @categories, $node->textContent();
276 }
277 }
278  
279 # remove duplicative, then cache and return the categories.
280 0 my %uniqueCategories = map {(lc $_, $_)} sort @categories;
281 0 @categories = sort values %uniqueCategories;
282 0 $Self->{categories_uncontrolled} = \@categories;
283 0 return $Self->{categories_uncontrolled};
284 }
285  
286 # get the categories assigned to the document by a computer and verified by a human editor.
287 sub getCategoriesControlled
288 0 ▶{
Subroutine getCategoriesControlled: not called CC=4 SCAR=30.0
Pod undocumented
289 0 my $Self = shift;
290  
291 # return the categories if previously computed.
292 0 ▶ return $Self->{categories_controlled} if exists $Self->{categories_controlled};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"categories_controlled"} 0 0 0
293  
294 # below is a list of the xpath expressions to get all the hand assigned categories.
295 0 my @xpathExpressions =
296 (
297 '/nitf/head/docdata/identified-content/classifier[@class="online_producer" and @type="general_descriptor"]', # 2.2.17 general online descriptors
298 '/nitf/head/docdata/identified-content/classifier[@class="online_producer" and @type="descriptor"]', # 2.2.26 online descriptors
299 '/nitf/head/docdata/identified-content/location[@class="online_producer"]', # 2.2.29 online locations
300 '/nitf/head/docdata/identified-content/org[@class="online_producer"]', # 2.2.30 online organizations
301 '/nitf/head/docdata/identified-content/person[@class="online_producer"]', # 2.2.31 online people
302 '/nitf/head/docdata/identified-content/object.title[@class="online_producer"]', # 2.2.33 online titles
303 );
304  
305 # get the categories.
306 0 my @categories;
307 0 foreach my $xpathExpression (@xpathExpressions)
308 {
309 0 my @nodeset = $Self->{xpathEngine}->findnodes($xpathExpression);
310 0 foreach my $node (@nodeset)
311 {
312 0 push @categories, $node->textContent();
313 }
314 }
315  
316 # remove duplicative, then cache and return the categories.
317 0 my %uniqueCategories = map {(lc $_, $_)} sort @categories;
318 0 @categories = sort values %uniqueCategories;
319 0 $Self->{categories_controlled} = \@categories;
320 0 return $Self->{categories_controlled};
321 }
322  
323 sub getCategoriesAll
324 0 ▶{
Subroutine getCategoriesAll: not called CC=2 SCAR=17.9
Pod undocumented
325 0 my $Self = shift;
326  
327 # return the categories if previously computed.
328 0 ▶ return $Self->{categories_all} if exists $Self->{categories_all};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"categories_all"} 0 0 0
329  
330 # get the controlled and uncontrolled categories.
331 0 my $categoriesControlled = $Self->getCategoriesControlled;
332 0 my $categoriesUncontrolled = $Self->getCategoriesUncontrolled;
333  
334 # remove duplicative, then cache and return the merged list of categories.
335 0 my %uniqueCategories = (map {(lc $_, $_)} sort (@{$categoriesControlled}, @{$categoriesUncontrolled}));
336 0 my @categoriesAll = sort values %uniqueCategories;
337 0 $Self->{categories_all} = \@categoriesAll;
338 0 return $Self->{categories_all};
339 }
340  
341 # The method getBaseDirAndFileName returns the pair (base-directory, base-file-name) of the
342 # document. For example, if the document has path nyt_corpus/data/1999/11/26/1156340.xml,
343 # then the pair returned is ('1999/11/26', '1156340').
344 sub getBaseDirAndFileName
345 0 ▶{
Subroutine getBaseDirAndFileName: not called CC=2 SCAR=17.9
Pod undocumented
346 0 my $Self = shift;
347 0 ▶ return ($1, $2) if ($Self->{filePath} =~ m|(\d\d\d\d.\d\d.\d\d).(\d{7})\.xml$|);
Branch: 1 branch
BranchTrueFalseTotal
if $Self->{"filePath"} =~ /(\d\d\d\d.\d\d.\d\d).(\d{7})\.xml$/ 0 0 0
348 0 return undef;
349 }
350  
351 =head2 C<getContent>
352  
353 getContent ()
354  
355 The method C<getContent> returns the content of the document as an array reference of the text
356 where each item in the array is a sentence, with the first sentence being the headline or title
357 of the article. If the lead sentence equals the headline of the article, then the headline is
358 not prefixed to the list.
359  
360 =cut
361  
362 # get the content of the document removing the repeating lead paragraph if it exists.
363 sub getContent
364 0 ▶{
Subroutine getContent: not called CC=2 SCAR=17.9
Pod documented
365 0 my $Self = shift;
366  
367 # if already computed return the content.
368 0 ▶ return $Self->{content} if exists $Self->{content};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"content"} 0 0 0
369 0 my $title = $Self->getTitle;
370 0 my $body = $Self->getBody;
371  
372 # return the title and lines of body text.
373 0 $Self->{content} = [ @$title, @$body ];
374 0 return $Self->{content};
375 }
376  
377 =head2 C<getDate>
378  
379 getDate (format => '%g')
380  
381 C<getDate> returns the date and time of the article in the format speficied by C<format> that uses the print
382 directives of L<Date::Manip::Date|Date::Manip::Date/PRINTF_DIRECTIVES>.
383 The default is to return the date and time in RFC2822 format.
384  
385 =cut
386  
387 # return the date of the article
388 sub getDate
389 0 ▶{
Subroutine getDate: not called CC=10 SCAR=47.0
Pod documented
390 0 my ($Self, %Parameters) = @_;
391  
392 # get the date/time format.
393 0 my $dateFormat = '%g';
394 0 ▶ $dateFormat = $Parameters{format} if (exists ($Parameters{format}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"format"} 0 0 0
395  
396 # if date already exists and is defined, return it now.
397 0 ▶ return UnixDate ($Self->{date}, $dateFormat) if (exists ($Self->{date}) && defined ($Self->{date}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"date"} and defined $Self->{"date"} 0 0 0
Condition0%0/3
exists $Self->{"date"} and defined $Self->{"date"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $Self->{"date"} and defined $Self->{"date"}
exists $Self->{"date"} defined $Self->{"date"}
Truth table0%0/3
A and B
A= exists $Self->{"date"}B= defined $Self->{"date"}
ABresult
0X0
100
111
398  
399 # if date exists but could not be parsed, returned undefined.
400 0 ▶ return undef if (exists ($Self->{date}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"date"} 0 0 0
401  
402 # get the article date.
403 0 my @dates;
404 0 my @nodes = $Self->{xpathEngine}->findnodes('/nitf/head/pubdata/@date.publication');
405 0 foreach my $node (@nodes)
406 {
407 0 push @dates, $node->textContent();
408 }
409  
410 0 my $date;
411 0 foreach my $foundDate (@dates)
412 {
413 0 my $pubDate = ParseDate ($foundDate);
414 0 ▶ if (defined ($pubDate) && length ($pubDate))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pubDate and length $pubDate) 0 0 0
Condition0%0/3
defined $pubDate and length $pubDate
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pubDate and length $pubDate
defined $pubDate length $pubDate
Truth table0%0/3
A and B
A= defined $pubDateB= length $pubDate
ABresult
0X0
100
111
415 {
416 0 $date = $pubDate;
417 0 last;
418 }
419 }
420  
421 # return the date.
422 0 ▶ return undef unless defined $date;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $date 0 0 0
423 0 $Self->{date} = $date;
424 0 return UnixDate ($Self->{date}, $dateFormat);
425 }
426  
427 =head2 C<getDescription>
428  
429 getDescription ()
430  
431 C<getDescription> returns an array reference of strings of sentences that
432 describe the articles content.
433  
434 =cut
435  
436 sub getDescription
437 0 ▶{
Subroutine getDescription: not called CC=3 SCAR=24.8
Pod documented
438 0 my $Self = shift;
439 0 ▶ return $Self->{description} if exists $Self->{description};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"description"} 0 0 0
440  
441 # get the article description.
442 0 my @linesOfText;
443 0 my @nodes = $Self->{xpathEngine}->findnodes('/nitf/body/body.head/abstract');
444 0 foreach my $node (@nodes)
445 {
446 0 push @linesOfText, $node->textContent();
447 }
448  
449 0 $Self->{description} = \@linesOfText;
450 0 return $Self->{description};
451 }
452  
453 =head2 C<getTitle>
454  
455 getTitle ()
456  
457 C<getTitle> returns an array reference of strings, usually one, of the title of the article.
458  
459 =cut
460  
461 sub getTitle
462 0 ▶{
Subroutine getTitle: not called CC=3 SCAR=24.8
Pod documented
463 0 my $Self = shift;
464  
465 # if already computed return the title.
466 0 ▶ return $Self->{title} if exists $Self->{title};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"title"} 0 0 0
467  
468 # get the print headline.
469 0 my @titleLines;
470 0 my @nodes = $Self->{xpathEngine}->findnodes('/nitf/body[1]/body.head/hedline/hl1');
471 0 foreach my $node (@nodes)
472 {
473 0 push @titleLines, $node->textContent();
474 }
475 0 $Self->{title} = \@titleLines;
476 0 return $Self->{title};
477 }
478  
479 =head2 C<getUri>
480  
481 getUri (type => 'file')
482  
483 C<getUri> returns the URI of the document where C<type> must be C<'file'> or C<'url'>.
484 If C<type> is C<'file'>, the file path of the document is returned; otherwise the URL
485 of the document is returned. The default is C<'file'>.
486  
487 =cut
488  
489 sub getUri
490 0 ▶{
Subroutine getUri: not called CC=5 SCAR=34.0
Pod documented
491 0 my ($Self, %Parameters) = shift;
492  
493 # set the category type.
494 0 my $type = 'f';
495 0 ▶ $type = lc substr ($Parameters{type}, 0, 1) if (exists ($Parameters{type}) && defined ($Parameters{type}) && length ($Parameters{type}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $Parameters{"type"} and defined $Parameters{"type"} and length $Parameters{"type"} 0 0 0
Condition0%0/6
exists $Parameters{"type"} and defined $Parameters{"type"} and length $Parameters{"type"}
!ll&&!rl&&r
000
exists $Parameters{"type"} and defined $Parameters{"type"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $Parameters{"type"} and defined $Parameters{"type"} and length $Parameters{"type"}
exists $Parameters{"type"} defined $Parameters{"type"} length $Parameters{"type"}
Truth table0%0/4
A and B and C
A= exists $Parameters{"type"}B= defined $Parameters{"type"}C= length $Parameters{"type"}
ABCresult
0XX0
10X0
1100
1111
496 0 ▶ return $Self->{filePath} if ($type eq 'f');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "f" 0 0 0
497 0 return $Self->getUrl;
498 }
499  
500 sub getUrl
501 0 ▶{
Subroutine getUrl: not called CC=5 SCAR=34.0
Pod undocumented
502 0 my $Self = shift;
503  
504 # return the url if previously computed.
505 0 ▶ return $Self->{url} if exists $Self->{url};
Branch: 1 branch
BranchTrueFalseTotal
if exists $Self->{"url"} 0 0 0
506  
507 # get the possible urls.
508 0 my @urls;
509 0 my @nodeset = $Self->{xpathEngine}->findnodes('/nitf/head/meta[@name="alternate_url"]/@content');
510 0 foreach my $node (@nodeset)
511 {
512 0 push @urls, $node->textContent();
513 }
514  
515 0 ▶ if (@urls < 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@urls < 1) 0 0 0
516 {
517 0 @nodeset = $Self->{xpathEngine}->findnodes('/nitf/head/pubdata/@ex-ref');
518 0 foreach my $node (@nodeset)
519 {
520 0 push @urls, $node->textContent();
521 }
522 }
523  
524 # cache and return the url.
525 0 $Self->{url} = $urls[0];
526 0 return $Self->{url};
527 }
528  
529 sub DESTROY
530 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
531 0 my $Self = shift;
532 0 undef $Self;
533 }
534  
535 =head1 INSTALLATION
536  
537 For installation instructions see L<Text::Corpus::NewYorkTimes>.
538  
539 =head1 AUTHOR
540  
541 Jeff Kubina<jeff.kubina@gmail.com>
542  
543 =head1 COPYRIGHT
544  
545 Copyright (c) 2009 Jeff Kubina. All rights reserved.
546 This program is free software; you can redistribute
547 it and/or modify it under the same terms as Perl itself.
548  
549 The full text of the license can be found in the
550 LICENSE file included with this module.
551  
552 =head1 KEYWORDS
553  
554 nyt, new york times, english corpus, information processing
555  
556 =head1 SEE ALSO
557  
558 =begin html
559  
560 This module requires the <a href="http://www.ldc.upenn.edu/Catalog/CatalogEntry.jsp?catalogId=LDC2008T19">The New York Times Annotated Corpus</a>
561 from the Linguistic Data Consortium; discussions about the corpus are moderated at the
562 Google Group called <a href="http://groups.google.com/group/nytnlp">The New York Times Annotated Corpus Community</a>.
563  
564 =end html
565  
566 L<Log::Log4perl>, L<Text::Corpus::NewYorkTimes>, L<XML::LibXML>, L<XML::LibXML::XPathContext>
567  
568 =cut
569  
570 1 1;
571 # The preceding line will help the module return a true value
↑ Summary NewYorkTimes.pm »