Importer/HOCR.pm

filter: Statementstmt 96.5% 83 / 86 Branchbran 73.3% 22 / 30 Conditioncond 93.1% 27 / 29 MC/DCmcdc 86.6% 13 / 15 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 1 totaltotal 90.8% 158 / 174 CC 26 SCAR 24.4
CC
26
Cov
91%
_next
20.1
_read
3.2
_parse_ocr_attr
3.1
CRAP 11.5
↑ Summary HOCR.pm »
1 package Catmandu::Importer::HOCR;
2 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use XML::LibXML::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Catmandu::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 with "Catmandu::Importer";
9  
10 1 has reader => ( is => "lazy", init_arg => undef );
11  
12 has current_page => (
13 is => "rw",
14 2 ▶ default => sub { undef; }
Subroutine __ANON__: called CC=1 SCAR=0.0
15 1 );
16  
17 2 ▶sub _build_reader {
Subroutine _build_reader: called CC=2 SCAR=7.0
18 2 my $self = $_[0];
19 2 binmode $self->fh;
20 2 ▶ my $reader = XML::LibXML::Reader->new(
Branch: 1 branch
BranchTrueFalseTotal
my $reader = "XML::LibXML::Reader"->new("IO", $self->fh, "validation", 0, "load_ext_dtd", 0) or "Catmandu::Error"->throw("unable to read file") 2 0 2
21 IO => $self->fh,
22 validation => 0,
23 load_ext_dtd => 0
24 ) or Catmandu::Error->throw("unable to read file");
25  
26 2 $reader;
27 }
28  
29 10 ▶sub _parse_ocr_attr {
Subroutine _parse_ocr_attr: called CC=3 SCAR=11.2
30  
31 10 my ( $key, $value ) = @_;
32  
33 10 ▶ if ( $key eq "bbox" ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key eq 'bbox') { } 5 5 10
elsif ($key eq 'x_wconf') { } 0 5 5
34  
35 5 my @coords = map { int($_) } split( " ", $value );
36 return +{
37 5 x1 => $coords[0], y1 => $coords[1],
38 x2 => $coords[2], y2 => $coords[3]
39 };
40  
41 }
42 elsif ( $key eq "x_wconf" ) {
43  
44 0 return int($value);
45  
46 }
47  
48 5 $value;
49  
50 }
51  
52 5 ▶sub _parse_ocr_attrs {
Subroutine _parse_ocr_attrs: called CC=2 SCAR=7.1
53  
54 5 my $title = $_[0];
55  
56 map {
57 5 $_ =~ s/^\s+//o;
58 10 $_ =~ s/\s+$//o;
59  
60 10 my $idx = index( $_, " " );
61 10 my $key;
62 my $val;
63  
64 10 ▶ if( $idx >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= 0) { } 10 0 10
65 10 $key = substr( $_, 0, $idx );
66 10 $val = substr( $_, $idx + 1 );
67 10 $val = _parse_ocr_attr( $key, $val );
68 }
69 else {
70 0 $key = $_;
71 0 $val = undef;
72 }
73  
74 10 $key => $val;
75 } split( ";", $title );
76  
77 }
78  
79 5 ▶sub _parse_coords {
Subroutine _parse_coords: called CC=1 SCAR=0.0
80 5 my $title = $_[0];
81 5 my %attrs = _parse_ocr_attrs( $title );
82 5 my $bbox = $attrs{bbox};
83 5 my $x = $bbox->{x1};
84 5 my $y = $bbox->{y1};
85 5 my $w = $bbox->{x2} - $bbox->{x1};
86 5 my $h = $bbox->{y2} - $bbox->{y1};
87  
88 +{
89 5 x => $x,
90 y => $y,
91 w => $w,
92 h => $h
93 };
94 }
95  
96 89 ▶sub _read {
Subroutine _read: called CC=3 SCAR=11.7
97 89 ▶ my $state = $_[0]->reader->read() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $state = $_[0]->reader->read or return 89 0 89
98 89 ▶ $state < 0 && Catmandu::Error->throw("error occurred during parsing of file");
Branch: 1 branch
BranchTrueFalseTotal
$state < 0 and "Catmandu::Error"->throw("error occurred during parsing of file") 0 89 89
99 89 $state;
100 }
101  
102 3 ▶sub _next {
Subroutine _next: called CC=20 SCAR=30.0
103  
104 3 my $self = $_[0];
105 3 state $start_element = XML_READER_TYPE_ELEMENT;
106 3 state $end_element = XML_READER_TYPE_END_ELEMENT;
107  
108 3 my $reader = $self->reader();
109  
110 3 my $line;
111  
112 3 ▶ while( $self->_read ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_read) 89 0 89
113  
114 89 my $name = $reader->name();
115 89 my $nodeType = $reader->nodeType();
116 89 my $class = $reader->getAttribute("class");
117  
118 89 ▶ if ( $nodeType == $start_element && defined($class) && $class eq "ocr_page" ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($nodeType == $start_element and defined $class and $class eq 'ocr_page') { } 2 87 89
elsif ($nodeType == $start_element and defined $class and $class eq 'ocr_line') { } 3 84 87
elsif ($nodeType == $start_element and defined $class and $class eq 'ocr_word' || $class eq 'ocrx_word') { } 16 68 84
elsif (defined $line and $nodeType == $end_element) { } 3 112 115
Condition100%6/6
$nodeType == $start_element and defined $class and $class eq "ocr_page"
!ll&&!rl&&r
65222
$nodeType == $start_element and defined $class
!ll&&!rl&&r
511424
MC/DC100%3/3
$nodeType == $start_element and defined $class and $class eq "ocr_page"
$nodeType == $start_element defined $class $class eq "ocr_page"
Truth table100%4/4
A and B and C
A= $nodeType == $start_elementB= defined $classC= $class eq "ocr_page"
ABCresult
0XX0
10X0
1100
1111
119  
120 2 ▶ my $old_page = $self->current_page // +{ no => 0 };
Condition50%1/2
$self->current_page // {"no", 0}
l!l
02
MC/DC0%0/1
$self->current_page // {"no", 0}
$self->current_page
Truth table50%1/2
A
A= $self->current_page
Aresult
11
00
121  
122 2 my $title = $reader->getAttribute("title");
123 2 my $coords = _parse_coords( $title );
124  
125 $self->current_page({
126 2 no => $old_page->{no} + 1,
127 %$coords
128 });
129  
130 2 next;
131  
132 }
133 ▶ elsif ( $nodeType == $start_element && defined($class) && $class eq "ocr_line" ) {
Condition100%6/6
$nodeType == $start_element and defined $class and $class eq "ocr_line"
!ll&&!rl&&r
65193
$nodeType == $start_element and defined $class
!ll&&!rl&&r
511422
MC/DC100%3/3
$nodeType == $start_element and defined $class and $class eq "ocr_line"
$nodeType == $start_element defined $class $class eq "ocr_line"
Truth table100%4/4
A and B and C
A= $nodeType == $start_elementB= defined $classC= $class eq "ocr_line"
ABCresult
0XX0
10X0
1100
1111
134  
135 3 my $title = $reader->getAttribute("title");
136 3 my $coords = _parse_coords( $title );
137  
138 3 my $current_page = $self->current_page();
139 $line = +{
140 page => $current_page->{no},
141 page_x => $current_page->{x},
142 page_y => $current_page->{y},
143 page_w => $current_page->{w},
144 page_h => $current_page->{h},
145 3 %$coords,
146 text => []
147 };
148  
149 }
150 ▶ elsif ( $nodeType == $start_element && defined($class) && ($class eq "ocr_word" || $class eq "ocrx_word") ) {
Condition88%8/9
$nodeType == $start_element and defined $class and $class eq "ocr_word" || $class eq "ocrx_word"
!ll&&!rl&&r
65316
$nodeType == $start_element and defined $class
!ll&&!rl&&r
511419
$class eq "ocr_word" or $class eq "ocrx_word"
l!l&&r!l&&!r
0163
MC/DC75%3/4
$nodeType == $start_element and defined $class and $class eq "ocr_word" || $class eq "ocrx_word"
$nodeType == $start_element defined $class $class eq "ocr_word" $class eq "ocrx_word"
Truth table80%4/5
A and B and C or D
A= $nodeType == $start_elementB= defined $classC= $class eq "ocr_word"D= $class eq "ocrx_word"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
151  
152 # only include text nodes, and read until end of span
153 16 ▶ while( $reader->read() ){
Branch: 1 branch
BranchTrueFalseTotal
if ($reader->read) 46 0 46
154  
155 # text may be enclosed with <strong>
156 46 ▶ if( $reader->nodeType() == XML_READER_TYPE_TEXT ){
Branch: 2 branches
BranchTrueFalseTotal
if ($reader->nodeType == 3) { } 16 30 46
elsif ($reader->nodeType == $end_element and $reader->localName eq 'span') { } 16 21 37
157 16 push @{$line->{text}}, $reader->value();
158 }
159 ▶ elsif( $reader->nodeType == $end_element && $reader->localName eq "span" ){
Condition100%3/3
$reader->nodeType == $end_element and $reader->localName eq "span"
!ll&&!rl&&r
7716
MC/DC100%2/2
$reader->nodeType == $end_element and $reader->localName eq "span"
$reader->nodeType == $end_element $reader->localName eq "span"
Truth table100%3/3
A and B
A= $reader->nodeType == $end_elementB= $reader->localName eq "span"
ABresult
0X0
100
111
160 16 last;
161 }
162  
163 }
164  
165 }
166 ▶ elsif ( defined($line) && $nodeType == $end_element ) {
Condition100%3/3
defined $line and $nodeType == $end_element
!ll&&!rl&&r
47183
MC/DC100%2/2
defined $line and $nodeType == $end_element
defined $line $nodeType == $end_element
Truth table100%3/3
A and B
A= defined $lineB= $nodeType == $end_element
ABresult
0X0
100
111
167  
168 3 ▶ if ( $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line) 3 0 3
169  
170 3 $line->{text} = join(" ", @{ $line->{text} });
171  
172 }
173 3 last;
174  
175 }
176  
177 }
178  
179 3 $line;
180 }
181  
182 2 ▶sub generator {
Subroutine generator: called CC=1 SCAR=0.0
Pod undocumented
183  
184 2 my $self = $_[0];
185  
186 sub {
187 $self->_next();
188 2 };
189  
190 }
191  
192 1 1;
↑ Summary HOCR.pm »