blib/lib/SWISH/HiLiter.pm untested

filter: Statementstmt 0.0% 0 / 103 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 182 CC 10 SCAR 24.0
CC
10
Cov
0%
init
30.0
_get_swish_index_info
12.0
stem
12.0
CRAP 11.0
1 package SWISH::HiLiter;
2 use strict;
3 use Carp;
4 use Data::Dump qw( dump );
5 use Search::Tools::HiLiter;
6 use Search::Tools::Snipper;
7 use Search::Tools::UTF8;
8 use base qw( Search::Tools::Object );
9  
10 # the fuzzy word access was fixed in SWISH::API 0.04
11 # but this module will still work under 0.03 if not using fuzzy
12 eval { require SWISH::API; }; # eval per cpan bug request 14003
13 if ( $@
14 or ( $SWISH::API::VERSION eq '0.01' or $SWISH::API::VERSION eq '0.02' ) )
15 {
16 die "SWISH::HiLiter requires SWISH::API version 0.03 or newer\n";
17 }
18  
19 our $VERSION = '0.08';
20  
21 __PACKAGE__->mk_accessors(qw( hiliter snipper swish query ));
22  
23 sub init {
24 my $self = shift;
25 my %args = @_;
26  
27 # interrogate the SWISH::API object first to get word_chars, etc.
28 if ( !$args{swish} ) {
29 croak "SWISH::API object required";
30 }
31 my $token_meta = $self->_get_swish_index_info( $args{swish} );
32  
33 # normalize param names
34 for my $name ( keys %$token_meta ) {
35 my $val = $token_meta->{$name};
36 $name =~ s/(\w+?)([A-Z])/$1_\l$2/g;
37 $name = lc($name);
38 next if ( exists $args{$name} );
39 $args{$name} = $val;
40 }
41  
42 #dump $self;
43  
44 $args{stemmer} = undef;
45 unless ( $args{no_stemmer} ) {
46 $args{stemmer} = sub {
47 return $self->{_use_stemming} ? $self->stem( $_[1] ) : $_[1];
48 };
49 }
50 my %api_ok = map { $_ => $args{$_} } grep { $self->can($_) } keys %args;
51 %api_ok = $self->_normalize_args(%api_ok);
52 $self->SUPER::init(%api_ok);
53  
54 my $query = $self->query or croak "query required";
55  
56 # cache the query_parser for use in set_query()
57 $self->{_query_parser} = $query->qp;
58  
59 # create snipper and hiliter
60 my %snipper_args = map { $_ => $args{$_} }
61 grep { Search::Tools::Snipper->can($_) } keys %args;
62 my %hiliter_args = map { $_ => $args{$_} }
63 grep { Search::Tools::HiLiter->can($_) } keys %args;
64  
65 $self->{_snipper_args} = \%snipper_args;
66 $self->{_hiliter_args} = \%hiliter_args;
67  
68 $self->{hiliter}
69 ||= Search::Tools::HiLiter->new( %hiliter_args, query => $query );
70 $self->{snipper}
71 ||= Search::Tools::Snipper->new( %snipper_args, query => $query );
72 return $self;
73 }
74  
75 sub _get_swish_index_info {
76  
77 # takes a SWISH::API object and
78 # uses the SWISH methods to set WordChar, etc.
79  
80 my $self = shift;
81 my $swish_obj = shift or croak "SWISH::API object required";
82 my @head_names = $swish_obj->HeaderNames;
83 my @indexes = $swish_obj->IndexNames;
84  
85 # just use the first index, assuming user
86 # won't pass more than one with different Header values
87 my $index = shift @indexes;
88 $self->{_index} = $index; # cache for stem()
89  
90 my %token_meta;
91 for my $h (@head_names) {
92  
93 my @v = $swish_obj->HeaderValue( $index, $h );
94  
95 $self->{_index_headers}->{$h}
96 = scalar @v > 1
97 ? [@v]
98 : $v[0];
99  
100 $token_meta{$h} = quotemeta( to_utf8( $v[0] || '' ) )
101 if $h =~ /char/i;
102  
103 }
104  
105 # set stemmer flag if it was used in the index
106  
107 $self->{_use_stemming} = $self->{'Stemming Applied'};
108  
109 return \%token_meta;
110 }
111  
112 sub stem {
113 if ( $SWISH::API::VERSION < 0.04 ) {
114 die "stem() requires SWISH::API version 0.04 or newer\n";
115 }
116 my $self = shift;
117 my $w = shift;
118 my $index = $self->{_index} || ( $self->{swish}->IndexNames )[0];
119 $self->{_index} ||= $index;
120 my $fw = $self->{swish}->Fuzzify( $index, $w );
121 my @fuzz = $fw->WordList;
122  
123 if ( my $e = $fw->WordError ) {
124 warn "Error in Fuzzy WordList ($e): $!\n";
125 return undef;
126 }
127  
128 return $fuzz[0]; # we ignore possible doublemetaphone
129  
130 }
131  
132 sub light {
133 return shift->hiliter->light(@_);
134 }
135  
136 *setq = \&set_query;
137  
138 sub set_query {
139 my $self = shift;
140 my $q = shift;
141 if ( !defined $q ) {
142 croak "query required";
143 }
144  
145 # re-create hiliter and snipper objects
146 # this is mostly a backwards compat feature,
147 # as it would be just as easy to create a new SWISH::HiLiter
148 # for each query. All we save is the index interrogation.
149 $self->{query} = $self->{_query_parser}->parse($q);
150 $self->{hiliter}
151 = Search::Tools::HiLiter->new( %{ $self->{_hiliter_args} },
152 query => $self->{query} );
153 $self->{snipper}
154 = Search::Tools::Snipper->new( %{ $self->{_snipper_args} },
155 query => $self->{query} );
156 return $self->{query};
157 }
158  
159 sub snip {
160 return shift->snipper->snip(@_);
161 }
162  
163 1;
164 __END__