Dancer/SearchApp/HTMLSnippet.pm

filter: Statementstmt 94.1% 48 / 51 Branchbran 85.7% 12 / 14 Conditioncond 63.6% 7 / 11 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 87.5% 7 / 8 Podpod 66.6% 2 / 3 totaltotal 83.8% 78 / 93 CC 13 SCAR 19.4
CC
13
Cov
88%
extract_highlights
11.2
make_snippet
3.0
cleanup_tika
2.0
CRAP 6.9
« Dancer/SearchApp/IndexSchema.pm ↑ Summary Dancer/SearchApp/Extractor/PDF.pm »
1 package Dancer::SearchApp::HTMLSnippet;
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use Filter::signatures;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use HTML::Restrict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 $VERSION = '0.06';
9  
10 =head1 NAME
11  
12 Dancer::SearchApp::HTMLSnippet - HTML snippet extractor
13  
14 =head1 SYNOPSIS
15  
16 my @document_snippets = Dancer::SearchApp::HTMLSnippet->extract_highlights(
17 html => $html,
18 hl_tag => '<em>',
19 hl_end => '</em>',
20 snippet_length => 150,
21 max_snippets => 8,
22 );
23  
24 =head1 METHODS
25  
26 =head2 C<< Dancer::SearchApp::HTMLSnippet->extract_highlights >>
27  
28 my @document_snippets = Dancer::SearchApp::HTMLSnippet->extract_highlights(
29 html => $html,
30 hl_tag => '<em>',
31 hl_end => '</em>',
32 snippet_length => 150,
33 max_snippets => 8,
34 );
35  
36 This extract the highlight snippets and metadata from the HTML
37 as prepared by Tika and highlightedd by Elasticsearch. It
38 returns a list of hash references, each containing a (well-formed)
39 HTML snippet containing the highlights and a C<page> entry
40 noting the original page number if the snippet originated from
41 within a C<< <p class="page\d+"> >> section (or crosses that)
42  
43 {
44 html => 'this is a <b>result</b> you searched for',
45 page => 42,
46 }
47  
48 =cut
49  
50 9 ▶sub make_snippet( $html, $from, $to, $max_length ) {
Subroutine make_snippet: called CC=3 SCAR=11.0
Pod undocumented
51 9 my $start = $from->{start};
52 9 my $end = $to->{end};
53 9 my $fudge = int(( $max_length - ($end-$start)) / 2); # / for Filter::Simple
54
55 # We want to start at something akin to a word boundary
56 9 ▶ if( substr($html,$start-$fudge-1,$fudge+2) =~ /\s+(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($html, $start - $fudge - 1, $fudge + 2) =~ /\s+(.*)$/) 8 1 9
57 8 $start-= length($1)
58 };
59  
60 9 $fudge = $max_length - ($end-$start);
61 9 ▶ $end += length($1)
Branch: 1 branch
BranchTrueFalseTotal
if substr($html, $end, $fudge) =~ /(.*)\s+/ 9 0 9
62 if( substr($html,$end,$fudge) =~ /(.*)\s+/ );
63
64 return +{
65 9 start => $start,
66 end => $end,
67 length => $end-$start,
68 };
69 };
70  
71 =head2 C<< Dancer::SearchApp::HTMLSnippet->extract_highlights >>
72  
73 my @hits = Dancer::SearchApp::HTMLSnippet->extract_highlights(
74 html => $html,
75 max_length => 300,
76 );
77
78 for my $entry (@hits) {
79 print "Match: $entry->{start} ($entry->{length} bytes)\n";
80 };
81  
82 =cut
83  
84 2 ▶sub extract_highlights( $class, %options ) {
Subroutine extract_highlights: called CC=11 SCAR=24.1
Pod documented
85 2 ▶ $options{ max_snippets } ||= 8;
Condition50%1/2
$options{"max_snippets"} ||= 8
l!l
02
MC/DC0%0/1
$options{"max_snippets"} ||= 8
$options{"max_snippets"}
Truth table50%1/2
A
A= $options{"max_snippets"}
Aresult
11
00
86 2 ▶ $options{ max_length } ||= 150;
Condition50%1/2
$options{"max_length"} ||= 150
l!l
20
MC/DC0%0/1
$options{"max_length"} ||= 150
$options{"max_length"}
Truth table50%1/2
A
A= $options{"max_length"}
Aresult
11
00
87 2 ▶ $options{ hl_tag } ||= '<em>';
Condition50%1/2
$options{"hl_tag"} ||= "<em>"
l!l
02
MC/DC0%0/1
$options{"hl_tag"} ||= "<em>"
$options{"hl_tag"}
Truth table50%1/2
A
A= $options{"hl_tag"}
Aresult
11
00
88 2 ▶ $options{ hl_end } ||= '</em>';
Condition50%1/2
$options{"hl_end"} ||= "</em>"
l!l
02
MC/DC0%0/1
$options{"hl_end"} ||= "</em>"
$options{"hl_end"}
Truth table50%1/2
A
A= $options{"hl_end"}
Aresult
11
00
89 2 my $html = $options{ html };
90 2 my @highlights;
91 2 ▶ while( $html =~ /(\Q$options{hl_tag}\E(.*?)\Q$options{hl_end}\E)/g ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /(\Q$options{"hl_tag"}\E(.*?)\Q$options{"hl_end"}\E)/g) 1150 2 1152
92 1150 push @highlights, {
93 start => pos($html)-length($1),
94 # Maybe count text outside of tags instead?!
95 length => length($1),
96 end => pos($html),
97 word => "$1",
98 };
99 };
100
101 # Now, find the first matches (hardcoded, instead of finding the
102 # best snippets in the document)
103
104 2 my @snippets;
105
106 2 ▶ if( @highlights ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@highlights) 2 0 2
107 # We can stop once we are by
108 2 my $last = $highlights[-1]->{start};
109 2 my $curr = 0;
110 2 my $gather = 0;
111 2 ▶ while( @snippets < $options{ max_snippets }
Branch: 1 branch
BranchTrueFalseTotal
if (@snippets < $options{"max_snippets"} and $curr + $gather < @highlights) 31 2 33
Condition100%3/3
@snippets < $options{"max_snippets"} and $curr + $gather < @highlights
!ll&&!rl&&r
1131
MC/DC100%2/2
@snippets < $options{"max_snippets"} and $curr + $gather < @highlights
@snippets < $options{"max_snippets"} $curr + $gather < @highlights
Truth table100%3/3
A and B
A= @snippets < $options{"max_snippets"}B= $curr + $gather < @highlights
ABresult
0X0
100
111
112 and ($curr+$gather) < @highlights
113 ) {
114 # gather up as many highlights as fit for the next snippet
115 my $snippet_length = $highlights[$curr+$gather]->{end}
116 - $highlights[$curr]->{start}
117 31 ;
118 31 ▶ if( $snippet_length < $options{ max_length }) {
Branch: 1 branch
BranchTrueFalseTotal
if ($snippet_length < $options{'max_length'}) { } 23 8 31
119 23 $gather++
120  
121 } else {
122 # Snippet got too long
123 # we should later readjust / center the snippet on the match(es)
124 push @snippets,
125 make_snippet( $html,
126 $highlights[$curr],
127 $highlights[$curr+$gather-1],
128 8 $options{ max_length });
129 8 $curr += $gather;
130 8 $gather = 0;
131 };
132 };
133
134 2 ▶ if( ! @snippets) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@snippets) 1 1 2
135 push @snippets,
136 make_snippet( $html,
137 $highlights[$curr],
138 $highlights[$curr],
139 1 $options{max_length});
140 };
141 };
142
143 @snippets
144 2 }
145  
146 =head2 C<< Dancer::SearchApp::HTMLSnippet->cleanup_tika >>
147  
148 my $content = Dancer::SearchApp::HTMLSnippet->cleanup_tika( $html );
149  
150 Cleans up HTML output from Apache Tika.
151  
152 =cut
153  
154 0 ▶sub cleanup_tika( $class, $html ) {
Subroutine cleanup_tika: not called CC=1 SCAR=6.9
Pod documented
155 0 my $p = HTML::Restrict->new(
156 # rules => { div => ['class'], },
157 rules => {
158 #p => ['class'],
159 div => ['class', { class => qr/^page$/i }],
160 table => [],
161 tbody => [],
162 thead => [],
163 tr => [],
164 td => [],
165 a => [],
166 ul => [],
167 ol => [],
168 li => [],
169 },
170 replace_img => 1,
171 strip_enclosed_content => [ 'script', 'style', 'head' ]
172 );
173
174 # rewrite the HTML to have the page numbers!
175 # Later
176 0 my $r = $p->process($html);
177 #warn $r;
178 0 return $r
179 }
180  
181 4 1;
182  
183 =head1 BUG TRACKER
184  
185 Please report bugs in this module via the RT CPAN bug queue at
186 L<https://rt.cpan.org/Public/Dist/Display.html?Name=Dancer-SearchApp>
187 or via mail to L<dancer-searchapp-Bugs@rt.cpan.org>.
188  
189 =head1 AUTHOR
190  
191 Max Maischein C<corion@cpan.org>
192  
193 =head1 COPYRIGHT (c)
194  
195 Copyright 2014-2016 by Max Maischein C<corion@cpan.org>.
196  
197 =head1 LICENSE
198  
199 This module is released under the same terms as Perl itself.
200  
201 =cut
« Dancer/SearchApp/IndexSchema.pm ↑ Summary Dancer/SearchApp/Extractor/PDF.pm »