Parser/Standard.pm

filter: Statementstmt 53.9% 164 / 304 Branchbran 31.0% 64 / 206 Conditioncond 24.6% 19 / 77 MC/DCmcdc 4.7% 2 / 42 Subroutinesub 81.8% 9 / 11 Podpod 100% 2 / 2 totaltotal 40.5% 260 / 642 CC 142 SCAR 82.0
CC
142
Cov
42%
parse
4430.4
_decolumnise
13.2
_addpagebreaks
9.1
CRAP 3645.0
↑ Summary Parser/Brody.pm »
1 package Biblio::Document::Parser::Standard;
2  
3 ######################################################################
4 #
5 # Biblio::Document::Parser::Standard;
6 #
7 ######################################################################
8 #
9 # This file is part of ParaCite Tools (http://paracite.eprints.org/developers/)
10 #
11 # Copyright (c) 2002 University of Southampton, UK. SO17 1BJ.
12 #
13 # ParaTools is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 2 of the License, or
16 # (at your option) any later version.
17 #
18 # ParaTools is distributed in the hope that it will be useful,
19 # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 # GNU General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with ParaTools; if not, write to the Free Software
25 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #
27 ######################################################################
28  
29 1 require Exporter;
30 1 @ISA = ("Exporter", "Biblio::Document::Parser");
31  
32 1 ▶use Biblio::Document::Parser::Utils qw( normalise_multichars );
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use vars qw($DEBUG);
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 1 our @EXPORT_OK = ( 'parse', 'new' );
40  
41 1 $DEBUG = 0;
42  
43 =pod
44  
45 =head1 NAME
46  
47 B<Biblio::Document::Parser::Standard> - document parsing functionality
48  
49 =head1 SYNOPSIS
50  
51 use Biblio::Document::Parser::Standard;
52 use Biblio::Document::Parser::Utils;
53 # First read a file into an array of lines.
54 my $content = Biblio::Document::Parser::Utils::get_content("http://www.foo.com/myfile.pdf");
55 my $doc_parser = new Biblio::Document::Parser::Standard();
56 my @references = $doc_parser->parse($content);
57 # Print a list of the extracted references.
58 foreach(@references) { print "-> $_\n"; }
59  
60 =head1 DESCRIPTION
61  
62 Biblio::Document::Parser::Standard provides a fairly simple implementation of
63 a system to extract references from documents.
64  
65 Various styles of reference are supported, including numeric and indented,
66 and documents with two columns are converted into single-column documents
67 prior to parsing. This is a very experimental module, and still contains
68 a few hard-coded constants that can probably be improved upon.
69  
70 =head1 METHODS
71  
72 =over 4
73  
74 =item $parser = Biblio::Document::Parser::Standard-E<gt>new()
75  
76 The new() method creates a new parser instance.
77  
78 =cut
79  
80 sub new
81 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
82 1 my($class) = @_;
83 1 my $self = {};
84 1 return bless($self, $class);
85 }
86  
87 =pod
88  
89 =item @references = $parser-E<gt>parse($lines, [%options])
90  
91 The parse() method takes a string as input (see the get_content()
92 function in Biblio::Document::Parser::Utils for a way to obtain this), and returns a list
93 of references in plain text suitable for passing to a CiteParser module.
94  
95 =cut
96  
97 sub parse
98 1 ▶{
Subroutine parse: called CC=125 SCAR=84.0
Pod documented
99 1 my($self, $lines, %options) = @_;
100 1 $lines = _addpagebreaks($lines);
101 1 my @lines = split("\n", $lines);
102 1 my($pivot, $avelen) = $self->_decolumnise(@lines);
103
104 1 my $in_refs = 0;
105 1 my @ref_table = ();
106 1 my $curr_ref = "";
107 1 my @newlines = ();
108 1 my $outcount = 0;
109 1 my @chopped_lines = @lines;
110 # First isolate the reference array. This ensures that we handle columns correctly.
111 1 foreach(@lines)
112 {
113 9 $outcount++;
114 9 chomp;
115 9 ▶ if (/(?:references)|(?:bibliography)|(?:\s+cited)/i)
Branch: 2 branches
BranchTrueFalseTotal
if (/(?:references)|(?:bibliography)|(?:\s+cited)/i) { } 1 8 9
elsif (/\f/) { } 0 8 8
116 {
117 1 last;
118 }
119 elsif (/\f/)
120 {
121 # No sign of any references yet, so pop off up to here
122 0 ▶ for(my $i=0; $i<$outcount; $i++) { shift @chopped_lines; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $outcount) 0 0 0
123 0 $outcount = 0;
124 }
125 }
126 1 my @arr1 = ();
127 1 my @arr2 = ();
128 1 my @arrout = ();
129 1 my $indnt = "";
130 1 ▶ if ($pivot)
Branch: 1 branch
BranchTrueFalseTotal
if ($pivot) 1 0 1
131 {
132 1 my ($pivotl,$pivotr) = ($pivot-5,$pivot+5);
133 1 foreach(@chopped_lines)
134 {
135 15 chomp;
136 15 ▶ if (/\f/)
Branch: 1 branch
BranchTrueFalseTotal
if (/\f/) { } 0 15 15
137 {
138 0 push @arrout, @arr1;
139 0 push @arrout, @arr2;
140 0 @arr1 = ();
141 0 @arr2 = ();
142 }
143 else
144 {
145 15 ▶ if(/^(.{$pivotl,$pivotr})\s{3}(\s{3,})?(\S.+?)$/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^(.{$pivotl,$pivotr})\s{3}(\s{3,})?(\S.+?)$/) { } 0 15 15
146 {
147 # push @arr1, $indnt.$1;
148 0 ▶ push @arr1, $1 if defined($1);
Branch: 1 branch
BranchTrueFalseTotal
if defined $1 0 0 0
149 0 ▶ push @arr2, ($2||'').$3 if defined($3);
Branch: 1 branch
BranchTrueFalseTotal
if defined $3 0 0 0
Condition0%0/2
$2 || ''
l!l
00
MC/DC0%0/1
$2 || ''
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
150 }
151 else
152 {
153 15 push @arr1, $indnt.$_;
154 }
155 }
156 }
157 1 push @arrout, @arr1;
158 1 push @arrout, @arr2;
159 1 @chopped_lines = @arrout;
160 }
161 1 my $prevnew = 0;
162 1 foreach(@chopped_lines)
163 {
164 15 chomp;
165 15 ▶ if (/^\s*references\s*$/i || /REFERENCES/ || /Bibliography/i || /References and Notes/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*references\s*$/i or /REFERENCES/ or /Bibliography/i or /References and Notes/) 1 14 15
Condition66%6/9
/^\s*references\s*$/i or /REFERENCES/ or /Bibliography/i or /References and Notes/
l!l&&r!l&&!r
1014
/^\s*references\s*$/i or /REFERENCES/ or /Bibliography/i
l!l&&r!l&&!r
1014
/^\s*references\s*$/i or /REFERENCES/
l!l&&r!l&&!r
1014
MC/DC25%1/4
/^\s*references\s*$/i or /REFERENCES/ or /Bibliography/i or /References and Notes/
/^\s*references\s*$/i /REFERENCES/ /Bibliography/i /References and Notes/
Truth table40%2/5
A or B or C or D
A= /^\s*references\s*$/iB= /REFERENCES/C= /Bibliography/iD= /References and Notes/
ABCDresult
1XXX1
01XX1
001X1
00011
00000
166 {
167 1 $in_refs = 1;
168 1 ▶ push @newlines, $' if defined($'); # Capture bad input
Branch: 1 branch
BranchTrueFalseTotal
if defined $' 1 0 1
169 1 next;
170 }
171 14 ▶ if (/^\s*\bappendix\b/i || /_{6}.{0,10}$/ || /^\s*\btable\b/i || /wish to thank/i || /\bfigure\s+\d/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\bappendix\b/i or /_{6}.{0,10}$/ or /^\s*\btable\b/i or /wish to thank/i or /\bfigure\s+\d/) 0 14 14
Condition33%4/12
/^\s*\bappendix\b/i or /_{6}.{0,10}$/ or /^\s*\btable\b/i or /wish to thank/i or /\bfigure\s+\d/
l!l&&r!l&&!r
0014
/^\s*\bappendix\b/i or /_{6}.{0,10}$/ or /^\s*\btable\b/i or /wish to thank/i
l!l&&r!l&&!r
0014
/^\s*\bappendix\b/i or /_{6}.{0,10}$/ or /^\s*\btable\b/i
l!l&&r!l&&!r
0014
/^\s*\bappendix\b/i or /_{6}.{0,10}$/
l!l&&r!l&&!r
0014
MC/DC0%0/5
/^\s*\bappendix\b/i or /_{6}.{0,10}$/ or /^\s*\btable\b/i or /wish to thank/i or /\bfigure\s+\d/
/^\s*\bappendix\b/i /_{6}.{0,10}$/ /^\s*\btable\b/i /wish to thank/i /\bfigure\s+\d/
Truth table16%1/6
A or B or C or D or E
A= /^\s*\bappendix\b/iB= /_{6}.{0,10}$/C= /^\s*\btable\b/iD= /wish to thank/iE= /\bfigure\s+\d/
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
172 {
173 0 $in_refs = 0;
174 }
175  
176 14 ▶ if (/^\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*$/) { } 6 8 14
177 {
178 6 ▶ if ($prevnew) { next; }
Branch: 1 branch
BranchTrueFalseTotal
if ($prevnew) 2 4 6
179 4 $prevnew = 1;
180 }
181 else
182 {
183 8 $prevnew = 0;
184 }
185  
186 0 ▶ if (/^\s*\d+\s*$/) { next; } # Page number
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\d+\s*$/) 0 12 12
187  
188 12 ▶ if ($in_refs)
Branch: 1 branch
BranchTrueFalseTotal
if ($in_refs) 5 7 12
189 {
190 5 ▶ my $spaces = /^(\s+)/ ? length($1) : 0;
Branch: 1 branch
BranchTrueFalseTotal
/^(\s+)/ ? : 0 5 5
191 5 ▶ if( @newlines && /^(\s+)[a-z]/ && _within(length($1),length($newlines[$#newlines]),5) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@newlines and /^(\s+)[a-z]/ and _within(length $1, length $newlines[$#newlines], 5)) { } 0 5 5
Condition33%2/6
@newlines and /^(\s+)[a-z]/ and _within(length $1, length $newlines[$#newlines], 5)
!ll&&!rl&&r
500
@newlines and /^(\s+)[a-z]/
!ll&&!rl&&r
050
MC/DC0%0/3
@newlines and /^(\s+)[a-z]/ and _within(length $1, length $newlines[$#newlines], 5)
@newlines /^(\s+)[a-z]/ _within(length $1, length $newlines[$#newlines], 5)
Truth table25%1/4
A and B and C
A= @newlinesB= /^(\s+)[a-z]/C= _within(length $1, length $newlines[$#newlines], 5)
ABCresult
0XX0
10X0
1100
1111
192 0 s/^\s+//s;
193 0 $newlines[$#newlines] .= $_;
194 } else {
195 5 push @newlines, $_;
196 }
197 }
198 }
199 # We failed to find the reference section, we'll do a last-ditch effect at finding numbered
200 # refs
201 1 ▶ unless($in_refs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($in_refs) 0 1 1
202 0 my $first = 0;
203 0 my $lastnum = 0;
204 0 my $numwith = 0;
205 0 my $numwo = 0;
206 0 ▶ for(my $i = 0; $i < @chopped_lines; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @chopped_lines) 0 0 0
207 0 $_ = $chopped_lines[$i];
208 0 ▶ if( /^\s*[\[\(](\d+)[\]\)]/ || /^\s*(\d+)(?:\.|\s{5,})/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^\s*[\[\(](\d+)[\]\)]/ or /^\s*(\d+)(?:\.|\s{5,})/) { } 0 0 0
elsif ($numwo++ == 5) { } 0 0 0
elsif ($numwith == 5) { } 0 0 0
Condition0%0/3
/^\s*[\[\(](\d+)[\]\)]/ or /^\s*(\d+)(?:\.|\s{5,})/
l!l&&r!l&&!r
000
MC/DC0%0/2
/^\s*[\[\(](\d+)[\]\)]/ or /^\s*(\d+)(?:\.|\s{5,})/
/^\s*[\[\(](\d+)[\]\)]/ /^\s*(\d+)(?:\.|\s{5,})/
Truth table0%0/3
A or B
A= /^\s*[\[\(](\d+)[\]\)]/B= /^\s*(\d+)(?:\.|\s{5,})/
ABresult
1X1
011
000
209 0 ▶ $first = $1 if $1 == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 == 1 0 0 0
210 0 ▶ if( $lastnum && $1 == $lastnum+1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastnum and $1 == $lastnum + 1) { } 0 0 0
Condition0%0/3
$lastnum and $1 == $lastnum + 1
!ll&&!rl&&r
000
MC/DC0%0/2
$lastnum and $1 == $lastnum + 1
$lastnum $1 == $lastnum + 1
Truth table0%0/3
A and B
A= $lastnumB= $1 == $lastnum + 1
ABresult
0X0
100
111
211 0 $numwo = 0;
212 0 $numwith++;
213 0 $lastnum++;
214 } else {
215 0 $first = $i;
216 0 $lastnum = $1;
217 }
218 } elsif( $numwo++ == 5 ) { # Reset
219 0 $first = $lastnum = $numwith = $numwo = 0;
220 } elsif( $numwith == 5 ) {
221 0 last;
222 }
223 }
224 0 ▶ @newlines = splice(@chopped_lines,$first) if $first && $numwith == 5;
Branch: 1 branch
BranchTrueFalseTotal
if $first and $numwith == 5 0 0 0
Condition0%0/3
$first and $numwith == 5
!ll&&!rl&&r
000
MC/DC0%0/2
$first and $numwith == 5
$first $numwith == 5
Truth table0%0/3
A and B
A= $firstB= $numwith == 5
ABresult
0X0
100
111
225 }
226 #warn "BEGIN REF SECTION\n", join("\n",@newlines), "\nEND REF SECTION\n";
227 # Work out what sort of separation is used
228 1 my $type = 0;
229 1 my $TYPE_NEWLINE = 0;
230 1 my $TYPE_INDENT = 1; # First line indented
231 1 my $TYPE_NUMBER = 2;
232 1 my $TYPE_NUMBERSQ = 3;
233 1 my $TYPE_LETTERSQ = 4;
234 1 my $TYPE_INDENT_OTHER = 5; # Other lines indented
235 1 my $numnew = 0;
236 1 my $numind = 0;
237 1 my $numnum = 0;
238 1 my $numsq = 0;
239 1 my $lettsq = 0;
240 1 my $indmin = 255;
241 1 my $indmax = 0;
242 1 my @indented;
243 # Handle numbered references joined together (e.g. bad to-text conversion)
244 1 my $ref_sect = join "\n", @newlines;
245 1 my $ref_b = 1; my $ref_e = 2;
246 1 my @num_refs;
247 1 ▶ while( $ref_sect =~ s/(\[$ref_b\].+?)(?=\[$ref_e\])//sg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_sect =~ s/(\[$ref_b\].+?)(?=\[$ref_e\])//gs) 0 1 1
248 0 $ref_b++; $ref_e++;
249 0 push @num_refs, split("\n", $1);
250 }
251 1 ▶ if( $ref_b >= 5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref_b >= 5) 0 1 1
252 0 @newlines = @num_refs;
253 0 ▶ push @newlines, $ref_sect if defined($ref_sect);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref_sect 0 0 0
254 }
255 # Resume normal processing
256 1 foreach(@newlines)
257 {
258 6 $_ = normalise_multichars($_);
259 6 ▶ if (/^\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*$/) 3 3 6
260 {
261 3 $numnew++;
262 }
263 6 ▶ if (/^(\s+)\b/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\s+)\b/) 0 6 6
264 {
265 0 ▶ if (length $1 < $indmin) { $indmin = length $1; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $1 < $indmin) 0 0 0
266 0 ▶ if (length $1 > $indmax) { $indmax = length $1; }
Branch: 1 branch
BranchTrueFalseTotal
if (length $1 > $indmax) 0 0 0
267 0 ▶ if( length($1) >= $indmax && /^\s+[A-Z]/ ) { $numind++ }
Branch: 1 branch
BranchTrueFalseTotal
if (length $1 >= $indmax and /^\s+[A-Z]/) 0 0 0
Condition0%0/3
length $1 >= $indmax and /^\s+[A-Z]/
!ll&&!rl&&r
000
MC/DC0%0/2
length $1 >= $indmax and /^\s+[A-Z]/
length $1 >= $indmax /^\s+[A-Z]/
Truth table0%0/3
A and B
A= length $1 >= $indmaxB= /^\s+[A-Z]/
ABresult
0X0
100
111
268 }
269 6 ▶ if (/^\s*\d+\.?\s+[[:alnum:]]/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\d+\.?\s+[[:alnum:]]/) 0 6 6
270 {
271 0 $numnum++;
272 }
273 6 ▶ if (/^\s*[\[\(]\d+[\]\)]\s+[[:alnum:]]/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*[\[\(]\d+[\]\)]\s+[[:alnum:]]/) 0 6 6
274 {
275 0 $numsq++;
276 }
277 6 ▶ if (/^\s*[\[\(][A-Za-z]\w*[\]\)]\s/)
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*[\[\(][A-Za-z]\w*[\]\)]\s/) 0 6 6
278 {
279 0 $lettsq++;
280 }
281 }
282
283 # if ($numnew < ($#newlines-5) && ($indmax > $indmin) && $indmax != 0 && $indmin != 255 && $indmax < 24) { $type = $TYPE_INDENT; }
284 # If references are seperated by blank lines, then we would expect to see around one blank line
285 # for each reference?
286 #warn "indmin=$indmin, indmax=$indmax\n";
287 0 ▶ if ($numnew < ($#newlines/2) && ($indmax >= $indmin) && $indmax != 0 && $indmin != 255 && $indmax < 24) { $type = $numind >= $#newlines/2 ? $TYPE_INDENT : $TYPE_INDENT_OTHER; }
Branch: 2 branches
BranchTrueFalseTotal
if ($numnew < $#newlines / 2 and $indmax >= $indmin and $indmax != 0 and $indmin != 255 and $indmax < 24) 0 1 1
$numind >= $#newlines / 2 ? : 0 0 0
Condition33%4/12
$numnew < $#newlines / 2 and $indmax >= $indmin and $indmax != 0 and $indmin != 255 and $indmax < 24
!ll&&!rl&&r
100
$numnew < $#newlines / 2 and $indmax >= $indmin and $indmax != 0 and $indmin != 255
!ll&&!rl&&r
100
$numnew < $#newlines / 2 and $indmax >= $indmin and $indmax != 0
!ll&&!rl&&r
100
$numnew < $#newlines / 2 and $indmax >= $indmin
!ll&&!rl&&r
100
MC/DC0%0/5
$numnew < $#newlines / 2 and $indmax >= $indmin and $indmax != 0 and $indmin != 255 and $indmax < 24
$numnew < $#newlines / 2 $indmax >= $indmin $indmax != 0 $indmin != 255 $indmax < 24
Truth table16%1/6
A and B and C and D and E
A= $numnew < $#newlines / 2B= $indmax >= $indminC= $indmax != 0D= $indmin != 255E= $indmax < 24
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
288 0 ▶ if ($numnum > 3) { $type = $TYPE_NUMBER; }
Branch: 1 branch
BranchTrueFalseTotal
if ($numnum > 3) 0 1 1
289 0 ▶ if ($numsq > 3) { $type = $TYPE_NUMBERSQ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($numsq > 3) 0 1 1
290 0 ▶ if ($lettsq > 3) { $type = $TYPE_LETTERSQ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($lettsq > 3) 0 1 1
291 1 ▶ if ($type == $TYPE_NEWLINE)
Branch: 6 branches
BranchTrueFalseTotal
if ($type == $TYPE_NEWLINE) { } 1 0 1
elsif ($type == $TYPE_INDENT) { } 0 0 0
elsif ($type == $TYPE_INDENT_OTHER) { } 0 0 0
elsif ($type == $TYPE_NUMBER) { } 0 0 0
elsif ($type == $TYPE_NUMBERSQ) { } 0 0 0
elsif ($type eq $TYPE_LETTERSQ) { } 0 0 0
292 {
293 1 ▶warn "type = NEWLINE" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Biblio::Document::Parser::Standard::DEBUG 0 1 1
294 1 ▶ my $indmin = $indmin>5 ? $indmin + 3 : 5;
Branch: 1 branch
BranchTrueFalseTotal
$indmin > 5 ? : 1 0 1
295 1 foreach(@newlines)
296 {
297 6 ▶ if (/^\s*$/)
Branch: 2 branches
BranchTrueFalseTotal
if (/^\s*$/) { } 3 3 6
elsif (/^\s{$indmin}/) { } 0 3 3
298 {
299 3 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 2 1 3
300 3 $curr_ref = "";
301 3 next;
302 }
303 # Indented line amongst justified text, attach to the previous reference
304 elsif( /^\s{$indmin}/ ) {
305 0 s/^\s*(.+)\s*$/$1/;
306 0 ▶ if( !$curr_ref && @ref_table ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $curr_ref and @ref_table) 0 0 0
Condition0%0/3
not $curr_ref and @ref_table
!ll&&!rl&&r
000
MC/DC0%0/2
not $curr_ref and @ref_table
not $curr_ref @ref_table
Truth table0%0/3
not(A) and B
A= not $curr_refB= @ref_table
ABresult
0X0
100
111
307 0 $ref_table[$#ref_table] .= " ".$_;
308 0 next;
309 }
310 }
311 # Trim off any whitespace surrounding chunk
312 3 s/^\s*(.+)\s*$/$1/;
313 3 s/^(.+)[\\-]+$/$1/;
314 3 ▶ if ($curr_ref =~ /http:\/\/\S+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref =~ m[http://\S+$]) { } 0 3 3
315 0 $curr_ref = $curr_ref.$_;
316 } else {
317 3 $curr_ref .= " ".$_;
318 }
319 }
320 1 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 1 0 1
321 }
322 elsif ($type == $TYPE_INDENT)
323 {
324 0 ▶warn "type = INDENT" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Biblio::Document::Parser::Standard::DEBUG 0 0 0
325 0 foreach(@newlines)
326 {
327 0 ▶ if (/^(\s*)\b/ && length $1 == $indmin)
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\s*)\b/ and length $1 == $indmin) { } 0 0 0
Condition0%0/3
/^(\s*)\b/ and length $1 == $indmin
!ll&&!rl&&r
000
MC/DC0%0/2
/^(\s*)\b/ and length $1 == $indmin
/^(\s*)\b/ length $1 == $indmin
Truth table0%0/3
A and B
A= /^(\s*)\b/B= length $1 == $indmin
ABresult
0X0
100
111
328 {
329 0 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 0 0 0
330 0 $curr_ref = $_;
331 }
332 else
333 {
334 # Trim off any whitespace surrounding chunk
335 0 s/^\s*(.+)\s*$/$1/;
336 0 ▶ if ($curr_ref =~ /http:\/\/\S+$/) { $curr_ref = $curr_ref.$_;} else
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref =~ m[http://\S+$]) { } 0 0 0
337 {
338 0 $curr_ref = $curr_ref." ".$_;
339 }
340  
341 }
342 }
343 0 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 0 0 0
344 }
345 elsif ($type == $TYPE_INDENT_OTHER)
346 {
347 0 ▶warn "type = INDENT_OTHER" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Biblio::Document::Parser::Standard::DEBUG 0 0 0
348 0 foreach(@newlines)
349 {
350 0 ▶ if (!$curr_ref ) { $curr_ref = $_; }
Branch: 2 branches
BranchTrueFalseTotal
if (not $curr_ref) { } 0 0 0
elsif (/^(\s*)\S/ and _within(length $1, $indmax, 2)) { } 0 0 0
351 ▶ elsif (/^(\s*)\S/ && _within(length($1),$indmax,2))
Condition0%0/3
/^(\s*)\S/ and _within(length $1, $indmax, 2)
!ll&&!rl&&r
000
MC/DC0%0/2
/^(\s*)\S/ and _within(length $1, $indmax, 2)
/^(\s*)\S/ _within(length $1, $indmax, 2)
Truth table0%0/3
A and B
A= /^(\s*)\S/B= _within(length $1, $indmax, 2)
ABresult
0X0
100
111
352 {
353 0 s/^\s+//;
354 0 ▶ if( $curr_ref =~ s/(?<=\w)\-\s*$// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref =~ s/(?<=\w)\-\s*$//) { } 0 0 0
355 0 $curr_ref .= $_;
356 } else {
357 0 $curr_ref .= " ".$_;
358 }
359 }
360 else
361 {
362 # Trim off any whitespace surrounding chunk
363 0 ▶ if ($curr_ref =~ /http:\/\/\S+$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref =~ m[http://\S+$]) { } 0 0 0
364 {
365 0 s/^\s*(.+)\s*$/$1/;
366 0 $curr_ref .= $_;
367 }
368 else
369 {
370 0 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 0 0 0
371 0 $curr_ref = $_;
372 }
373 }
374 }
375 0 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 0 0 0
376 }
377 elsif ($type == $TYPE_NUMBER)
378 {
379 0 ▶warn "type = NUMBER" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Biblio::Document::Parser::Standard::DEBUG 0 0 0
380 0 my $lastnum = 0;
381 0 foreach(@newlines)
382 {
383 0 s/^\s*(.+)\s*$/$1/;
384 0 ▶ if (/^(\d+)\.?(([\s_]{8}\s*[,a;])|\s+[[:alnum:]_]).+$/ && $1 == $lastnum+1 )
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+)\.?(([\s_]{8}\s*[,a;])|\s+[[:alnum:]_]).+$/ and $1 == $lastnum + 1) { } 0 0 0
Condition0%0/3
/^(\d+)\.?(([\s_]{8}\s*[,a;])|\s+[[:alnum:]_]).+$/ and $1 == $lastnum + 1
!ll&&!rl&&r
000
MC/DC0%0/2
/^(\d+)\.?(([\s_]{8}\s*[,a;])|\s+[[:alnum:]_]).+$/ and $1 == $lastnum + 1
/^(\d+)\.?(([\s_]{8}\s*[,a;])|\s+[[:alnum:]_]).+$/ $1 == $lastnum + 1
Truth table0%0/3
A and B
A= /^(\d+)\.?(([\s_]{8}\s*[,a;])|\s+[[:alnum:]_]).+$/B= $1 == $lastnum + 1
ABresult
0X0
100
111
385 {
386 0 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 0 0 0
387 0 $curr_ref = $_;
388 0 $lastnum++;
389 0 next;
390 }
391 else
392 {
393 0 ▶ if ($curr_ref =~ /http:\/\/\S+$/) { $curr_ref = $curr_ref.$_;} else
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref =~ m[http://\S+$]) { } 0 0 0
394 {
395 0 $curr_ref = $curr_ref." ".$_;
396 }
397  
398 }
399 }
400 0 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 0 0 0
401 }
402 elsif ($type == $TYPE_NUMBERSQ)
403 {
404 0 ▶warn "type = NUMBERSQ" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Biblio::Document::Parser::Standard::DEBUG 0 0 0
405 0 my $lastnum = 0;
406 0 foreach(@newlines)
407 {
408 0 s/^\s*(.+)\s*$/$1/;
409 # () used in oai:arXiv.org:math-ph/9805026
410 0 ▶ if (/^\s*[\(\[](\d+)[\]\)]\s.+$/s && $1 == $lastnum+1 )
Branch: 3 branches
BranchTrueFalseTotal
if (/^\s*[\(\[](\d+)[\]\)]\s.+$/s and $1 == $lastnum + 1) { } 0 0 0
elsif (/^\s*$/) { } 0 0 0
elsif ($curr_ref) { } 0 0 0
Condition0%0/3
/^\s*[\(\[](\d+)[\]\)]\s.+$/s and $1 == $lastnum + 1
!ll&&!rl&&r
000
MC/DC0%0/2
/^\s*[\(\[](\d+)[\]\)]\s.+$/s and $1 == $lastnum + 1
/^\s*[\(\[](\d+)[\]\)]\s.+$/s $1 == $lastnum + 1
Truth table0%0/3
A and B
A= /^\s*[\(\[](\d+)[\]\)]\s.+$/sB= $1 == $lastnum + 1
ABresult
0X0
100
111
411 {
412 0 ▶ push @ref_table, $curr_ref if $curr_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $curr_ref 0 0 0
413 0 $curr_ref = $_;
414 0 $lastnum++;
415 }
416 elsif( /^\s*$/ ) # Blank line
417 {
418 0 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 0 0 0
419 0 undef $curr_ref;
420 }
421 elsif($curr_ref)
422 {
423 0 ▶ if ($curr_ref =~ /http:\/\/\S+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref =~ m[http://\S+$]) { } 0 0 0
424 0 $curr_ref .= $_;
425 } else {
426 0 $curr_ref .= " ".$_;
427 }
428  
429 }
430 }
431 0 ▶ push @ref_table, $curr_ref if $curr_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $curr_ref 0 0 0
432 }
433 elsif( $type eq $TYPE_LETTERSQ )
434 {
435 0 ▶warn "type = LETTERSQ" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $Biblio::Document::Parser::Standard::DEBUG 0 0 0
436 0 foreach(@newlines)
437 {
438 0 s/^\s*(.+)\s*$/$1/;
439 # () used in oai:arXiv.org:math-ph/9805026
440 0 ▶ if (/^\s*[\(\[](\w+)[\]\)]\s.+$/s )
Branch: 3 branches
BranchTrueFalseTotal
if (/^\s*[\(\[](\w+)[\]\)]\s.+$/s) { } 0 0 0
elsif (/^\s*$/) { } 0 0 0
elsif ($curr_ref) { } 0 0 0
441 {
442 0 ▶ push @ref_table, $curr_ref if $curr_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $curr_ref 0 0 0
443 0 $curr_ref = $_;
444 }
445 elsif( /^\s*$/ ) # Blank line
446 {
447 0 ▶ if ($curr_ref) { push @ref_table, $curr_ref; }
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref) 0 0 0
448 0 undef $curr_ref;
449 }
450 elsif($curr_ref)
451 {
452 0 ▶ if ($curr_ref =~ /http:\/\/\S+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_ref =~ m[http://\S+$]) { } 0 0 0
453 0 $curr_ref .= $_;
454 } else {
455 0 $curr_ref .= " ".$_;
456 }
457  
458 }
459 }
460 0 ▶ push @ref_table, $curr_ref if $curr_ref;
Branch: 1 branch
BranchTrueFalseTotal
if $curr_ref 0 0 0
461 }
462  
463 1 my @refs_out = ();
464 # A little cleaning up before returning
465 1 my $prev_author;
466 1 for (@ref_table)
467 {
468 3 s/([[:alpha:]])\-\s+/$1/g; # End of a line hyphen
469 3 s/^\s*[\[\(]([^\]]+)[\]\)](.+)$/($1) $2/s;
470 # Same author as previous citation
471 3 ▶ $prev_author && s/^((?:[\(\[]\w+[\)\]])|(?:\d{1,3}\.))[\s_]{8,}/$1 $prev_author /;
Branch: 1 branch
BranchTrueFalseTotal
$prev_author and s/^((?:[\(\[]\w+[\)\]])|(?:\d{1,3}\.))[\s_]{8,}/$1 $prev_author / 0 3 3
472 3 ▶ if( /^(?:(?:[\(\[]\w+[\)\]])|(?:\d{1,3}\.))\s*([^,]+?)(?:,|and)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(?:(?:[\(\[]\w+[\)\]])|(?:\d{1,3}\.))\s*([^,]+?)(?:,|and)/) { } 0 3 3
473 0 $prev_author = $1;
474 } else {
475 3 undef $prev_author;
476 }
477 3 s/\s\s+/ /g;
478 3 s/^\s*(.+)\s*$/$1/;
479 # next if length $_ > 200;
480 3 push @refs_out, $_;
481 }
482 1 return @refs_out;
483 }
484  
485 # Private method to determine if/where columns are present.
486  
487 sub _decolumnise
488 1 ▶{
Subroutine _decolumnise: called CC=13 SCAR=25.8
489 1 my($self, @lines) = @_;
490 1 my @bitsout;
491 1 my @lens = (0); # Removes need to check $lens[0] is defined
492 1 foreach(@lines)
493 {
494 # Replaces tabs with 8 spaces
495 15 s/\t/ /g;
496 # Ignore lines that are >75% whitespace (probably diagrams/equations)
497 15 ▶ next if( length($_) == 0 || (($_ =~ tr/ //)/length($_)) > .75 );
Branch: 1 branch
BranchTrueFalseTotal
if length $_ == 0 or $_ =~ tr/ // / length($_) > "0.75" 6 9 15
Condition66%2/3
length $_ == 0 or $_ =~ tr/ // / length($_) > "0.75"
l!l&&r!l&&!r
609
MC/DC50%1/2
length $_ == 0 or $_ =~ tr/ // / length($_) > "0.75"
length $_ == 0 $_ =~ tr/ // / length($_) > "0.75"
Truth table66%2/3
A or B
A= length $_ == 0B= $_ =~ tr/ // / length($_) > "0.75"
ABresult
1X1
011
000
498 # Split into characters
499 9 my @bits = unpack "c*", $_;
500 # Count lines together that vary slightly in length (within 5 chars)
501 9 $lens[int(scalar @bits/5)*5+2]++;
502 9 ▶ my @newbits = map { $_ = ($_==32?1:0) } @bits;
Branch: 1 branch
BranchTrueFalseTotal
$_ == 32 ? : 7 82 89
503 9 ▶ for(my $i=0; $i<$#newbits; $i++) { $bitsout[$i]+=$newbits[$i]; }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#newbits) 80 9 89
504 }
505 # Calculate the average length based on the modal.
506 # 2003-05-14 Fixed by tdb
507 1 my $avelen = 0;
508 1 ▶ for(my $i = 0; $i < @lens; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lens) 23 1 24
509 23 ▶ next unless defined $lens[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lens[$i] 18 5 23
510 5 ▶ $avelen = $i if $lens[$i] > $lens[$avelen];
Branch: 1 branch
BranchTrueFalseTotal
if $lens[$i] > $lens[$avelen] 1 4 5
511 }
512 1 my $maxpoint = 0;
513 1 my $max = 0;
514 # Determine which point has the most spaces
515 1 ▶ for(my $i=0; $i<$#bitsout; $i++) { if ($bitsout[$i] > $max) { $max = $bitsout[$i]; $maxpoint = $i; } }
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $#bitsout) 19 1 20
if ($bitsout[$i] > $max) 2 17 19
516 1 my $center = int($avelen/2);
517 1 my $output = 0;
518 # Only accept if the max point lies around the average center.
519 0 ▶ if ($center-6 <= $maxpoint && $center+6>= $maxpoint) { $output = $maxpoint; } else {$output = 0;}
Branch: 1 branch
BranchTrueFalseTotal
if ($center - 6 <= $maxpoint and $center + 6 >= $maxpoint) { } 1 0 1
Condition33%1/3
$center - 6 <= $maxpoint and $center + 6 >= $maxpoint
!ll&&!rl&&r
001
MC/DC0%0/2
$center - 6 <= $maxpoint and $center + 6 >= $maxpoint
$center - 6 <= $maxpoint $center + 6 >= $maxpoint
Truth table33%1/3
A and B
A= $center - 6 <= $maxpointB= $center + 6 >= $maxpoint
ABresult
0X0
100
111
520 #warn "Decol: avelen=$avelen, center=$center, maxpoint=$maxpoint (output=$output)\n";
521 1 return ($output, $avelen);
522 }
523  
524 # Private function that replaces header/footers with form feeds
525  
526 1 ▶sub _addpagebreaks {
Subroutine _addpagebreaks: called CC=5 SCAR=22.0
527 1 my $doc = shift;
528 1 ▶ return $doc if $doc =~ /\f/s;
Branch: 1 branch
BranchTrueFalseTotal
if $doc =~ /\f/s 0 1 1
529 1 my %HEADERS;
530  
531 1 ▶ while( $doc =~ /(?:\n[\r[:blank:]]*){2}([^\n]{0,40}\w+[^\n]{0,40})(?:\n[\r[:blank:]]*){3}/osg ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc =~ /(?:\n[\r[:blank:]]*){2}([^\n]{0,40}\w+[^\n]{0,40})(?:\n[\r[:blank:]]*){3}/sog) 0 1 1
532 0 $HEADERS{_header_to_regexp($1)}++;
533 }
534  
535 1 ▶ if( %HEADERS ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%HEADERS) { } 0 1 1
536 0 my @regexps = sort { $HEADERS{$b} <=> $HEADERS{$a} } keys %HEADERS;
537 0 my $regexp = $regexps[0];
538 0 ▶ if( $HEADERS{$regexp} > 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HEADERS{$regexp} > 3) { } 0 0 0
539 0 my $c = $doc =~ s/(?:\n[\r[:blank:]]*){2}(?:$regexp)(?:\n[\r[:blank:]]*){3}/\f/sg;
540 # warn "Applying regexp: $regexp ($HEADERS{$regexp} original matches) Removed $c header/footers using ($HEADERS{$regexp} original matches): $regexp\n";
541 } else {
542 0 warn "Not enough matching header/footers were found ($HEADERS{$regexp} only)";
543 }
544 } else {
545 1 warn "Header/footers not found - flying blind if this is a multi-column document";
546 }
547  
548 1 return $doc;
549 }
550  
551 0 ▶sub _header_to_regexp {
Subroutine _header_to_regexp: not called CC=1 SCAR=6.9
552 0 my $header = shift;
553 0 $header =~ s/([\\\|\(\)\[\]\.\*\+\?\{\}])/\\$1/g;
554 0 $header =~ s/\s+/\\s+/g;
555 0 $header =~ s/\d+/\\d+/g;
556 0 return $header;
557 }
558  
559 0 ▶sub _within {
Subroutine _within: not called CC=2 SCAR=17.9
560 0 my ($l,$r,$p) = @_;
561 #warn "Is $l with $p of $r?\n";
562 0 ▶ return $r >= $l-$p && $r <= $l+$p;
Condition0%0/3
$r >= $l - $p && $r <= $l + $p
!ll&&!rl&&r
000
MC/DC0%0/2
$r >= $l - $p && $r <= $l + $p
$r >= $l - $p $r <= $l + $p
Truth table0%0/3
A && B
A= $r >= $l - $pB= $r <= $l + $p
ABresult
0X0
100
111
563 }
564  
565 1 1;
566  
567 __END__
↑ Summary Parser/Brody.pm »