File Coverage

blib/lib/Text/Keywords/Container.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package Text::Keywords::Container;
2             BEGIN {
3 2     2   2250 $Text::Keywords::Container::AUTHORITY = 'cpan:GETTY';
4             }
5             BEGIN {
6 2     2   37 $Text::Keywords::Container::VERSION = '0.900';
7             }
8             # ABSTRACT: Class for a container of serveral Text::Keywords::List
9              
10 2     2   63 use 5.010;
  2         8  
  2         69  
11 2     2   11 use Moo;
  2         4  
  2         17  
12 2     2   2118 use Text::Keywords::Found;
  2         6  
  2         2631  
13              
14             has lists => (
15             is => 'ro',
16             required => 1,
17             );
18              
19             has shuffle => (
20             is => 'ro',
21             default => sub { 1 },
22             );
23              
24             has use_secondary => (
25             is => 'ro',
26             default => sub { 1 },
27             );
28              
29             has params => (
30             is => 'ro',
31             default => sub {{}},
32             );
33              
34             sub find_keywords {
35             my ( $self, $primary, $secondary ) = @_;
36             $primary = $secondary if !$primary;
37             return () if !$primary;
38             my @founds;
39             my @keywordlists;
40             my $klpos = 0;
41             for (@{$self->lists}) {
42             push @keywordlists, [ $_, $_->count ];
43             }
44             while (@keywordlists) {
45             my $klcount = scalar @keywordlists;
46             my $idx = $klpos % $klcount;
47             my $kl = $keywordlists[$idx];
48             $klpos++;
49             for my $keyword (@{$kl->[0]->keywords}[$kl->[0]->count - $kl->[1],$kl->[0]->count - 1]) {
50             $kl->[1]--;
51             my $found;
52             splice(@keywordlists, $idx, 1) if (!$kl->[1]);
53             my $rx = qr/(?:^|[^\w]|_)($keyword)(?:$|[^\w]|_)/i;
54             my @text_found;
55             my $primary_str = $primary;
56             push (@text_found, [$primary_str =~ $rx]), $primary_str =~ s{$rx}{} while $primary_str =~ $rx;
57             my @secondary_found;
58             if ($secondary && $self->use_secondary) {
59             my $con_str = $secondary;
60             push (@secondary_found, [$con_str =~ $rx]), $con_str =~ s{$rx}{} while $con_str =~ $rx;
61             }
62             for (@text_found) {
63             $found = 1;
64             my @matches = @{$_};
65             my $found_string = shift @matches;
66             my $already_found = 0;
67             for my $cur_found (@founds) {
68             if ($cur_found->found eq $found_string and @{$cur_found->matches} ~~ @matches) {
69             $already_found = 1; last;
70             }
71             }
72             next if $already_found;
73             my $found_in_secondary = 0;
74             my $cidx = 0;
75             for (@secondary_found) {
76             my @cmatches = @{$_};
77             my $found_cstring = shift @cmatches;
78             if ($found_string eq $found_cstring and @matches ~~ @cmatches) {
79             delete $secondary_found[$cidx];
80             }
81             $cidx++;
82             }
83             push @founds, $self->_new_found($found_string,1,$found_in_secondary,$kl->[0],$keyword,\@matches);
84             }
85             my @still_secondary_found;
86             if (@text_found) {
87             for (@secondary_found) {
88             push @still_secondary_found, $_ if ($_);
89             }
90             } else {
91             @still_secondary_found = @secondary_found;
92             }
93             for (@still_secondary_found) {
94             $found = 1;
95             my @matches = @{$_};
96             my $found_string = shift @matches;
97             my $already_found = 0;
98             for my $cur_found (@founds) {
99             if ($cur_found->found eq $found_string and @{$cur_found->matches} ~~ @matches) {
100             $already_found = 1; last;
101             }
102             }
103             next if $already_found;
104             push @founds, $self->_new_found($found_string,0,1,$kl->[0],$keyword,\@matches);
105             }
106             last if $self->shuffle and $found;
107             }
108             }
109             return @founds;
110             }
111              
112             sub _new_found {
113             my ( $self, $found, $in_primary, $in_secondary, $keywordlist, $keyword, $matches ) = @_;
114             return Text::Keywords::Found->new({
115             keyword => $keyword,
116             found => $found,
117             list => $keywordlist,
118             matches => $matches,
119             in_primary => $in_primary ? 1 : 0,
120             in_secondary => $in_secondary ? 1 : 0,
121             container => $self
122             });
123             }
124              
125             1;
126             __END__